-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparse_table.cpp
224 lines (194 loc) · 7.21 KB
/
sparse_table.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
#include "test_utils.hpp"
#include "struct/sparse_table.hpp"
#include "numeric/modnum.hpp"
void stress_test_sparse_table() {
for (int N = 1; N <= 20; N++) {
vector<int> A(N);
for (int i = 0; i < N; i++) {
A[i] = rand_unif<int>(1, 50) * (1 << rand_unif<int>(0, 5));
}
sparse_table st_gcd(A, [&](int u, int v) { return gcd(u, v); });
for (int a = 0; a < N; a++) {
for (int b = a + 1; b <= N; b++) {
int x = st_gcd.query(a, b);
int X = accumulate(begin(A) + a, begin(A) + b, 0,
[&](int u, int v) { return gcd(u, v); });
assert(x == X);
}
}
}
}
void stress_test_disjoint_sparse_table() {
using num = modnum<998244353>;
for (int N = 1; N <= 20; N++) {
vector<num> A(N);
for (int i = 0; i < N; i++) {
A[i] = 1 << i;
}
disjoint_sparse_table sum(A, std::plus<num>{});
disjoint_sparse_table mul(A, std::multiplies<num>{});
for (int a = 0; a < N; a++) {
for (int b = a + 1; b <= N; b++) {
num x = sum.query(a, b);
num y = mul.query(a, b);
num X = accumulate(begin(A) + a, begin(A) + b, num(0));
num Y = accumulate(begin(A) + a, begin(A) + b, num(1),
std::multiplies<num>{});
assert(x == X && y == Y);
}
}
}
}
void stress_test_sparse_index_table() {
for (int N = 1; N <= 20; N++) {
vector<int> A(N);
for (int i = 0; i < N; i++) {
A[i] = rand_unif<int>(1, 50) * (1 << rand_unif<int>(0, 5));
}
sparse_index_table minrmq(N, [&](int u, int v) {
return make_pair(A[u], u) < make_pair(A[v], v) ? u : v;
});
for (int a = 0; a < N; a++) {
for (int b = a + 1; b <= N; b++) {
int x = minrmq.query(a, b);
int X = min_element(begin(A) + a, begin(A) + b) - begin(A);
assert(x == X);
}
}
}
}
void stress_test_sparse_table_2d() {
for (int N = 1; N <= 20; N++) {
for (int M = 1; M <= 20; M++) {
vector<vector<int>> A(N, vector<int>(M));
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
A[i][j] = rand_unif<int>(1, 50000) * (1 << rand_unif<int>(0, 10));
}
}
sparse_table_2d rmq1(A, [&](int u, int v) { return min(u, v); });
disjoint_sparse_table_2d rmq2(A, [&](int u, int v) { return min(u, v); });
auto get = [&](int il, int ir, int jl, int jr) {
int ans = INT_MAX;
for (int i = il; i < ir; i++) {
for (int j = jl; j < jr; j++) {
ans = min(ans, A[i][j]);
}
}
return ans;
};
for (int a = 0; a < N; a++) {
for (int b = a + 1; b <= N; b++) {
for (int c = 0; c < M; c++) {
for (int d = c + 1; d <= M; d++) {
int x1 = rmq1.query(a, b, c, d);
int x2 = rmq2.query(a, b, c, d);
int X = get(a, b, c, d);
assert(x1 == X);
assert(x2 == X);
}
}
}
}
}
}
}
void stress_test_disjoint_sparse_table_2d() {
for (int N = 1; N <= 20; N++) {
for (int M = 1; M <= 20; M++) {
vector<vector<int>> A(N, vector<int>(M));
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
A[i][j] = rand_unif<int>(1, 500) * (1 << rand_unif<int>(0, 5));
}
}
disjoint_sparse_table_2d rmq(A, std::plus<int>{});
auto get = [&](int il, int ir, int jl, int jr) {
int ans = 0;
for (int i = il; i < ir; i++) {
ans += accumulate(begin(A[i]) + jl, begin(A[i]) + jr, 0);
}
return ans;
};
for (int a = 0; a < N; a++) {
for (int b = a + 1; b <= N; b++) {
for (int c = 0; c < M; c++) {
for (int d = c + 1; d <= M; d++) {
int x = rmq.query(a, b, c, d);
int X = get(a, b, c, d);
assert(x == X);
}
}
}
}
}
}
}
void speed_test_sparse_table_2d() {
vector<int> Ns = {60, 100, 150, 220, 300, 400};
vector<pair<int, int>> inputs;
for (int N : Ns) {
for (int M : Ns) {
if (N * M < 150000) {
inputs.push_back({N, M});
}
}
}
map<tuple<int, int, string>, stringable> times;
for (auto [N, M] : inputs) {
printcl("speed sparse table N={} M={}", N, M);
vector<vector<int>> A(N, vector<int>(M));
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
A[i][j] = rand_unif<int>(1, 50000) * (1 << rand_unif<int>(0, 10));
}
}
START_ACC2(normal_build, disjoint_build);
START_ACC2(normal_query, disjoint_query);
__int128_t ans1 = 0, ans2 = 0;
int64_t Q = 1LL * N * (N - 1) * M * (M - 1) / 4;
ADD_TIME_BLOCK(normal_query) {
START(normal_build);
sparse_table_2d rmq(A, [](int u, int v) { return min(u, v); });
ADD_TIME(normal_build);
for (int a = 0; a < N; a++) {
for (int b = a + 1; b < N; b++) {
for (int c = 0; c < M; c++) {
for (int d = c + 1; d < M; d++) {
ans1 += rmq.query(a, b, c, d);
}
}
}
}
}
ADD_TIME_BLOCK(disjoint_query) {
START(disjoint_build);
disjoint_sparse_table_2d rmq(A, [](int u, int v) { return min(u, v); });
ADD_TIME(disjoint_build);
for (int a = 0; a < N; a++) {
for (int b = a + 1; b < N; b++) {
for (int c = 0; c < M; c++) {
for (int d = c + 1; d < M; d++) {
ans2 += rmq.query(a, b, c, d);
}
}
}
}
}
assert(ans1 == ans2);
times[{N, M, "build-normal"}] = FORMAT_TIME(normal_build);
times[{N, M, "build-disjoint"}] = FORMAT_TIME(disjoint_build);
times[{N, M, "query-normal"}] = FORMAT_EACH(normal_query, Q);
times[{N, M, "query-disjoint"}] = FORMAT_EACH(disjoint_query, Q);
}
print_time_table(times, "2D Sparse Table");
}
int main() {
RUN_BLOCK(stress_test_sparse_table());
RUN_BLOCK(stress_test_disjoint_sparse_table());
RUN_BLOCK(stress_test_sparse_index_table());
RUN_BLOCK(stress_test_sparse_table_2d());
RUN_BLOCK(stress_test_disjoint_sparse_table_2d());
RUN_BLOCK(speed_test_sparse_table_2d());
return 0;
}