- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse_table.cpp
More file actions
Latest commit
23 lines (23 loc) · 681 Bytes
/
Copy pathsparse_table.cpp
File metadata and controls
23 lines (23 loc) · 681 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template<constint& (*f)(constint&, constint&)>
classSparseTable {
V(VI) sparse_table;
public:
template<typename T>
SparseTable(int N, const T &A) : sparse_table(21, VI(N)) {
REP(i, N) {
sparse_table[0][i] = A[i];
}
int k = 1;
while ((1 << k) <= N) {
REP1(i, 0, N - (1 << k)) {
sparse_table[k][i] = f(sparse_table[k-1][i], sparse_table[k-1][i+(1<<(k-1))]);
}
k++;
}
}
intquery(int l, int r) {
assert(l <= r);
int k = sizeof(int) * 8 - 1 - __builtin_clz(r+1-l);
returnf(sparse_table[k][l], sparse_table[k][r+1-(1<<k)]);
}
};