-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersistentSegmentTree.cpp
More file actions
40 lines (40 loc) · 1.04 KB
/
PersistentSegmentTree.cpp
File metadata and controls
40 lines (40 loc) · 1.04 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
struct Node {
int cnt = 0;
Node *l, *r;
Node(): cnt(0), l(nullptr), r(nullptr) {}
Node(Node* root, int value): cnt(root->cnt + value), l(nullptr), r(nullptr) {}
Node(Node* L, Node* R): cnt(0), l(L), r(R) {
if (l) { cnt += l->cnt; }
if (r) { cnt += r->cnt; }
}
};
struct Segtree {
Node* Build(int L, int R) {
if (L == R) {
return new Node();
}
int M = (L + R) / 2;
return new Node(Build(L, M), Build(M + 1, R));
}
Node* modify(Node* root, int L, int R, int pos, int value) {
if (L == R) {
return new Node(root, value);
}
int M = (L + R) / 2;
if (pos <= M) {
return new Node(modify(root->l, L, M, pos, value), root->r);
} else {
return new Node(root->l, modify(root->r, M + 1, R, pos, value));
}
}
int query(Node *root, int L, int R, int ql, int qr) {
if (L > qr or R < ql) {
return 0;
}
if (L >= ql and R <= qr) {
return root->cnt;
}
int M = (L + R) / 2;
return query(root->l, L, M, ql, qr) + query(root->r, M + 1, R, ql, qr);
}
};