/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| 2.17M|def maintain(int x) -> void {
20| 2.17M| node[x].size = node[node[x].l].size + 1 + node[node[x].r].size;
21| 2.17M| node[x].sum = (node[node[x].l].sum + node[x].val + node[node[x].r].sum) % P;
22| 2.17M|}
23| |
24| 25|def build(int l, int r) -> int {
25| 25| if (l > r) return 0;
^13
------------------
| Branch (25:7): [True: 52.00%, False: 48.00%]
------------------
26| 12| int m = (l + r) / 2;
27| 12| node[m].l = build(l, m - 1);
28| 12| node[m].r = build(m + 1, r);
29| 12| maintain(m);
30| 12| return m;
31| 25|}
32| |
33| 0|def reverse(int x) -> void { node[x].rev ^= 1; }
34| |
35| 0|def affine(int x, u64 a, u64 b) -> void {
36| 0| if (x == 0) return;
------------------
| Branch (36:7): [True: 0.00%, False: 0.00%]
------------------
37| 0| node[x].a = (a * node[x].a) % P;
38| 0| node[x].b = (a * node[x].b + b) % P;
39| 0| node[x].val = (a * node[x].val + b) % P;
40| 0| node[x].sum = (a * node[x].sum + b * node[x].size) % P;
41| 0|}
42| |
43| 3.17M|def pushdown(int x) -> void {
44| 3.17M| if (node[x].rev) {
------------------
| Branch (44:7): [True: 0.00%, False: 100.00%]
------------------
45| 0| std::swap(node[x].l, node[x].r);
46| 0| node[node[x].l].rev ^= 1;
47| 0| node[node[x].r].rev ^= 1;
48| 0| node[x].rev = 0;
49| 0| }
50| 3.17M| if ((node[x].a != 1) | (node[x].b != 0)) {
------------------
| Branch (50:7): [True: 0.00%, False: 100.00%]
------------------
51| 0| affine(node[x].l, node[x].a, node[x].b);
52| 0| affine(node[x].r, node[x].a, node[x].b);
53| 0| node[x].a = 1;
54| 0| node[x].b = 0;
55| 0| }
56| 3.17M|}
57| |
58| 1.19M|def zig(int &x) -> void {
59| 1.19M| int l = node[x].l;
60| 1.19M| node[x].l = node[l].r;
61| 1.19M| maintain(x);
62| 1.19M| node[l].r = x;
63| 1.19M| x = l;
64| 1.19M|}
65| |
66| 979k|def zag(int &x) -> void {
67| 979k| int r = node[x].r;
68| 979k| node[x].r = node[r].l;
69| 979k| maintain(x);
70| 979k| node[r].l = x;
71| 979k| x = r;
72| 979k|}
73| |
74| 1.86M|def splay(int &x, int k) -> void {
75| 1.86M| pushdown(x);
76| 1.86M| if (int &l = node[x].l, &r = node[x].r, size = node[l].size; k == size) {
------------------
| Branch (76:64): [True: 29.61%, False: 70.39%]
------------------
77| 551k| return;
78| 1.31M| } else if (k < size) {
------------------
| Branch (78:14): [True: 53.16%, False: 46.84%]
------------------
79| 696k| pushdown(l);
80| 696k| if (int &ll = node[l].l, &lr = node[l].r, size = node[ll].size; k == size) {
------------------
| Branch (80:69): [True: 39.43%, False: 60.57%]
------------------
81| 274k| zig(x);
82| 421k| } else if (k < size) {
------------------
| Branch (82:16): [True: 69.08%, False: 30.92%]
------------------
83| 291k| splay(ll, k);
84| 291k| zig(x);
85| 291k| zig(x);
86| 291k| } else {
87| 130k| splay(lr, k - size - 1);
88| 130k| zag(l);
89| 130k| zig(x);
90| 130k| }
91| 696k| } else {
92| 613k| pushdown(r);
93| 613k| k -= size + 1;
94| 613k| if (int &rl = node[r].l, &rr = node[r].r, size = node[rl].size; k == size) {
------------------
| Branch (94:69): [True: 28.39%, False: 71.61%]
------------------
95| 174k| zag(x);
96| 439k| } else if (k < size) {
------------------
| Branch (96:16): [True: 46.48%, False: 53.52%]
------------------
97| 204k| splay(rl, k);
98| 204k| zig(r);
99| 204k| zag(x);
100| 235k| } else {
101| 235k| splay(rr, k - size - 1);
102| 235k| zag(x);
103| 235k| zag(x);
104| 235k| }
105| 613k| }
106| 1.86M|}
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| 500k| for (int i = 1; i <= n + q + 1; ++i) node[i] = Node{.a = 1, .size = 1};
^500k^500k
------------------
| Branch (115:19): [True: 100.00%, False: 0.00%]
------------------
116| 11| for (int i = 2; i <= n + 1; ++i) node[i].val = rd.uw();
^10 ^10
------------------
| Branch (116:19): [True: 90.91%, False: 9.09%]
------------------
117| 1| int x = build(1, n += 2);
118| 500k| while (q--) {
------------------
| Branch (118:10): [True: 100.00%, False: 0.00%]
------------------
119| 500k| let t = rd.u1();
120| 500k| if (t == 0) {
------------------
| Branch (120:9): [True: 50.00%, False: 50.00%]
------------------
121| 250k| ++n;
122| 250k| int k = rd.uh();
123| 250k| node[n].val = node[n].sum = rd.uw();
124| 250k| splay(x, k);
125| 250k| splay(node[x].r, 0);
126| 250k| node[node[x].r].l = n;
127| 250k| }
128| 500k| if (t == 1) {
------------------
| Branch (128:9): [True: 0.00%, False: 100.00%]
------------------
129| 0| int k = rd.uh();
130| 0| splay(x, k);
131| 0| splay(node[x].r, 1);
132| 0| node[node[x].r].l = 0;
133| 0| }
134| 500k| if (t == 2) {
------------------
| Branch (134:9): [True: 0.00%, False: 100.00%]
------------------
135| 0| int l = rd.uh();
136| 0| int r = rd.uh();
137| 0| splay(x, l);
138| 0| splay(node[x].r, r - l);
139| 0| reverse(node[node[x].r].l);
140| 0| }
141| 500k| if (t == 3) {
------------------
| Branch (141:9): [True: 0.00%, False: 100.00%]
------------------
142| 0| int l = rd.uh();
143| 0| int r = rd.uh();
144| 0| splay(x, l);
145| 0| splay(node[x].r, r - l);
146| 0| int a = rd.uw();
147| 0| int b = rd.uw();
148| 0| affine(node[node[x].r].l, a, b);
149| 0| }
150| 500k| if (t == 4) {
------------------
| Branch (150:9): [True: 50.00%, False: 50.00%]
------------------
151| 250k| int l = rd.uh();
152| 250k| int r = rd.uh();
153| 250k| splay(x, l);
154| 250k| splay(node[x].r, r - l);
155| 250k| wt.ud(node[node[node[x].r].l].sum);
156| 250k| }
157| 500k| }
158| 1| return 0;
159| 1|}