-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparse-table.sublime-snippet
53 lines (42 loc) · 1.09 KB
/
sparse-table.sublime-snippet
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
<snippet>
<content><![CDATA[
struct sparse {
int n;
// REMEMBER TO CHANGE
const static int LOG = 20;
vector<vector<int>> a;
vector<int> r;
sparse(vector<int> A) {
a = vector<vector<int>>(LOG, vector<int>(A.size()));
n = A.size();
r = vector<int>(n + 1, 0);
for (int i = 0; i < n; i++)
a[0][i] = A[i];
for (int i = 1; i < LOG; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = a[i - 1][j];
if (j + (1 << (i - 1)) < n)
a[i][j] = min(a[i][j], a[i - 1][j + (1 << (i - 1))]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < LOG; j++) {
if ((1 << j) <= i) {
r[i] = j;
}
}
}
}
int query(int L, int R) {
int t = r[R - L + 1];
int lo = L;
int hi = R - (1 << t) + 1;
return min(a[t][lo], a[t][hi]);
}
};
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>sparse</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>