-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegtreeDynamic.cpp
More file actions
70 lines (61 loc) · 1.58 KB
/
Copy pathSegtreeDynamic.cpp
File metadata and controls
70 lines (61 loc) · 1.58 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
template <typename Info>
struct Seg {
i64 left, right;
Info info;
Seg *left_child = nullptr, *right_child = nullptr;
Seg(i64 lb, i64 rb) {
left = lb;
right = rb;
}
void extend() {
if (!left_child && left + 1 < right) {
int t = (left + right) / 2;
left_child = new Seg(left, t);
right_child = new Seg(t, right);
}
}
void pull() {
info = Info();
if (left_child)
info = info + left_child->info;
if (right_child)
info = info + right_child->info;
}
void modify(i64 k, const Info& v) {
if (left + 1 == right) {
info = v;
return;
}
i64 mid = (left + right) / 2;
if (k < mid) {
if (!left_child)
left_child = new Seg(left, mid);
left_child->modify(k, v);
} else {
if (!right_child)
right_child = new Seg(mid, right);
right_child->modify(k, v);
}
pull();
}
Info rangeQuery(i64 lq, i64 rq) {
if (lq <= left && right <= rq)
return info;
if (max(left, lq) >= min(right, rq))
return Info();
Info ans;
if (left_child)
ans = ans + left_child->rangeQuery(lq, rq);
if (right_child)
ans = ans + right_child->rangeQuery(lq, rq);
return ans;
}
};
constexpr i64 INF = 1e9 + 5;
struct Info {
i64 x;
Info(i64 x = 0) : x(x) {}
};
Info operator+(const Info& a, const Info& b) {
return Info(a.x + b.x);
}