-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparse Table.cpp
More file actions
32 lines (32 loc) · 1.07 KB
/
Copy pathSparse Table.cpp
File metadata and controls
32 lines (32 loc) · 1.07 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
template <class T> struct SparseTable {
int n, logn;
vector <vector <T>> dp1,dp2;
SparseTable(int _n) {
n = _n;
logn = ceil(log2(n)) + 1;
dp1.assign(logn, vector <T> (n, 0));
dp2.assign(logn, vector <T> (n, 0));
}
T comb1(T x, T y) { return min(x, y); }
T comb2(T x, T y) { return max(x, y); }
void build(vector<T> &a) {
for(int i = 0; i < n; i ++) {
dp1[0][i] = a[i];
dp2[0][i] = a[i];
}
for(int j = 1; j < logn; j ++) {
for(int i = 0; i + (1LL << j) <= n; i ++) {
dp1[j][i] = comb1(dp1[j - 1][i], dp1[j - 1][i + (1LL << (j - 1))]);
dp2[j][i] = comb2(dp2[j - 1][i], dp2[j - 1][i + (1LL << (j - 1))]);
}
}
}
T query1(int l, int r) {
int ln = 31 - __builtin_clz(r - l + 1);
return comb1(dp1[ln][l], dp1[ln][r - (1LL << ln) + 1]);
}
T query2(int l, int r) {
int ln = 31 - __builtin_clz(r - l + 1);
return comb2(dp2[ln][l], dp2[ln][r - (1LL << ln) + 1]);
}
};