-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegTree.cpp
More file actions
80 lines (62 loc) · 1.71 KB
/
SegTree.cpp
File metadata and controls
80 lines (62 loc) · 1.71 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <bits/stdc++.h>
using namespace std;
struct SegTree {
struct Node {
long long x;
static auto join(Node const& lhs, Node const& rhs) -> Node {
return {lhs.x + rhs.x};
}
void update(Node rhs) {
x += rhs.x;
}
};
int n;
vector<Node> tree;
SegTree(int n) :
n(n), tree(n*4)
{};
SegTree(vector<Node> const& v) :
n(v.size()), tree(n*4)
{
build(v, 0, 0, n-1);
}
auto build(vector<Node> const& v, int no, int l, int r) -> void {
if (l == r) {
tree[no] = v[l];
}
else {
int m = (l+r)/2;
build(v, no*2+1, l, m);
build(v, no*2+2, m+1, r);
tree[no] = Node::join(tree[no*2+1], tree[no*2+2]);
}
}
auto get(int no, int l, int r, int a, int b) -> Node {
if (a <= l and r <= b) {
return tree[no];
}
int m = (l+r)/2;
if (b <= m) return get(no*2+1, l, m, a, b);
else if (a > m) return get(no*2+2, m+1, r, a, b);
return Node::join(
get(no*2+1, l, m, a, b),
get(no*2+2, m+1, r, a, b)
);
}
auto get(int a, int b) -> Node {
return get(0, 0, n-1, a, b);
}
auto upd(int no, int l, int r, int p, Node const& val) -> void {
if (l == r) {
tree[no].update(val);
return;
}
int m = (l+r)/2;
if (p <= m) upd(no*2+1, l, m, p, val);
else upd(no*2+2, m+1, r, p, val);
tree[no] = Node::join(tree[no*2+1], tree[no*2+2]);
}
auto upd(int p, Node const& val) -> void {
upd(0, 0, n-1, p, val);
}
};