/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| 1.50M|def maintain(int x) -> void {
20| 1.50M| node[x].size = node[node[x].l].size + 1 + node[node[x].r].size;
21| 1.50M| node[x].sum = (node[node[x].l].sum + node[x].val + node[node[x].r].sum) % P;
22| 1.50M|}
23| |
24| 858k|def build(int l, int r) -> int {
25| 858k| if (l > r) return 0;
^429k
------------------
| Branch (25:7): [True: 50.00%, False: 50.00%]
------------------
26| 429k| int m = (l + r) / 2;
27| 429k| node[m].l = build(l, m - 1);
28| 429k| node[m].r = build(m + 1, r);
29| 429k| maintain(m);
30| 429k| return m;
31| 858k|}
32| |
33| 5.12k|def reverse(int x) -> void { node[x].rev ^= 1; }
34| |
35| 1.43M|def affine(int x, u64 a, u64 b) -> void {
36| 1.43M| if (x == 0) return;
^92.7k
------------------
| Branch (36:7): [True: 6.46%, False: 93.54%]
------------------
37| 1.34M| node[x].a = (a * node[x].a) % P;
38| 1.34M| node[x].b = (a * node[x].b + b) % P;
39| 1.34M| node[x].val = (a * node[x].val + b) % P;
40| 1.34M| node[x].sum = (a * node[x].sum + b * node[x].size) % P;
41| 1.34M|}
42| |
43| 1.13M|def pushdown(int x) -> void {
44| 1.13M| if (node[x].rev) {
------------------
| Branch (44:7): [True: 34.13%, False: 65.87%]
------------------
45| 386k| std::swap(node[x].l, node[x].r);
46| 386k| node[node[x].l].rev ^= 1;
47| 386k| node[node[x].r].rev ^= 1;
48| 386k| node[x].rev = 0;
49| 386k| }
50| 1.13M| if ((node[x].a != 1) | (node[x].b != 0)) {
------------------
| Branch (50:7): [True: 63.17%, False: 36.83%]
------------------
51| 714k| affine(node[x].l, node[x].a, node[x].b);
52| 714k| affine(node[x].r, node[x].a, node[x].b);
53| 714k| node[x].a = 1;
54| 714k| node[x].b = 0;
55| 714k| }
56| 1.13M|}
57| |
58| 574k|def zig(int &x) -> void {
59| 574k| int l = node[x].l;
60| 574k| node[x].l = node[l].r;
61| 574k| maintain(x);
62| 574k| node[l].r = x;
63| 574k| x = l;
64| 574k|}
65| |
66| 505k|def zag(int &x) -> void {
67| 505k| int r = node[x].r;
68| 505k| node[x].r = node[r].l;
69| 505k| maintain(x);
70| 505k| node[r].l = x;
71| 505k| x = r;
72| 505k|}
73| |
74| 578k|def splay(int &x, int k) -> void {
75| 578k| pushdown(x);
76| 578k| if (int &l = node[x].l, &r = node[x].r, size = node[l].size; k == size) {
------------------
| Branch (76:64): [True: 4.46%, False: 95.54%]
------------------
77| 25.7k| return;
78| 552k| } else if (k < size) {
------------------
| Branch (78:14): [True: 53.27%, False: 46.73%]
------------------
79| 294k| pushdown(l);
80| 294k| if (int &ll = node[l].l, &lr = node[l].r, size = node[ll].size; k == size) {
------------------
| Branch (80:69): [True: 5.10%, False: 94.90%]
------------------
81| 15.0k| zig(x);
82| 279k| } else if (k < size) {
------------------
| Branch (82:16): [True: 57.76%, False: 42.24%]
------------------
83| 161k| splay(ll, k);
84| 161k| zig(x);
85| 161k| zig(x);
86| 161k| } else {
87| 117k| splay(lr, k - size - 1);
88| 117k| zag(l);
89| 117k| zig(x);
90| 117k| }
91| 294k| } else {
92| 258k| pushdown(r);
93| 258k| k -= size + 1;
94| 258k| if (int &rl = node[r].l, &rr = node[r].r, size = node[rl].size; k == size) {
------------------
| Branch (94:69): [True: 4.20%, False: 95.80%]
------------------
95| 10.8k| zag(x);
96| 247k| } else if (k < size) {
------------------
| Branch (96:16): [True: 47.86%, False: 52.14%]
------------------
97| 118k| splay(rl, k);
98| 118k| zig(r);
99| 118k| zag(x);
100| 129k| } else {
101| 129k| splay(rr, k - size - 1);
102| 129k| zag(x);
103| 129k| zag(x);
104| 129k| }
105| 258k| }
106| 578k|}
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| 455k| for (int i = 1; i <= n + q + 1; ++i) node[i] = Node{.a = 1, .size = 1};
^455k^455k
------------------
| Branch (115:19): [True: 100.00%, False: 0.00%]
------------------
116| 429k| for (int i = 2; i <= n + 1; ++i) node[i].val = rd.uw();
^429k^429k
------------------
| Branch (116:19): [True: 100.00%, False: 0.00%]
------------------
117| 1| int x = build(1, n += 2);
118| 25.8k| while (q--) {
------------------
| Branch (118:10): [True: 100.00%, False: 0.00%]
------------------
119| 25.8k| let t = rd.u1();
120| 25.8k| if (t == 0) {
------------------
| Branch (120:9): [True: 20.13%, False: 79.87%]
------------------
121| 5.20k| ++n;
122| 5.20k| int k = rd.uh();
123| 5.20k| node[n].val = node[n].sum = rd.uw();
124| 5.20k| splay(x, k);
125| 5.20k| splay(node[x].r, 0);
126| 5.20k| node[node[x].r].l = n;
127| 5.20k| }
128| 25.8k| if (t == 1) {
------------------
| Branch (128:9): [True: 20.33%, False: 79.67%]
------------------
129| 5.25k| int k = rd.uh();
130| 5.25k| splay(x, k);
131| 5.25k| splay(node[x].r, 1);
132| 5.25k| node[node[x].r].l = 0;
133| 5.25k| }
134| 25.8k| if (t == 2) {
------------------
| Branch (134:9): [True: 19.83%, False: 80.17%]
------------------
135| 5.12k| int l = rd.uh();
136| 5.12k| int r = rd.uh();
137| 5.12k| splay(x, l);
138| 5.12k| splay(node[x].r, r - l);
139| 5.12k| reverse(node[node[x].r].l);
140| 5.12k| }
141| 25.8k| if (t == 3) {
------------------
| Branch (141:9): [True: 19.97%, False: 80.03%]
------------------
142| 5.16k| int l = rd.uh();
143| 5.16k| int r = rd.uh();
144| 5.16k| splay(x, l);
145| 5.16k| splay(node[x].r, r - l);
146| 5.16k| int a = rd.uw();
147| 5.16k| int b = rd.uw();
148| 5.16k| affine(node[node[x].r].l, a, b);
149| 5.16k| }
150| 25.8k| if (t == 4) {
------------------
| Branch (150:9): [True: 19.74%, False: 80.26%]
------------------
151| 5.10k| int l = rd.uh();
152| 5.10k| int r = rd.uh();
153| 5.10k| splay(x, l);
154| 5.10k| splay(node[x].r, r - l);
155| 5.10k| wt.ud(node[node[node[x].r].l].sum);
156| 5.10k| }
157| 25.8k| }
158| 1| return 0;
159| 1|}