/tmp/solutions/build/range_affine_range_sum-main.cpp:
1| |#include <common.h>
2| |#include <toy/bit.h>
3| |prelude;
4| |
5| |namespace {
6| |
7| |constexpr int N = 1e6;
8| |constexpr int P = 998244353;
9| |
10| |struct affine {
11| | u32 a, b;
12| 2.62k| auto operator+(affine t) -> affine {
13| 2.62k| return {u32(u64(t.a) * a % P), u32((u64(t.a) * b + t.b) % P)};
14| 2.62k| }
15| 2.62k| auto operator+=(affine t) -> void { *this = *this + t; }
16| |};
17| |
18| |struct node {
19| | u32 siz;
20| | u32 sum;
21| | affine aff;
22| 2.62k| auto operator+=(affine t) -> void {
23| 2.62k| aff += t;
24| 2.62k| sum = (u64(t.a) * sum + u64(t.b) * siz) % P;
25| 2.62k| }
26| |} a[N];
27| |
28| 1.04k|void pushdown(int k) {
29| 2.09k| for (int i = log(k) - 1; i >= 0; --i) {
^1.04k
------------------
| Branch (29:28): [True: 50.00%, False: 50.00%]
------------------
30| 1.04k| affine t = a[k >> i >> 1].aff;
31| 1.04k| a[k >> i >> 1].aff = affine{1, 0};
32| 1.04k| a[k >> i ^ 0] += t;
33| 1.04k| a[k >> i ^ 1] += t;
34| 1.04k| }
35| 1.04k|}
36| |
37| 829|u32 mod(u32 x) { return x < P ? x : x - P; }
^670^159
------------------
| Branch (37:25): [True: 80.82%, False: 19.18%]
------------------
38| |
39| 1.04k|void pushup(int k) {
40| 1.39k| for (k /= 2; k > 0; k /= 2) {
^351
------------------
| Branch (40:16): [True: 25.09%, False: 74.91%]
------------------
41| 351| a[k].sum = mod(a[k * 2].sum + a[k * 2 + 1].sum);
42| 351| }
43| 1.04k|}
44| |
45| |}; // namespace
46| |
47| 1|int main() {
48| 1| rd rd;
49| 1| wt wt;
50| 1| int n = rd.uh();
51| 1| int q = rd.uh();
52| 3| for (int i = 0; i < n; ++i) a[n + i].siz = 1, a[n + i].sum = rd.uw();
^2 ^2
------------------
| Branch (52:19): [True: 66.67%, False: 33.33%]
------------------
53| 3| for (int i = n - 1; i >= 0; --i) {
^2
------------------
| Branch (53:23): [True: 66.67%, False: 33.33%]
------------------
54| 2| a[i].aff = {1, 0};
55| 2| a[i].siz = a[2 * i].siz + a[2 * i + 1].siz;
56| 2| a[i].sum = mod(a[2 * i].sum + a[2 * i + 1].sum);
57| 2| }
58| 1.00k| while (q--) {
------------------
| Branch (58:10): [True: 99.90%, False: 0.10%]
------------------
59| 1.00k| let t = rd.u1();
60| 1.00k| if (t == 0) {
------------------
| Branch (60:9): [True: 52.40%, False: 47.60%]
------------------
61| 524| int l = n + rd.uh();
62| 524| int r = n + rd.uh() - 1;
63| 524| pushdown(l--);
64| 524| pushdown(r++);
65| 524| int k = log(l ^ r);
66| 524| affine x = {rd.uw(), rd.uw()};
67| 524| int t, i;
68| 873| for (t = ~l & ~(-1 << k), i = 31; t > 0; t -= 1 << i) {
^349
------------------
| Branch (68:41): [True: 39.98%, False: 60.02%]
------------------
69| 349| i = log(t);
70| 349| a[l >> i ^ 1] += x;
71| 349| }
72| 524| pushup(l >> i);
73| 699| for (t = +r & ~(-1 << k), i = 31; t > 0; t -= 1 << i) {
^175
------------------
| Branch (73:41): [True: 25.04%, False: 74.96%]
------------------
74| 175| i = log(t);
75| 175| a[r >> i ^ 1] += x;
76| 175| }
77| 524| pushup(r >> i);
78| 524| }
79| 1.00k| if (t == 1) {
------------------
| Branch (79:9): [True: 47.60%, False: 52.40%]
------------------
80| 476| int l = n + rd.uh();
81| 476| int r = n + rd.uh() - 1;
82| 476| u64 sizL = 0, sumL = 0;
83| 476| u64 sizR = 0, sumR = 0;
84| 1.26k| for (--l, ++r; l ^ r ^ 1;) {
------------------
| Branch (84:22): [True: 62.40%, False: 37.60%]
------------------
85| 790| if (~l & 1) sizL += a[l ^ 1].siz, sumL += a[l ^ 1].sum;
^314
------------------
| Branch (85:13): [True: 39.75%, False: 60.25%]
------------------
86| 790| if (+r & 1) sizR += a[r ^ 1].siz, sumR += a[r ^ 1].sum;
^162
------------------
| Branch (86:13): [True: 20.51%, False: 79.49%]
------------------
87| 790| l /= 2, r /= 2;
88| 790| sumL = (a[l].aff.a * sumL + a[l].aff.b * sizL) % P;
89| 790| sumR = (a[r].aff.a * sumR + a[r].aff.b * sizR) % P;
90| 790| }
91| 476| sumL = mod(u32(sumL + sumR));
92| 476| sizL += sizR;
93| 476| for (l /= 2; l > 0; l /= 2) {
^0
------------------
| Branch (93:20): [True: 0.00%, False: 100.00%]
------------------
94| 0| sumL = (a[l].aff.a * sumL + a[l].aff.b * sizL) % P;
95| 0| }
96| 476| wt.uw(u32(sumL));
97| 476| }
98| 1.00k| }
99| 1| return 0;
100| 1|}