/tmp/solutions/build/dynamic_sequence_range_affine_range_sum-main.cpp:
1| |#include <common.h>
2| |prelude;
3| |
4| |namespace {
5| |
6| |constexpr int N = 1000003;
7| |constexpr u32 P = 998244353;
8| |
9| |struct Node {
10| | u32 val;
11| | u32 sum;
12| | u32 a;
13| | u32 b;
14| | int rev;
15| | int size;
16| | int l, r;
17| |} node[N];
18| |
19| 18.4M|def maintain(int x) -> void {
20| 18.4M| node[x].size = node[node[x].l].size + 1 + node[node[x].r].size;
21| 18.4M| node[x].sum = (node[node[x].l].sum + node[x].val + node[node[x].r].sum) % P;
22| 18.4M|}
23| |
24| 926k|def build(int l, int r) -> int {
25| 926k| if (l > r) return 0;
^463k
------------------
| Branch (25:7): [True: 50.00%, False: 50.00%]
------------------
26| 463k| int m = (l + r) / 2;
27| 463k| node[m].l = build(l, m - 1);
28| 463k| node[m].r = build(m + 1, r);
29| 463k| maintain(m);
30| 463k| return m;
31| 926k|}
32| |
33| 82.5k|def reverse(int x) -> void { node[x].rev ^= 1; }
34| |
35| 24.2M|def affine(int x, u64 a, u64 b) -> void {
36| 24.2M| if (x == 0) return;
^1.86M
------------------
| Branch (36:7): [True: 7.66%, False: 92.34%]
------------------
37| 22.4M| node[x].a = (a * node[x].a) % P;
38| 22.4M| node[x].b = (a * node[x].b + b) % P;
39| 22.4M| node[x].val = (a * node[x].val + b) % P;
40| 22.4M| node[x].sum = (a * node[x].sum + b * node[x].size) % P;
41| 22.4M|}
42| |
43| 18.8M|def pushdown(int x) -> void {
44| 18.8M| if (node[x].rev) {
------------------
| Branch (44:7): [True: 34.73%, False: 65.27%]
------------------
45| 6.53M| std::swap(node[x].l, node[x].r);
46| 6.53M| node[node[x].l].rev ^= 1;
47| 6.53M| node[node[x].r].rev ^= 1;
48| 6.53M| node[x].rev = 0;
49| 6.53M| }
50| 18.8M| if ((node[x].a != 1) | (node[x].b != 0)) {
------------------
| Branch (50:7): [True: 64.32%, False: 35.68%]
------------------
51| 12.1M| affine(node[x].l, node[x].a, node[x].b);
52| 12.1M| affine(node[x].r, node[x].a, node[x].b);
53| 12.1M| node[x].a = 1;
54| 12.1M| node[x].b = 0;
55| 12.1M| }
56| 18.8M|}
57| |
58| 9.57M|def zig(int &x) -> void {
59| 9.57M| int l = node[x].l;
60| 9.57M| node[x].l = node[l].r;
61| 9.57M| maintain(x);
62| 9.57M| node[l].r = x;
63| 9.57M| x = l;
64| 9.57M|}
65| |
66| 8.41M|def zag(int &x) -> void {
67| 8.41M| int r = node[x].r;
68| 8.41M| node[x].r = node[r].l;
69| 8.41M| maintain(x);
70| 8.41M| node[r].l = x;
71| 8.41M| x = r;
72| 8.41M|}
73| |
74| 9.61M|def splay(int &x, int k) -> void {
75| 9.61M| pushdown(x);
76| 9.61M| if (int &l = node[x].l, &r = node[x].r, size = node[l].size; k == size) {
------------------
| Branch (76:64): [True: 4.29%, False: 95.71%]
------------------
77| 412k| return;
78| 9.20M| } else if (k < size) {
------------------
| Branch (78:14): [True: 53.26%, False: 46.74%]
------------------
79| 4.90M| pushdown(l);
80| 4.90M| if (int &ll = node[l].l, &lr = node[l].r, size = node[ll].size; k == size) {
------------------
| Branch (80:69): [True: 4.86%, False: 95.14%]
------------------
81| 238k| zig(x);
82| 4.66M| } else if (k < size) {
------------------
| Branch (82:16): [True: 57.99%, False: 42.01%]
------------------
83| 2.70M| splay(ll, k);
84| 2.70M| zig(x);
85| 2.70M| zig(x);
86| 2.70M| } else {
87| 1.95M| splay(lr, k - size - 1);
88| 1.95M| zag(l);
89| 1.95M| zig(x);
90| 1.95M| }
91| 4.90M| } else {
92| 4.30M| pushdown(r);
93| 4.30M| k -= size + 1;
94| 4.30M| if (int &rl = node[r].l, &rr = node[r].r, size = node[rl].size; k == size) {
------------------
| Branch (94:69): [True: 4.06%, False: 95.94%]
------------------
95| 174k| zag(x);
96| 4.12M| } else if (k < size) {
------------------
| Branch (96:16): [True: 47.71%, False: 52.29%]
------------------
97| 1.96M| splay(rl, k);
98| 1.96M| zig(r);
99| 1.96M| zag(x);
100| 2.15M| } else {
101| 2.15M| splay(rr, k - size - 1);
102| 2.15M| zag(x);
103| 2.15M| zag(x);
104| 2.15M| }
105| 4.30M| }
106| 9.61M|}
107| |
108| |} // namespace
109| |
110| 1|int main() {
111| 1| rd rd;
112| 1| wt wt;
113| 1| int n = rd.uh();
114| 1| int q = rd.uh();
115| 875k| for (int i = 1; i <= n + q + 1; ++i) node[i] = Node{.a = 1, .size = 1};
^875k^875k
------------------
| Branch (115:19): [True: 100.00%, False: 0.00%]
------------------
116| 463k| for (int i = 2; i <= n + 1; ++i) node[i].val = rd.uw();
^463k^463k
------------------
| Branch (116:19): [True: 100.00%, False: 0.00%]
------------------
117| 1| int x = build(1, n += 2);
118| 412k| while (q--) {
------------------
| Branch (118:10): [True: 100.00%, False: 0.00%]
------------------
119| 412k| let t = rd.u1();
120| 412k| if (t == 0) {
------------------
| Branch (120:9): [True: 19.98%, False: 80.02%]
------------------
121| 82.5k| ++n;
122| 82.5k| int k = rd.uh();
123| 82.5k| node[n].val = node[n].sum = rd.uw();
124| 82.5k| splay(x, k);
125| 82.5k| splay(node[x].r, 0);
126| 82.5k| node[node[x].r].l = n;
127| 82.5k| }
128| 412k| if (t == 1) {
------------------
| Branch (128:9): [True: 19.99%, False: 80.01%]
------------------
129| 82.5k| int k = rd.uh();
130| 82.5k| splay(x, k);
131| 82.5k| splay(node[x].r, 1);
132| 82.5k| node[node[x].r].l = 0;
133| 82.5k| }
134| 412k| if (t == 2) {
------------------
| Branch (134:9): [True: 19.99%, False: 80.01%]
------------------
135| 82.5k| int l = rd.uh();
136| 82.5k| int r = rd.uh();
137| 82.5k| splay(x, l);
138| 82.5k| splay(node[x].r, r - l);
139| 82.5k| reverse(node[node[x].r].l);
140| 82.5k| }
141| 412k| if (t == 3) {
------------------
| Branch (141:9): [True: 20.06%, False: 79.94%]
------------------
142| 82.8k| int l = rd.uh();
143| 82.8k| int r = rd.uh();
144| 82.8k| splay(x, l);
145| 82.8k| splay(node[x].r, r - l);
146| 82.8k| int a = rd.uw();
147| 82.8k| int b = rd.uw();
148| 82.8k| affine(node[node[x].r].l, a, b);
149| 82.8k| }
150| 412k| if (t == 4) {
------------------
| Branch (150:9): [True: 19.98%, False: 80.02%]
------------------
151| 82.5k| int l = rd.uh();
152| 82.5k| int r = rd.uh();
153| 82.5k| splay(x, l);
154| 82.5k| splay(node[x].r, r - l);
155| 82.5k| wt.ud(node[node[node[x].r].l].sum);
156| 82.5k| }
157| 412k| }
158| 1| return 0;
159| 1|}