-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegtree.sublime-snippet
64 lines (49 loc) · 1.23 KB
/
segtree.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
54
55
56
57
58
59
60
61
62
63
64
<snippet>
<content><![CDATA[
template <typename T>
struct Segtree
{
int n;
int bstart, stsize;
vector<T> st;
/*
To fill in
*/
const T zero;
T op(T a, T b) {
}
Segtree(int _n) {
n = _n;
bstart = 1;
while (bstart < n) bstart *= 2;
stsize = bstart * 2 - 1;
bstart--;
st = vector<T>(stsize, zero);
}
void update(int ind, T v) {
ind += bstart;
st[ind] = v;
while (1) {
if (!ind) break;
ind = (ind - 1) / 2;
st[ind] = op(st[ind * 2 + 1], st[ind * 2 + 2]);
}
}
T query(int c, int cmin, int cmax, int minb, int maxb) {
if (cmin >= minb && cmax <= maxb) return st[c];
if (cmin > maxb || cmax < minb) return zero;
return op(query(c * 2 + 1, cmin, (cmin + cmax) / 2, minb, maxb),
query(c * 2 + 2, 1 + (cmin + cmax) / 2, cmax, minb, maxb));
}
T query(int lo, int hi) {
if (lo > hi) return zero;
if (lo == hi) return st[lo + bstart];
return query(0, 0, bstart, lo, hi);
}
};
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>segtree</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>