/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.0M|def maintain(int x) -> void {
20| 18.0M| node[x].size = node[node[x].l].size + 1 + node[node[x].r].size;
21| 18.0M| node[x].sum = (node[node[x].l].sum + node[x].val + node[node[x].r].sum) % P;
22| 18.0M|}
23| |
24| 779k|def build(int l, int r) -> int {
25| 779k| if (l > r) return 0;
^389k
------------------
| Branch (25:7): [True: 50.00%, False: 50.00%]
------------------
26| 389k| int m = (l + r) / 2;
27| 389k| node[m].l = build(l, m - 1);
28| 389k| node[m].r = build(m + 1, r);
29| 389k| maintain(m);
30| 389k| return m;
31| 779k|}
32| |
33| 82.1k|def reverse(int x) -> void { node[x].rev ^= 1; }
34| |
35| 23.7M|def affine(int x, u64 a, u64 b) -> void {
36| 23.7M| if (x == 0) return;
^1.89M
------------------
| Branch (36:7): [True: 7.97%, False: 92.03%]
------------------
37| 21.8M| node[x].a = (a * node[x].a) % P;
38| 21.8M| node[x].b = (a * node[x].b + b) % P;
39| 21.8M| node[x].val = (a * node[x].val + b) % P;
40| 21.8M| node[x].sum = (a * node[x].sum + b * node[x].size) % P;
41| 21.8M|}
42| |
43| 18.4M|def pushdown(int x) -> void {
44| 18.4M| if (node[x].rev) {
------------------
| Branch (44:7): [True: 34.60%, False: 65.40%]
------------------
45| 6.39M| std::swap(node[x].l, node[x].r);
46| 6.39M| node[node[x].l].rev ^= 1;
47| 6.39M| node[node[x].r].rev ^= 1;
48| 6.39M| node[x].rev = 0;
49| 6.39M| }
50| 18.4M| if ((node[x].a != 1) | (node[x].b != 0)) {
------------------
| Branch (50:7): [True: 63.96%, False: 36.04%]
------------------
51| 11.8M| affine(node[x].l, node[x].a, node[x].b);
52| 11.8M| affine(node[x].r, node[x].a, node[x].b);
53| 11.8M| node[x].a = 1;
54| 11.8M| node[x].b = 0;
55| 11.8M| }
56| 18.4M|}
57| |
58| 9.39M|def zig(int &x) -> void {
59| 9.39M| int l = node[x].l;
60| 9.39M| node[x].l = node[l].r;
61| 9.39M| maintain(x);
62| 9.39M| node[l].r = x;
63| 9.39M| x = l;
64| 9.39M|}
65| |
66| 8.26M|def zag(int &x) -> void {
67| 8.26M| int r = node[x].r;
68| 8.26M| node[x].r = node[r].l;
69| 8.26M| maintain(x);
70| 8.26M| node[r].l = x;
71| 8.26M| x = r;
72| 8.26M|}
73| |
74| 9.44M|def splay(int &x, int k) -> void {
75| 9.44M| pushdown(x);
76| 9.44M| if (int &l = node[x].l, &r = node[x].r, size = node[l].size; k == size) {
------------------
| Branch (76:64): [True: 4.35%, False: 95.65%]
------------------
77| 410k| return;
78| 9.03M| } else if (k < size) {
------------------
| Branch (78:14): [True: 53.23%, False: 46.77%]
------------------
79| 4.80M| pushdown(l);
80| 4.80M| if (int &ll = node[l].l, &lr = node[l].r, size = node[ll].size; k == size) {
------------------
| Branch (80:69): [True: 4.91%, False: 95.09%]
------------------
81| 236k| zig(x);
82| 4.57M| } else if (k < size) {
------------------
| Branch (82:16): [True: 58.06%, False: 41.94%]
------------------
83| 2.65M| splay(ll, k);
84| 2.65M| zig(x);
85| 2.65M| zig(x);
86| 2.65M| } else {
87| 1.91M| splay(lr, k - size - 1);
88| 1.91M| zag(l);
89| 1.91M| zig(x);
90| 1.91M| }
91| 4.80M| } else {
92| 4.22M| pushdown(r);
93| 4.22M| k -= size + 1;
94| 4.22M| if (int &rl = node[r].l, &rr = node[r].r, size = node[rl].size; k == size) {
------------------
| Branch (94:69): [True: 4.14%, False: 95.86%]
------------------
95| 174k| zag(x);
96| 4.05M| } else if (k < size) {
------------------
| Branch (96:16): [True: 47.67%, False: 52.33%]
------------------
97| 1.93M| splay(rl, k);
98| 1.93M| zig(r);
99| 1.93M| zag(x);
100| 2.11M| } else {
101| 2.11M| splay(rr, k - size - 1);
102| 2.11M| zag(x);
103| 2.11M| zag(x);
104| 2.11M| }
105| 4.22M| }
106| 9.44M|}
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| 800k| for (int i = 1; i <= n + q + 1; ++i) node[i] = Node{.a = 1, .size = 1};
^800k^800k
------------------
| Branch (115:19): [True: 100.00%, False: 0.00%]
------------------
116| 389k| for (int i = 2; i <= n + 1; ++i) node[i].val = rd.uw();
^389k^389k
------------------
| Branch (116:19): [True: 100.00%, False: 0.00%]
------------------
117| 1| int x = build(1, n += 2);
118| 410k| while (q--) {
------------------
| Branch (118:10): [True: 100.00%, False: 0.00%]
------------------
119| 410k| let t = rd.u1();
120| 410k| if (t == 0) {
------------------
| Branch (120:9): [True: 19.89%, False: 80.11%]
------------------
121| 81.7k| ++n;
122| 81.7k| int k = rd.uh();
123| 81.7k| node[n].val = node[n].sum = rd.uw();
124| 81.7k| splay(x, k);
125| 81.7k| splay(node[x].r, 0);
126| 81.7k| node[node[x].r].l = n;
127| 81.7k| }
128| 410k| if (t == 1) {
------------------
| Branch (128:9): [True: 20.09%, False: 79.91%]
------------------
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| 410k| if (t == 2) {
------------------
| Branch (134:9): [True: 20.00%, False: 80.00%]
------------------
135| 82.1k| int l = rd.uh();
136| 82.1k| int r = rd.uh();
137| 82.1k| splay(x, l);
138| 82.1k| splay(node[x].r, r - l);
139| 82.1k| reverse(node[node[x].r].l);
140| 82.1k| }
141| 410k| if (t == 3) {
------------------
| Branch (141:9): [True: 20.01%, False: 79.99%]
------------------
142| 82.2k| int l = rd.uh();
143| 82.2k| int r = rd.uh();
144| 82.2k| splay(x, l);
145| 82.2k| splay(node[x].r, r - l);
146| 82.2k| int a = rd.uw();
147| 82.2k| int b = rd.uw();
148| 82.2k| affine(node[node[x].r].l, a, b);
149| 82.2k| }
150| 410k| if (t == 4) {
------------------
| Branch (150:9): [True: 20.02%, False: 79.98%]
------------------
151| 82.2k| int l = rd.uh();
152| 82.2k| int r = rd.uh();
153| 82.2k| splay(x, l);
154| 82.2k| splay(node[x].r, r - l);
155| 82.2k| wt.ud(node[node[node[x].r].l].sum);
156| 82.2k| }
157| 410k| }
158| 1| return 0;
159| 1|}