/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| 0|def maintain(int x) -> void {
18| 0| node[x].size = node[node[x].l].size + 1 + node[node[x].r].size;
19| 0| node[x].sum = node[node[x].l].sum + node[x].val + node[node[x].r].sum;
20| 0|}
21| |
22| 0|def pushdown(int x) -> void {
23| 0| if (node[x].rev) {
------------------
| Branch (23:7): [True: 0.00%, False: 0.00%]
------------------
24| 0| std::swap(node[x].l, node[x].r);
25| 0| node[node[x].l].rev ^= 1;
26| 0| node[node[x].r].rev ^= 1;
27| 0| node[x].rev = 0;
28| 0| }
29| 0|}
30| |
31| 0|def reverse(int x) -> void {
32| 0| if (x == 0) return;
------------------
| Branch (32:7): [True: 0.00%, False: 0.00%]
------------------
33| 0| node[x].rev ^= 1;
34| 0| pushdown(x);
35| 0|}
36| |
37| 1|def merge(int x, int y) -> int {
38| 1| if (x == 0) return y;
------------------
| Branch (38:7): [True: 100.00%, False: 0.00%]
------------------
39| 0| if (y == 0) return x;
------------------
| Branch (39:7): [True: 0.00%, False: 0.00%]
------------------
40| 0| if (node[x].pri > node[y].pri) {
------------------
| Branch (40:7): [True: 0.00%, False: 0.00%]
------------------
41| 0| pushdown(x);
42| 0| node[x].r = merge(node[x].r, y);
43| 0| maintain(x);
44| 0| return x;
45| 0| } else {
46| 0| pushdown(y);
47| 0| node[y].l = merge(x, node[y].l);
48| 0| maintain(y);
49| 0| return y;
50| 0| }
51| 0|}
52| |
53| 0|def split(int x, int k) -> std::pair<int, int> {
54| 0| if (x == 0) return {0, 0};
------------------
| Branch (54:7): [True: 0.00%, False: 0.00%]
------------------
55| 0| pushdown(x);
56| 0| if (int size = node[node[x].l].size; k <= size) {
------------------
| Branch (56:40): [True: 0.00%, False: 0.00%]
------------------
57| 0| let[a, b] = split(node[x].l, k);
58| 0| node[x].l = b;
59| 0| maintain(x);
60| 0| return {a, x};
61| 0| } else {
62| 0| let[a, b] = split(node[x].r, k - size - 1);
63| 0| node[x].r = a;
64| 0| maintain(x);
65| 0| return {x, b};
66| 0| }
67| 0|}
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| 2| for (int i = 1; i <= n; ++i) {
^1
------------------
| Branch (80:19): [True: 50.00%, False: 50.00%]
------------------
81| 1| node[i].size = 1;
82| 1| node[i].pri = node[i].sum = node[i].val = rd.uw();
83| 1| x = merge(x, i);
84| 1| }
85| 1| if (n == 0) rd.skip(1);
^0
------------------
| Branch (85:7): [True: 0.00%, False: 100.00%]
------------------
86| 1| while (q--) {
------------------
| Branch (86:10): [True: 0.00%, False: 100.00%]
------------------
87| 0| int t = rd.u1();
88| 0| int l = rd.uh();
89| 0| int r = rd.uh();
90| 0| let[a, b] = split(x, l);
91| 0| let[c, d] = split(b, r - l);
92| 0| if (t == 0) {
------------------
| Branch (92:9): [True: 0.00%, False: 0.00%]
------------------
93| 0| reverse(c);
94| 0| }
95| 0| if (t == 1) {
------------------
| Branch (95:9): [True: 0.00%, False: 0.00%]
------------------
96| 0| wt.ud(node[c].sum);
97| 0| }
98| 0| x = merge(a, merge(c, d));
99| 0| }
100| 1| return 0;
101| 1|}