-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegment-Tree.cpp
177 lines (147 loc) · 3.88 KB
/
Segment-Tree.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include<bits/stdc++.h>
using namespace std;
template<typename Info>
class SegTree {
public:
int n;
vector<Info> info;
SegTree(int _n) {
n = _n;
info = vector<Info>(4 * n + 1);
}
SegTree(const vector<Info> &init) : SegTree(init.size()) {
function<void(int, int, int)> build = [&](int p, int l, int r) -> void {
if(l == r) {
info[p] = init[l];
return;
}
int m = l + (r - l) / 2;
build(2 * p + 1, l, m);
build(2 * p + 2, m + 1, r);
pull(p);
};
build(0, 0, n - 1);
}
void pull(int p) {
info[p] = info[2 * p + 1] + info[2 * p + 2];
}
void modify(int p, int l, int r, int x, const Info& v) {
if(r == l) {
info[p] = v;
return;
}
int m = l + (r - l) / 2;
if(x <= m) {
modify(2 * p + 1, l, m, x, v);
} else {
modify(2 * p + 2, m + 1, r, x, v);
}
pull(p);
}
void modify(int p, const Info& v) {
modify(0, 0, n - 1, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if(y < l or r < x) {
return Info();
}
if(x <= l and r <= y) {
return info[p];
}
int m = l + (r - l) / 2;
return rangeQuery(2 * p + 1, l, m, x, y) + rangeQuery(2 * p + 2, m + 1, r, x, y);
}
Info rangeQuery(int l, int r) {
return rangeQuery(0, 0, n - 1, l, r);
}
};
template<typename Info, typename Tag>
class LazySegTree {
public:
int n;
vector<Info> info;
vector<Tag> tag;
LazySegTree(int _n) {
n = _n;
info = vector<Info>(4 * n + 1);
tag = vector<Tag>(4 * n + 1);
}
LazySegTree(const vector<Info> &init) : LazySegTree(init.size()) {
function<void(int, int, int)> build = [&](int p, int l, int r) -> void {
if(r == l) {
info[p] = init[l];
return;
}
int m = l + (r - l) / 2;
build(2 * p + 1, l, m);
build(2 * p + 2, m + 1, r);
pull(p);
};
build(0, 0, n - 1);
}
void pull(int p) {
info[p] = info[2 * p + 1] + info[2 * p + 2];
}
void apply(int p, const Tag &v) {
info[p].apply(v);
tag[p].apply(v);
}
void push(int p) {
apply(2 * p + 1, tag[p]);
apply(2 * p + 2, tag[p]);
tag[p] = Tag();
}
void modify(int p, int l, int r, int x, const Info &v) {
if(r == l) {
info[p] = v;
return;
}
int m = l + (r - l) / 2;
push(p);
if(x <= m) {
modify(2 * p + 1, l, m, x, v);
} else {
modify(2 * p + 2, m + 1, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(0, 0, n - 1, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if(y < l or r < x) {
return Info();
}
if(x <= l and r <= y) {
return info[p];
}
int m = l + (r - l) / 2;
push(p);
return rangeQuery(2 * p + 1, l, m, x, y) + rangeQuery(2 * p + 2, m + 1, r, x, y);
}
Info rangeQuery(int l, int r) {
return rangeQuery(0, 0, n - 1, l, r);
}
void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
if(y < l or r < x) {
return;
}
if(x <= l and r <= y) {
apply(p, v);
return;
}
int m = l + (r - l) / 2;
push(p);
rangeApply(2 * p + 1, l, m, x, y, v);
rangeApply(2 * p + 2, m + 1, r, x, y, v);
pull(p);
}
void rangeApply(int l, int r, const Tag &v) {
return rangeApply(0, 0, n - 1, l, r, v);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}