/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| 22.4M|def maintain(int x) -> void {
20| 22.4M| node[x].size = node[node[x].l].size + 1 + node[node[x].r].size;
21| 22.4M| node[x].sum = (node[node[x].l].sum + node[x].val + node[node[x].r].sum) % P;
22| 22.4M|}
23| |
24| 998k|def build(int l, int r) -> int {
25| 998k| if (l > r) return 0;
^499k
------------------
| Branch (25:7): [True: 50.00%, False: 50.00%]
------------------
26| 499k| int m = (l + r) / 2;
27| 499k| node[m].l = build(l, m - 1);
28| 499k| node[m].r = build(m + 1, r);
29| 499k| maintain(m);
30| 499k| return m;
31| 998k|}
32| |
33| 99.6k|def reverse(int x) -> void { node[x].rev ^= 1; }
34| |
35| 29.6M|def affine(int x, u64 a, u64 b) -> void {
36| 29.6M| if (x == 0) return;
^2.25M
------------------
| Branch (36:7): [True: 7.60%, False: 92.40%]
------------------
37| 27.4M| node[x].a = (a * node[x].a) % P;
38| 27.4M| node[x].b = (a * node[x].b + b) % P;
39| 27.4M| node[x].val = (a * node[x].val + b) % P;
40| 27.4M| node[x].sum = (a * node[x].sum + b * node[x].size) % P;
41| 27.4M|}
42| |
43| 22.9M|def pushdown(int x) -> void {
44| 22.9M| if (node[x].rev) {
------------------
| Branch (44:7): [True: 34.81%, False: 65.19%]
------------------
45| 7.97M| std::swap(node[x].l, node[x].r);
46| 7.97M| node[node[x].l].rev ^= 1;
47| 7.97M| node[node[x].r].rev ^= 1;
48| 7.97M| node[x].rev = 0;
49| 7.97M| }
50| 22.9M| if ((node[x].a != 1) | (node[x].b != 0)) {
------------------
| Branch (50:7): [True: 64.53%, False: 35.47%]
------------------
51| 14.7M| affine(node[x].l, node[x].a, node[x].b);
52| 14.7M| affine(node[x].r, node[x].a, node[x].b);
53| 14.7M| node[x].a = 1;
54| 14.7M| node[x].b = 0;
55| 14.7M| }
56| 22.9M|}
57| |
58| 11.6M|def zig(int &x) -> void {
59| 11.6M| int l = node[x].l;
60| 11.6M| node[x].l = node[l].r;
61| 11.6M| maintain(x);
62| 11.6M| node[l].r = x;
63| 11.6M| x = l;
64| 11.6M|}
65| |
66| 10.2M|def zag(int &x) -> void {
67| 10.2M| int r = node[x].r;
68| 10.2M| node[x].r = node[r].l;
69| 10.2M| maintain(x);
70| 10.2M| node[r].l = x;
71| 10.2M| x = r;
72| 10.2M|}
73| |
74| 11.7M|def splay(int &x, int k) -> void {
75| 11.7M| pushdown(x);
76| 11.7M| if (int &l = node[x].l, &r = node[x].r, size = node[l].size; k == size) {
------------------
| Branch (76:64): [True: 4.27%, False: 95.73%]
------------------
77| 499k| return;
78| 11.2M| } else if (k < size) {
------------------
| Branch (78:14): [True: 53.25%, False: 46.75%]
------------------
79| 5.96M| pushdown(l);
80| 5.96M| if (int &ll = node[l].l, &lr = node[l].r, size = node[ll].size; k == size) {
------------------
| Branch (80:69): [True: 4.82%, False: 95.18%]
------------------
81| 287k| zig(x);
82| 5.68M| } else if (k < size) {
------------------
| Branch (82:16): [True: 58.03%, False: 41.97%]
------------------
83| 3.29M| splay(ll, k);
84| 3.29M| zig(x);
85| 3.29M| zig(x);
86| 3.29M| } else {
87| 2.38M| splay(lr, k - size - 1);
88| 2.38M| zag(l);
89| 2.38M| zig(x);
90| 2.38M| }
91| 5.96M| } else {
92| 5.23M| pushdown(r);
93| 5.23M| k -= size + 1;
94| 5.23M| if (int &rl = node[r].l, &rr = node[r].r, size = node[rl].size; k == size) {
------------------
| Branch (94:69): [True: 4.03%, False: 95.97%]
------------------
95| 211k| zag(x);
96| 5.02M| } else if (k < size) {
------------------
| Branch (96:16): [True: 47.74%, False: 52.26%]
------------------
97| 2.40M| splay(rl, k);
98| 2.40M| zig(r);
99| 2.40M| zag(x);
100| 2.62M| } else {
101| 2.62M| splay(rr, k - size - 1);
102| 2.62M| zag(x);
103| 2.62M| zag(x);
104| 2.62M| }
105| 5.23M| }
106| 11.7M|}
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| 998k| for (int i = 1; i <= n + q + 1; ++i) node[i] = Node{.a = 1, .size = 1};
^998k^998k
------------------
| Branch (115:19): [True: 100.00%, False: 0.00%]
------------------
116| 499k| for (int i = 2; i <= n + 1; ++i) node[i].val = rd.uw();
^499k^499k
------------------
| Branch (116:19): [True: 100.00%, False: 0.00%]
------------------
117| 1| int x = build(1, n += 2);
118| 499k| while (q--) {
------------------
| Branch (118:10): [True: 100.00%, False: 0.00%]
------------------
119| 499k| let t = rd.u1();
120| 499k| if (t == 0) {
------------------
| Branch (120:9): [True: 20.00%, False: 80.00%]
------------------
121| 99.8k| ++n;
122| 99.8k| int k = rd.uh();
123| 99.8k| node[n].val = node[n].sum = rd.uw();
124| 99.8k| splay(x, k);
125| 99.8k| splay(node[x].r, 0);
126| 99.8k| node[node[x].r].l = n;
127| 99.8k| }
128| 499k| if (t == 1) {
------------------
| Branch (128:9): [True: 19.95%, False: 80.05%]
------------------
129| 99.5k| int k = rd.uh();
130| 99.5k| splay(x, k);
131| 99.5k| splay(node[x].r, 1);
132| 99.5k| node[node[x].r].l = 0;
133| 99.5k| }
134| 499k| if (t == 2) {
------------------
| Branch (134:9): [True: 19.97%, False: 80.03%]
------------------
135| 99.6k| int l = rd.uh();
136| 99.6k| int r = rd.uh();
137| 99.6k| splay(x, l);
138| 99.6k| splay(node[x].r, r - l);
139| 99.6k| reverse(node[node[x].r].l);
140| 99.6k| }
141| 499k| if (t == 3) {
------------------
| Branch (141:9): [True: 20.10%, False: 79.90%]
------------------
142| 100k| int l = rd.uh();
143| 100k| int r = rd.uh();
144| 100k| splay(x, l);
145| 100k| splay(node[x].r, r - l);
146| 100k| int a = rd.uw();
147| 100k| int b = rd.uw();
148| 100k| affine(node[node[x].r].l, a, b);
149| 100k| }
150| 499k| if (t == 4) {
------------------
| Branch (150:9): [True: 19.99%, False: 80.01%]
------------------
151| 99.7k| int l = rd.uh();
152| 99.7k| int r = rd.uh();
153| 99.7k| splay(x, l);
154| 99.7k| splay(node[x].r, r - l);
155| 99.7k| wt.ud(node[node[node[x].r].l].sum);
156| 99.7k| }
157| 499k| }
158| 1| return 0;
159| 1|}