-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_segtree.cpp
257 lines (225 loc) · 7.93 KB
/
dynamic_segtree.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "test_utils.hpp"
#include "struct/dynamic_segtree.hpp"
#include "struct/dynamic_2dsegtree.hpp"
#include "struct/segtree_nodes.hpp"
void stress_test_dynamic_segtree() {
// make two lazy arrays of size SMALL, arr[] and brr[]
// merge them as arr,brr,brr,arr (size 4*SMALL)
// arr is array-initialized, brr is sparse initialized
// then add levels on top, B total, size of 4*SMALL*(1<<B)
constexpr int B = 5, SMALL = 40;
constexpr int N = 4 * SMALL * (1 << B);
constexpr int L = 0, R = N;
vector<int> arr(R, 0);
for (int i = 0; i < SMALL; i++)
arr[i] = arr[i + 3 * SMALL] = rand_unif<int>(-10, 10);
for (int i = 4 * SMALL; i < R; i++)
arr[i] = arr[i - 4 * SMALL];
dynamic_segtree<sum_segnode> st;
int root_a = st.build_array(SMALL, arr, true);
int root_b = st.build_sparse(0, true);
int root_c = st.build_concat({root_a, root_b, root_b, root_a}, true);
int root = st.build_levels(root_c, B, false);
print("initial number of nodes: {}\n", st.num_nodes());
LOOP_FOR_DURATION (2s) {
if (cointoss(0.5)) {
int i = rand_unif<int>(0, N - 1);
int v = rand_unif<int>(-10, 10);
st.update_point(root, L, R, i, v);
arr[i] += v;
}
if (cointoss(0.5)) {
auto [qL, qR] = ordered_unif<int>(0, N);
int v = rand_unif<int>(-10, 10);
st.update_range(root, L, R, qL, qR, v);
for (int i = qL; i < qR; i++) {
arr[i] += v;
}
}
if (cointoss(0.5)) {
auto [qL, qR] = ordered_unif<int>(0, N);
long got = st.query_range(root, L, R, qL, qR).value;
long actual = accumulate(begin(arr) + qL, begin(arr) + qR, 0LL);
assert(got == actual);
}
}
print("final number of nodes: {}\n", st.num_nodes());
print(" normal segtree: {}\n", 2 * (R - L));
}
void stress_test_maxsubrange_segtree() {
constexpr int N = 200;
using V = maxsubrange_segnode::V;
vector<V> arr(N, 0);
dynamic_segtree<maxsubrange_segnode> st;
int root = st.build_sparse({0}, false);
LOOP_FOR_DURATION_TRACKED_RUNS (1s, now, runs) {
if (cointoss(0.5)) {
int i = rand_unif<int>(0, N - 1);
int v = rand_unif<int>(-50, 50);
st.update_point(root, 0, N, i, v);
arr[i] += v;
}
if (cointoss(0.5)) {
auto [L, R] = ordered_unif<int>(0, N);
auto got = st.query_range(root, 0, N, L, R);
auto actual = maxsubrange_segnode::MIN;
for (int i = L; i < R; i++) {
auto sum = arr[i];
actual = max(actual, sum);
for (int j = i + 1; j < R; j++) {
sum += arr[j];
actual = max(actual, sum);
}
}
assert(got == actual);
}
}
}
void stress_test_affine_segtree() {
constexpr int N = 200;
using num = modnum<998244353>;
using Data = affine_segnode::Data;
vector<Data> arr(N, {1, 0});
dynamic_segtree<affine_segnode> st;
int root = st.build_sparse(Data{1, 0}, false);
LOOP_FOR_DURATION_TRACKED_RUNS (1s, now, runs) {
if (cointoss(0.5)) {
int i = rand_unif<int>(0, N - 1);
num b = rand_unif<int>(0, 500);
num c = rand_unif<int>(-1000, 1000);
st.update_point(root, 0, N, i, Data{b, c});
arr[i] = {b, c};
}
if (cointoss(0.5)) {
auto [L, R] = ordered_unif<int>(0, N);
auto got = st.query_range(root, 0, N, L, R);
num x = rand_unif<int>(1, 73);
auto got_lmr = got.eval_lmr(x);
auto got_rml = got.eval_rml(x);
num actual_lmr = x;
num actual_rml = x;
for (int i = L; i < R; i++) {
actual_rml = arr[i][0] * actual_rml + arr[i][1];
}
for (int i = R - 1; i >= L; i--) {
actual_lmr = arr[i][0] * actual_lmr + arr[i][1];
}
assert(got_lmr == actual_lmr);
assert(got_rml == actual_rml);
}
}
}
void stress_test_gcd_segtree() {
constexpr int N = 200;
vector<int> arr(N);
dynamic_segtree<gcd_segnode> st;
int root = st.build_sparse({0}, false);
LOOP_FOR_DURATION (1s) {
if (cointoss(0.5)) {
auto [L, R] = ordered_unif<int>(0, N);
int v = rand_unif<int>(-10, 10);
st.update_range(root, 0, N, L, R, v);
for (int i = L; i < R; i++) {
arr[i] += v;
}
}
if (cointoss(0.5)) {
auto [L, R] = ordered_unif<int>(0, N);
int got = st.query_range(root, 0, N, L, R);
int actual = 0;
for (int i = L; i < R; i++) {
actual = gcd(actual, arr[i]);
}
assert(got == actual);
}
}
}
void stress_test_maxcount_segtree() {
constexpr int N = 200;
using V = maxcount_segnode::V;
vector<V> arr(N, 0);
dynamic_segtree<maxcount_segnode> st;
int root = st.build_array(N, arr, false);
LOOP_FOR_DURATION_TRACKED_RUNS (1s, now, runs) {
if (cointoss(0.5)) {
auto [L, R] = ordered_unif<int>(0, N);
int v = rand_unif<int>(-50, 50);
st.update_range(root, 0, N, L, R, v);
for (int i = L; i < R; i++) {
arr[i] += v;
}
}
if (cointoss(0.5)) {
auto [L, R] = ordered_unif<int>(0, N);
auto ans = st.query_range(root, 0, N, L, R);
auto got_max = ans.value;
auto got_cnt = ans.count;
auto actual_max = maxcount_segnode::MIN;
int actual_cnt = 0;
for (int i = L; i < R; i++) {
if (arr[i] > actual_max) {
actual_max = arr[i];
actual_cnt = 1;
} else if (arr[i] == actual_max) {
actual_cnt++;
}
}
assert(got_max == actual_max && got_cnt == actual_cnt);
}
}
}
void speed_test_affine_dynamic_segtree() {
constexpr int N = 500'000;
using num = modnum<998244353>;
using Data = affine_segnode::Data;
dynamic_segtree<affine_segnode> st;
int root = st.build_sparse(Data{1, 0}, false);
LOOP_FOR_DURATION_TRACKED_RUNS (3s, now, runs) {
if (cointoss(0.5)) {
int i = rand_unif<int>(0, N - 1);
num b = rand_unif<int>(0, 500);
num c = rand_unif<int>(-1000, 1000);
st.update_point(root, 0, N, i, Data{b, c});
}
if (cointoss(0.5)) {
auto [L, R] = ordered_unif<int>(0, N);
auto got = st.query_range(root, 0, N, L, R);
num x = rand_unif<int>(1, 73);
got.eval_lmr(x);
got.eval_rml(x);
}
}
println("runs: {}\n", runs);
}
void speed_test_dynamic_segtree() {
constexpr int N = 500'000;
dynamic_segtree<sum_segnode> st;
int root = st.build_sparse({0}, false);
LOOP_FOR_DURATION_TRACKED_RUNS (3s, now, runs) {
if (cointoss(0.5)) {
int i = rand_unif<int>(0, N - 1);
int v = rand_unif<int>(0, 100);
st.update_point(root, 0, N, i, v);
}
if (cointoss(0.5)) {
auto [L, R] = ordered_unif<int>(0, N);
int v = rand_unif<int>(0, 10);
st.update_range(root, 0, N, L, R, v);
}
if (cointoss(0.5)) {
auto [L, R] = ordered_unif<int>(0, N);
st.query_range(root, 0, N, L, R);
}
}
printcl("runs: {}\n", runs);
}
int main() {
RUN_BLOCK(stress_test_dynamic_segtree());
RUN_BLOCK(stress_test_maxsubrange_segtree());
RUN_BLOCK(stress_test_affine_segtree());
RUN_BLOCK(stress_test_gcd_segtree());
RUN_BLOCK(stress_test_maxcount_segtree());
RUN_BLOCK(speed_test_affine_dynamic_segtree());
RUN_BLOCK(speed_test_dynamic_segtree());
return 0;
}