-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse_table.cpp
More file actions
51 lines (41 loc) · 886 Bytes
/
Copy pathsparse_table.cpp
File metadata and controls
51 lines (41 loc) · 886 Bytes
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
49
/*
here we store the the sum of all 1,2,4,8... elements from ith index, so S[n][LOG2(max)]
we will use the property 4 + 4 = 8, 8 + 8 = 16 and so on, you can think of
*/
#define int ll
const int K = 62;
const int N = 1e5+5;
int lg[N];
int s[N][K];
int a[N];
int n;
void build() {
FOR(i,0,n) {
s[i][0] = a[i];
}
lg[1] = 0;
FOR(i,2,N) {
lg[i] = lg[i/2] + 1; // for range minimum query
}
FOR(j,1,K+1) {
for(int i = 0; i + (1ll << j) <= n; ++i) {
// storing sum of next 2^j elements starting from i, given we have calculated
// for 2^(j-1)
s[i][j] = min(s[i][j-1], s[i + (1ll << (j-1))][j-1]); // change
}
}
}
int RMQ(int l,int r) {
int j = lg[r-l+1];
return min(s[l][j],s[r - (1ll << j) + 1][j]); // change
}
int Query(int l,int r) {
int ans = 0;
REV(i,K,0) {
if((1 << i) <= r - l + 1) {
ans += s[l][i];
l += (1 << i);
}
}
return ans;
}