-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcartesian_tree.cpp
41 lines (35 loc) · 1.21 KB
/
cartesian_tree.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
#include "test_utils.hpp"
#include "algo/cartesian_tree.hpp"
#include "algo/y_combinator.hpp"
auto stress_test_cartesian_tree() {
LOOP_FOR_DURATION_OR_RUNS_TRACKED (3s, now, 10000, runs) {
print_time(now, 3s, "stress cartesian tree ({} runs)", runs);
int N = rand_unif<int>(1, 1000);
vector<int> index(N);
iota(begin(index), end(index), 0);
vector<int> arr = index;
shuffle(begin(arr), end(arr), mt);
auto [root, parent, kids] = cartesian_tree(arr);
vector<int> inorder;
y_combinator([&, kids = kids, parent = parent](auto self, int i) -> void {
if (kids[i][0] != -1) {
self(kids[i][0]);
assert(kids[i][0] < i);
assert(parent[kids[i][0]] == i);
assert(arr[i] < arr[kids[i][0]]);
}
inorder.push_back(i);
if (kids[i][1] != -1) {
self(kids[i][1]);
assert(i < kids[i][1]);
assert(parent[kids[i][1]] == i);
assert(arr[i] < arr[kids[i][1]]);
}
})(root);
assert(inorder == index);
}
}
int main() {
RUN_BLOCK(stress_test_cartesian_tree());
return 0;
}