-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse_table.cpp
More file actions
30 lines (23 loc) · 782 Bytes
/
sparse_table.cpp
File metadata and controls
30 lines (23 loc) · 782 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
template<typename T>
struct SparseTable {
using F = function<T(T,T)>;
F f;
vector<vector<T>> st;
SparseTable(const vector<T> &v, const F &f_) : f(f_) {
size_t b = 0;
while(((size_t)1 << b) <= v.size()) b++;
st.assign(b, vector<T>(1<<b));
copy_n(v.begin(), v.size(), st[0].begin());
for(size_t i=1; i<b; i++)
for(int j=0; j+(1<<(i-1)) + (1<<(i-1)) < (1<<b); j++)
st[i][j] = f(st[i-1][j], st[i-1][j + (1<<(i-1))]);
}
// [l, r)
inline T query(size_t l, size_t r) const {
size_t b = 0;
while(((size_t)1 << b) <= (r - l)) b++;
b--;
return f(st[b][l], st[b][r - (1<<b)]);
}
};
// SparseTable<long> st(a, [&](long a, long b){return min(a, b);});