-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplay_segtree.cpp
170 lines (154 loc) · 5.53 KB
/
splay_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
#include "test_utils.hpp"
#include "struct/splay_segtree.hpp"
#include "struct/splay_segnodes.hpp"
#include "numeric/modnum.hpp"
void stress_test_sum_splay_segtree() {
constexpr int N = 200;
LOOP_FOR_DURATION (5s) {
vector<int> arr(N);
splay_segtree<sum_segnode<int>> st(0, N);
LOOP_FOR_DURATION (25ms) {
if (cointoss(0.5)) {
int i = rand_unif<int>(0, N - 1);
int v = rand_unif<int>(-5, 5);
st.update_point(i, v);
arr[i] += v;
}
if (cointoss(0.5)) {
int i = rand_unif<int>(0, N - 1);
int got = st.query_point(i).point_val();
int actual = arr[i];
assert(got == actual);
}
if (cointoss(0.5)) {
auto [L, R] = diff_unif<int>(0, N);
int v = rand_unif<int>(-5, 5);
st.update_range(L, R, v);
for (int i = L; i < R; i++) {
arr[i] += v;
}
}
if (cointoss(0.5)) {
auto [L, R] = diff_unif<int>(0, N);
int got = st.query_range(L, R).range_sum();
int actual = accumulate(begin(arr) + L, begin(arr) + R, 0);
assert(got == actual);
}
if (cointoss(0.5)) {
int v = rand_unif<int>(-5, 5);
st.update_all(v);
for (int i = 0; i < N; i++) {
arr[i] += v;
}
}
if (cointoss(0.5)) {
int got = st.query_all().range_sum();
int actual = accumulate(begin(arr), end(arr), 0);
assert(got == actual);
}
}
}
}
void stress_test_sum_affine_splay_segtree() {
constexpr int N = 200;
using num = modnum<998244353>;
LOOP_FOR_DURATION (5s) {
vector<num> arr(N);
splay_segtree<sum_affine_segnode<num>> st(0, N);
LOOP_FOR_DURATION (25ms) {
if (cointoss(0.5)) {
auto [L, R] = diff_unif<int>(0, N);
num b = rand_unif<int>(-1000, 1000);
num c = rand_unif<int>(-1000, 1000);
st.update_range(L, R, make_pair(b, c));
for (int i = L; i < R; i++) {
arr[i] = b * arr[i] + c;
}
}
if (cointoss(0.5)) {
auto [L, R] = diff_unif<int>(0, N);
num got = st.query_range(L, R).range_sum();
num actual = accumulate(begin(arr) + L, begin(arr) + R, num(0));
assert(got == actual);
}
if (cointoss(0.5)) {
num b = rand_unif<int>(-1000, 1000);
num c = rand_unif<int>(-1000, 1000);
st.update_all(make_pair(b, c));
for (int i = 0; i < N; i++) {
arr[i] = b * arr[i] + c;
}
}
if (cointoss(0.5)) {
num got = st.query_all().range_sum();
num actual = accumulate(begin(arr), end(arr), num(0));
assert(got == actual);
}
}
}
}
void stress_test_min_splay_segtree() {
constexpr int N = 200;
LOOP_FOR_DURATION (5s) {
vector<int> arr(N, 0);
splay_segtree<min_segnode<int>> st(0, N, min_segnode<int>(0));
LOOP_FOR_DURATION (100ms) {
if (cointoss(0.5)) {
auto [L, R] = diff_unif<int>(0, N);
int v = rand_unif<int>(-50, 50);
st.update_range(L, R, v);
for (int i = L; i < R; i++) {
arr[i] += v;
}
}
if (cointoss(0.5)) {
auto [L, R] = diff_unif<int>(0, N);
int got = st.query_range(L, R).range_min();
int actual = *min_element(begin(arr) + L, begin(arr) + R);
assert(got == actual);
}
}
}
}
void stress_test_affine_splay_segtree() {
constexpr int N = 200;
using num = modnum<998244353>;
using Data = affine_segnode<num>::Data;
LOOP_FOR_DURATION (5s) {
vector<Data> arr(N, {1, 0});
splay_segtree<affine_segnode<num>> st(0, N);
LOOP_FOR_DURATION_TRACKED_RUNS (100ms, now, runs) {
if (cointoss(0.5)) {
int i = rand_unif<int>(0, N - 1);
num b = rand_unif<int>(-1000, 1000);
num c = rand_unif<int>(-1000, 1000);
st.update_point(i, Data{b, c});
arr[i] = {b, c};
}
if (cointoss(0.5)) {
auto [L, R] = diff_unif<int>(0, N);
auto got = st.query_range(L, R);
num x = rand_unif<int>(-100, 100);
auto got_lmr = got.eval_lmr(x);
auto got_rml = got.eval_rml(x);
num expected_lmr = x;
num expected_rml = x;
for (int i = L; i < R; i++) {
expected_rml = arr[i][0] * expected_rml + arr[i][1];
}
for (int i = R - 1; i >= L; i--) {
expected_lmr = arr[i][0] * expected_lmr + arr[i][1];
}
assert(got_lmr == expected_lmr);
assert(got_rml == expected_rml);
}
}
}
}
int main() {
RUN_BLOCK(stress_test_sum_splay_segtree());
RUN_BLOCK(stress_test_sum_affine_splay_segtree());
RUN_BLOCK(stress_test_min_splay_segtree());
RUN_BLOCK(stress_test_affine_splay_segtree());
return 0;
}