/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| 180k|def maintain(int x) -> void {
18| 180k| node[x].size = node[node[x].l].size + 1 + node[node[x].r].size;
19| 180k| node[x].sum = node[node[x].l].sum + node[x].val + node[node[x].r].sum;
20| 180k|}
21| |
22| 197k|def pushdown(int x) -> void {
23| 197k| if (node[x].rev) {
------------------
| Branch (23:7): [True: 8.95%, False: 91.05%]
------------------
24| 17.6k| std::swap(node[x].l, node[x].r);
25| 17.6k| node[node[x].l].rev ^= 1;
26| 17.6k| node[node[x].r].rev ^= 1;
27| 17.6k| node[x].rev = 0;
28| 17.6k| }
29| 197k|}
30| |
31| 54.0k|def reverse(int x) -> void {
32| 54.0k| if (x == 0) return;
^36.3k
------------------
| Branch (32:7): [True: 67.24%, False: 32.76%]
------------------
33| 17.6k| node[x].rev ^= 1;
34| 17.6k| pushdown(x);
35| 17.6k|}
36| |
37| 216k|def merge(int x, int y) -> int {
38| 216k| if (x == 0) return y;
^144k
------------------
| Branch (38:7): [True: 66.74%, False: 33.26%]
------------------
39| 71.9k| if (y == 0) return x;
------------------
| Branch (39:7): [True: 100.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| 396k|def split(int x, int k) -> std::pair<int, int> {
54| 396k| if (x == 0) return {0, 0};
^216k
------------------
| Branch (54:7): [True: 54.57%, False: 45.43%]
------------------
55| 180k| pushdown(x);
56| 180k| if (int size = node[node[x].l].size; k <= size) {
------------------
| Branch (56:40): [True: 60.05%, False: 39.95%]
------------------
57| 108k| let[a, b] = split(node[x].l, k);
58| 108k| node[x].l = b;
59| 108k| maintain(x);
60| 108k| return {a, x};
61| 108k| } else {
62| 71.9k| let[a, b] = split(node[x].r, k - size - 1);
63| 71.9k| node[x].r = a;
64| 71.9k| maintain(x);
65| 71.9k| return {x, b};
66| 71.9k| }
67| 180k|}
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| 108k| while (q--) {
------------------
| Branch (86:10): [True: 100.00%, False: 0.00%]
------------------
87| 108k| int t = rd.u1();
88| 108k| int l = rd.uh();
89| 108k| int r = rd.uh();
90| 108k| let[a, b] = split(x, l);
91| 108k| let[c, d] = split(b, r - l);
92| 108k| if (t == 0) {
------------------
| Branch (92:9): [True: 49.95%, False: 50.05%]
------------------
93| 54.0k| reverse(c);
94| 54.0k| }
95| 108k| if (t == 1) {
------------------
| Branch (95:9): [True: 50.05%, False: 49.95%]
------------------
96| 54.1k| wt.ud(node[c].sum);
97| 54.1k| }
98| 108k| x = merge(a, merge(c, d));
99| 108k| }
100| 1| return 0;
101| 1|}