/tmp/solutions/build/range_reverse_range_sum-slow.cpp:
1| |#include <common.h>
2| |prelude;
3| |
4| |namespace {
5| |
6| |constexpr int N = 200001;
7| |
8| |struct Node {
9| | int pri;
10| | int val;
11| | u64 sum;
12| | int rev;
13| | int size;
14| | int l, r;
15| |} node[N];
16| |
17| 32|def maintain(int x) -> void {
18| 32| node[x].size = node[node[x].l].size + 1 + node[node[x].r].size;
19| 32| node[x].sum = node[node[x].l].sum + node[x].val + node[node[x].r].sum;
20| 32|}
21| |
22| 33|def pushdown(int x) -> void {
23| 33| if (node[x].rev) {
------------------
| Branch (23:7): [True: 6.06%, False: 93.94%]
------------------
24| 2| std::swap(node[x].l, node[x].r);
25| 2| node[node[x].l].rev ^= 1;
26| 2| node[node[x].r].rev ^= 1;
27| 2| node[x].rev = 0;
28| 2| }
29| 33|}
30| |
31| 2|def reverse(int x) -> void {
32| 2| if (x == 0) return;
^1
------------------
| Branch (32:7): [True: 50.00%, False: 50.00%]
------------------
33| 1| node[x].rev ^= 1;
34| 1| pushdown(x);
35| 1|}
36| |
37| 23|def merge(int x, int y) -> int {
38| 23| if (x == 0) return y;
^6
------------------
| Branch (38:7): [True: 26.09%, False: 73.91%]
------------------
39| 17| if (y == 0) return x;
^8
------------------
| Branch (39:7): [True: 47.06%, False: 52.94%]
------------------
40| 9| if (node[x].pri > node[y].pri) {
------------------
| Branch (40:7): [True: 11.11%, False: 88.89%]
------------------
41| 1| pushdown(x);
42| 1| node[x].r = merge(node[x].r, y);
43| 1| maintain(x);
44| 1| return x;
45| 8| } else {
46| 8| pushdown(y);
47| 8| node[y].l = merge(x, node[y].l);
48| 8| maintain(y);
49| 8| return y;
50| 8| }
51| 9|}
52| |
53| 33|def split(int x, int k) -> std::pair<int, int> {
54| 33| if (x == 0) return {0, 0};
^10
------------------
| Branch (54:7): [True: 30.30%, False: 69.70%]
------------------
55| 23| pushdown(x);
56| 23| if (int size = node[node[x].l].size; k <= size) {
------------------
| Branch (56:40): [True: 69.57%, False: 30.43%]
------------------
57| 16| let[a, b] = split(node[x].l, k);
58| 16| node[x].l = b;
59| 16| maintain(x);
60| 16| return {a, x};
61| 16| } else {
62| 7| let[a, b] = split(node[x].r, k - size - 1);
63| 7| node[x].r = a;
64| 7| maintain(x);
65| 7| return {x, b};
66| 7| }
67| 23|}
68| |
69| |} // namespace
70| |
71| 1|int main() {
72| 1| rd rd;
73| 1| wt wt;
74| 1| int n = rd.uh();
75| 1| int q = rd.uh();
76| 1|#ifdef LOCAL
77| 1| std::memset(node, 0, sizeof(Node) * (n + 1));
78| 1|#endif
79| 1| int x = 0;
80| 5| for (int i = 1; i <= n; ++i) {
^4
------------------
| Branch (80:19): [True: 80.00%, False: 20.00%]
------------------
81| 4| node[i].size = 1;
82| 4| node[i].pri = node[i].sum = node[i].val = rd.uw();
83| 4| x = merge(x, i);
84| 4| }
85| 1| if (n == 0) rd.skip(1);
^0
------------------
| Branch (85:7): [True: 0.00%, False: 100.00%]
------------------
86| 6| while (q--) {
------------------
| Branch (86:10): [True: 83.33%, False: 16.67%]
------------------
87| 5| int t = rd.u1();
88| 5| int l = rd.uh();
89| 5| int r = rd.uh();
90| 5| let[a, b] = split(x, l);
91| 5| let[c, d] = split(b, r - l);
92| 5| if (t == 0) {
------------------
| Branch (92:9): [True: 40.00%, False: 60.00%]
------------------
93| 2| reverse(c);
94| 2| }
95| 5| if (t == 1) {
------------------
| Branch (95:9): [True: 60.00%, False: 40.00%]
------------------
96| 3| wt.ud(node[c].sum);
97| 3| }
98| 5| x = merge(a, merge(c, d));
99| 5| }
100| 1| return 0;
101| 1|}