-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSegment Tree.cpp
More file actions
65 lines (58 loc) · 1.48 KB
/
Segment Tree.cpp
File metadata and controls
65 lines (58 loc) · 1.48 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
58
59
60
61
62
63
64
65
// SegTree
//
struct node {
ll val;
node() {
val = 0;
}
node(ll val) : val(val) {
}
node operator + (const node &rhs) const {
return node(val + rhs.val);
// return node(val op rhs.val);
}
};
struct SegTree {
int n;
vector<node> t;
SegTree(int n) : n(n) {
t.assign(4 * n, node());
}
SegTree(vector<int> &a) {
n = a.size();
t.resize(4 * n);
build(1, 0, n - 1, a);
}
void build(int pos, int l, int r, vector<int> &a) {
if(l == r) {
t[pos] = node(a[l]);
return;
}
int m = (l + r) / 2;
build(2 * pos, l, m, a);
build(2 * pos + 1, m + 1, r, a);
t[pos] = t[2 * pos] + t[2 * pos + 1];
}
void update(int i, int val, int pos, int l, int r) {
if(l == r) {
t[pos] = node(val);
return;
}
int m = (l + r) / 2;
if(i <= m) update(i, val, 2 * pos, l, m);
else update(i, val, 2 * pos + 1, m + 1, r);
t[pos] = t[2 * pos] + t[2 * pos + 1];
}
void update(int i, int val) {
update(i, val, 1, 0, n - 1);
}
node query(int i, int j, int pos, int l, int r) {
if(j < l || r < i) return node();
if(i <= l && r <= j) return t[pos];
int m = (l + r) / 2;
return query(i, j, 2 * pos, l, m) + query(i, j, 2 * pos + 1, m + 1, r);
}
node query(int i, int j) {
return query(i, j, 1, 0, n - 1);
}
};