-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary lifting.cpp
More file actions
48 lines (38 loc) · 1.03 KB
/
Copy pathbinary lifting.cpp
File metadata and controls
48 lines (38 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// template for binary lifting
// first initiate the 0th col with first parent
// call query function for kth parent
const int Log=20 ;
int sparse[M+2][Log+2] ;
// returns kth parent for id th node
// return -1 kth parent does not exist
int SparseTableQuery(int id, int k) {
int curr ,par = id ;
rep(i , Log+1) if(checkbit(k ,i ) ) curr= i;
while(1) {
// dbg(curr, par) ;
if(par== -1)break ;
if(checkbit( k, curr) ==0 )curr-- ;
else {
par = sparse[par][curr] ;
curr-- ;
}
if( curr<0) break ;
}
//cout << endl;
return par ;
}
void SparseTableInit(int N ) {
int n=N ;
for(int j=1; j< Log; j++ ) {
for(int i=0; i< N ;i++) {
if(sparse[i ][ j-1 ] !=-1) {
sparse[i][j ] = sparse[ sparse[i ][j -1] ][j-1] ;
}
}
}
// rep(i, n ) {
// cout << i << "-> " ;
// rep(j , Log) cout<< sparse[i][j] << ' ' ;
// cout << endl ;
// }
}