-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathsparse_table.cpp
More file actions
57 lines (50 loc) · 1.36 KB
/
sparse_table.cpp
File metadata and controls
57 lines (50 loc) · 1.36 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
49
50
51
52
53
54
55
56
57
// N : the maximum size of the array.
// M : ceil(log2(N)).
const int N = 100100, M = 20;
// The array to compute its sparse table.
int n, a[N];
//
// Sparse table related variables.
//
// ST[j][i] : the sparse table value (min, max, ...etc) in the interval [i, i + (2^j) - 1].
// LOG[i] : floor(log2(i)).
int ST[M][N], LOG[N];
/**
* Builds the sparse table for computing min/max/gcd/lcm/...etc
* for any contiguous sub-segment of the array.
*
* This is an example of computing the index of the minimum value.
*
* Complexity: O(n.log(n))
*/
void buildST() {
LOG[0] = -1;
for (int i = 0; i < n; ++i) {
ST[0][i] = i;
LOG[i + 1] = LOG[i] + !(i & (i + 1));
}
for (int j = 1; (1 << j) <= n; ++j) {
for (int i = 0; (i + (1 << j)) <= n; ++i) {
int x = ST[j - 1][i];
int y = ST[j - 1][i + (1 << (j - 1))];
ST[j][i] = (a[x] <= a[y] ? x : y);
}
}
}
/**
* Queries the sparse table for the value of the interval [l, r]
* (i.e. from l to r inclusive).
*
* Complexity: O(1)
*
* @param l the left index of the range (inclusive).
* @param r the right index of the range (inclusive).
*
* @return the computed value of the given interval.
*/
int query(int l, int r) {
int g = LOG[r - l + 1];
int x = ST[g][l];
int y = ST[g][r - (1 << g) + 1];
return (a[x] <= a[y] ? x : y);
}