-
Notifications
You must be signed in to change notification settings - Fork 522
/
segment_tree.h
176 lines (156 loc) · 4.49 KB
/
segment_tree.h
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
#include <bits/stdc++.h>
using namespace std;
struct segtree {
struct node {
// initial values for leaves
long long mx = 0;
long long sum = 0;
long long add = 0;
// set initial value for a leave
void initialize(long long v) { mx = v; }
// apply aggregate operation to the node
void apply(int l, int r, long long v) {
mx += v;
sum += (r - l + 1) * v;
add += v;
}
};
// construct a node from its children
static node unite(const node &a, const node &b) {
node res;
res.mx = max(a.mx, b.mx);
res.sum = a.sum + b.sum;
return res;
}
void push(int x, int l, int r) {
int m = (l + r) >> 1;
int y = x + ((m - l + 1) << 1);
if (tree[x].add != 0) {
tree[x + 1].apply(l, m, tree[x].add);
tree[y].apply(m + 1, r, tree[x].add);
tree[x].add = 0;
}
}
void pull(int x, int y) { tree[x] = unite(tree[x + 1], tree[y]); }
int n;
vector<node> tree;
void build(int x, int l, int r) {
if (l == r) {
return;
}
int m = (l + r) >> 1;
int y = x + ((m - l + 1) << 1);
build(x + 1, l, m);
build(y, m + 1, r);
pull(x, y);
}
template <class T>
void build(int x, int l, int r, const vector<T> &v) {
if (l == r) {
tree[x].initialize(v[l]);
return;
}
int m = (l + r) >> 1;
int y = x + ((m - l + 1) << 1);
build(x + 1, l, m, v);
build(y, m + 1, r, v);
pull(x, y);
}
node get(int x, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr) {
return tree[x];
}
int m = (l + r) >> 1;
int y = x + ((m - l + 1) << 1);
push(x, l, r);
node res;
if (rr <= m) {
res = get(x + 1, l, m, ll, rr);
} else {
if (ll > m) {
res = get(y, m + 1, r, ll, rr);
} else {
res = unite(get(x + 1, l, m, ll, rr), get(y, m + 1, r, ll, rr));
}
}
pull(x, y);
return res;
}
template <class T>
void modify(int x, int l, int r, int ll, int rr, const T &v) {
if (ll <= l && r <= rr) {
tree[x].apply(l, r, v);
return;
}
int m = (l + r) >> 1;
int y = x + ((m - l + 1) << 1);
push(x, l, r);
if (ll <= m) {
modify(x + 1, l, m, ll, rr, v);
}
if (rr > m) {
modify(y, m + 1, r, ll, rr, v);
}
pull(x, y);
}
segtree(int _n) : n(_n) {
assert(n > 0);
tree.resize(2 * n - 1);
build(0, 0, n - 1);
}
template <class T>
segtree(const vector<T> &v) {
n = v.size();
assert(n > 0);
tree.resize(2 * n - 1);
build(0, 0, n - 1, v);
}
node get(int ll, int rr) {
assert(0 <= ll && ll <= rr && rr <= n - 1);
return get(0, 0, n - 1, ll, rr);
}
node get(int p) {
assert(0 <= p && p <= n - 1);
return get(0, 0, n - 1, p, p);
}
template <class T>
void modify(int ll, int rr, const T v) {
assert(0 <= ll && ll <= rr && rr <= n - 1);
modify(0, 0, n - 1, ll, rr, v);
}
int find_first(int ll, int rr, const function<bool(const node &)> &f, int x, int l, int r) {
if (ll <= l && r <= rr && !f(tree[x])) {
return -1;
}
if (l == r) {
return l;
}
push(x, l, r);
int m = (l + r) >> 1;
int y = x + ((m - l + 1) << 1);
int res = -1;
if (ll <= m) {
res = find_first(ll, rr, f, x + 1, l, m);
}
if (rr > m && res == -1) {
res = find_first(ll, rr, f, y, m + 1, r);
}
pull(x, y);
return res;
}
// calls all FALSE elements to the left of the sought position exactly once
int find_first(int ll, int rr, const function<bool(const node &)> &f) {
assert(0 <= ll && ll <= rr && rr <= n - 1);
return find_first(ll, rr, f, 0, 0, n - 1);
}
};
// Returns min(p | p<=rr && sum[ll..p]>=sum). If no such p exists, returns -1
int sum_lower_bound(segtree &t, int ll, int rr, long long sum) {
long long sumSoFar = 0;
return t.find_first(ll, rr, [&](const segtree::node &node) {
if (sumSoFar + node.sum >= sum)
return true;
sumSoFar += node.sum;
return false;
});
}