Skip to content

Commit 08f99b7

Browse files
authored
Create SegmentTree.cpp
1 parent 7e8ca09 commit 08f99b7

File tree

1 file changed

+209
-0
lines changed

1 file changed

+209
-0
lines changed

SegmentTree.cpp

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
class segtree {
2+
public:
3+
struct node {
4+
int mx = -1;
5+
6+
void apply(int l, int r, int v) {
7+
mx = v;
8+
}
9+
};
10+
11+
node unite(const node &a, const node &b) const {
12+
node res;
13+
res.mx = max(a.mx, b.mx);
14+
return res;
15+
}
16+
17+
inline void push(int x, int l, int r) {
18+
}
19+
20+
inline void pull(int x, int z) {
21+
tree[x] = unite(tree[x + 1], tree[z]);
22+
}
23+
24+
int n;
25+
vector<node> tree;
26+
27+
void build(int x, int l, int r) {
28+
if (l == r) {
29+
return;
30+
}
31+
int y = (l + r) >> 1;
32+
int z = x + ((y - l + 1) << 1);
33+
build(x + 1, l, y);
34+
build(z, y + 1, r);
35+
pull(x, z);
36+
}
37+
38+
template <typename M>
39+
void build(int x, int l, int r, const vector<M> &v) {
40+
if (l == r) {
41+
tree[x].apply(l, r, v[l]);
42+
return;
43+
}
44+
int y = (l + r) >> 1;
45+
int z = x + ((y - l + 1) << 1);
46+
build(x + 1, l, y, v);
47+
build(z, y + 1, r, v);
48+
pull(x, z);
49+
}
50+
51+
node get(int x, int l, int r, int ll, int rr) {
52+
if (ll <= l && r <= rr) {
53+
return tree[x];
54+
}
55+
int y = (l + r) >> 1;
56+
int z = x + ((y - l + 1) << 1);
57+
push(x, l, r);
58+
node res{};
59+
if (rr <= y) {
60+
res = get(x + 1, l, y, ll, rr);
61+
} else {
62+
if (ll > y) {
63+
res = get(z, y + 1, r, ll, rr);
64+
} else {
65+
res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr));
66+
}
67+
}
68+
pull(x, z);
69+
return res;
70+
}
71+
72+
template <typename... M>
73+
void modify(int x, int l, int r, int ll, int rr, const M&... v) {
74+
if (ll <= l && r <= rr) {
75+
tree[x].apply(l, r, v...);
76+
return;
77+
}
78+
int y = (l + r) >> 1;
79+
int z = x + ((y - l + 1) << 1);
80+
push(x, l, r);
81+
if (ll <= y) {
82+
modify(x + 1, l, y, ll, rr, v...);
83+
}
84+
if (rr > y) {
85+
modify(z, y + 1, r, ll, rr, v...);
86+
}
87+
pull(x, z);
88+
}
89+
90+
int find_first_knowingly(int x, int l, int r, const function<bool(const node&)> &f) {
91+
if (l == r) {
92+
return l;
93+
}
94+
push(x, l, r);
95+
int y = (l + r) >> 1;
96+
int z = x + ((y - l + 1) << 1);
97+
int res;
98+
if (f(tree[x + 1])) {
99+
res = find_first_knowingly(x + 1, l, y, f);
100+
} else {
101+
res = find_first_knowingly(z, y + 1, r, f);
102+
}
103+
pull(x, z);
104+
return res;
105+
}
106+
107+
int find_first(int x, int l, int r, int ll, int rr, const function<bool(const node&)> &f) {
108+
if (ll <= l && r <= rr) {
109+
if (!f(tree[x])) {
110+
return -1;
111+
}
112+
return find_first_knowingly(x, l, r, f);
113+
}
114+
push(x, l, r);
115+
int y = (l + r) >> 1;
116+
int z = x + ((y - l + 1) << 1);
117+
int res = -1;
118+
if (ll <= y) {
119+
res = find_first(x + 1, l, y, ll, rr, f);
120+
}
121+
if (rr > y && res == -1) {
122+
res = find_first(z, y + 1, r, ll, rr, f);
123+
}
124+
pull(x, z);
125+
return res;
126+
}
127+
128+
int find_last_knowingly(int x, int l, int r, const function<bool(const node&)> &f) {
129+
if (l == r) {
130+
return l;
131+
}
132+
push(x, l, r);
133+
int y = (l + r) >> 1;
134+
int z = x + ((y - l + 1) << 1);
135+
int res;
136+
if (f(tree[z])) {
137+
res = find_last_knowingly(z, y + 1, r, f);
138+
} else {
139+
res = find_last_knowingly(x + 1, l, y, f);
140+
}
141+
pull(x, z);
142+
return res;
143+
}
144+
145+
int find_last(int x, int l, int r, int ll, int rr, const function<bool(const node&)> &f) {
146+
if (ll <= l && r <= rr) {
147+
if (!f(tree[x])) {
148+
return -1;
149+
}
150+
return find_last_knowingly(x, l, r, f);
151+
}
152+
push(x, l, r);
153+
int y = (l + r) >> 1;
154+
int z = x + ((y - l + 1) << 1);
155+
int res = -1;
156+
if (rr > y) {
157+
res = find_last(z, y + 1, r, ll, rr, f);
158+
}
159+
if (ll <= y && res == -1) {
160+
res = find_last(x + 1, l, y, ll, rr, f);
161+
}
162+
pull(x, z);
163+
return res;
164+
}
165+
166+
segtree(int _n) : n(_n) {
167+
assert(n > 0);
168+
tree.resize(2 * n - 1);
169+
build(0, 0, n - 1);
170+
}
171+
172+
template <typename M>
173+
segtree(const vector<M> &v) {
174+
n = v.size();
175+
assert(n > 0);
176+
tree.resize(2 * n - 1);
177+
build(0, 0, n - 1, v);
178+
}
179+
180+
node get(int ll, int rr) {
181+
assert(0 <= ll && ll <= rr && rr <= n - 1);
182+
return get(0, 0, n - 1, ll, rr);
183+
}
184+
185+
node get(int p) {
186+
assert(0 <= p && p <= n - 1);
187+
return get(0, 0, n - 1, p, p);
188+
}
189+
190+
template <typename... M>
191+
void modify(int ll, int rr, const M&... v) {
192+
assert(0 <= ll && ll <= rr && rr <= n - 1);
193+
modify(0, 0, n - 1, ll, rr, v...);
194+
}
195+
196+
// find_first and find_last call all FALSE elements
197+
// to the left (right) of the sought position exactly once
198+
199+
int find_first(int ll, int rr, const function<bool(const node&)> &f) {
200+
assert(0 <= ll && ll <= rr && rr <= n - 1);
201+
return find_first(0, 0, n - 1, ll, rr, f);
202+
}
203+
204+
int find_last(int ll, int rr, const function<bool(const node&)> &f) {
205+
assert(0 <= ll && ll <= rr && rr <= n - 1);
206+
return find_last(0, 0, n - 1, ll, rr, f);
207+
}
208+
};
209+

0 commit comments

Comments
 (0)