-
Notifications
You must be signed in to change notification settings - Fork 106
/
STsum_update.cpp
45 lines (39 loc) · 1.06 KB
/
STsum_update.cpp
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
class SGTree {
vector<int> seg;
public:
SGTree(int n) {
seg.resize(4 * n + 1);
}
void build(int ind, int low, int high, int arr[]) {
if (low == high) {
seg[ind] = arr[low];
return;
}
int mid = (low + high) / 2;
build(2 * ind + 1, low, mid, arr);
build(2 * ind + 2, mid + 1, high, arr);
seg[ind] = min(seg[2 * ind + 1], seg[2 * ind + 2]);
}
int query(int ind, int low, int high, int l, int r) {
// no overlap
// l r low high or low high l r
if (r < low || high < l) return INT_MAX;
// complete overlap
// [l low high r]
if (low >= l && high <= r) return seg[ind];
int mid = (low + high) >> 1;
int left = query(2 * ind + 1, low, mid, l, r);
int right = query(2 * ind + 2, mid + 1, high, l, r);
return min(left, right);
}
void update(int ind, int low, int high, int i, int val) {
if (low == high) {
seg[ind] = val;
return;
}
int mid = (low + high) >> 1;
if (i <= mid) update(2 * ind + 1, low, mid, i, val);
else update(2 * ind + 2, mid + 1, high, i, val);
seg[ind] = min(seg[2 * ind + 1], seg[2 * ind + 2]);
}
};