/tmp/solutions/build/vertex_set_path_composite-main.cpp:
1| |#include <common.h>
2| |#include <toy/bit.h>
3| |prelude;
4| |
5| |namespace {
6| |
7| |constexpr int N = 2e5;
8| |constexpr int P = 998244353;
9| |
10| |struct Node {
11| | u32 a, b, c;
12| 668| auto operator+(const Node &t) const -> Node {
13| 668| u32 x = u64(a) * t.a % P;
14| 668| u32 y = (u64(a) * t.b + b) % P;
15| 668| u32 z = (u64(t.a) * c + t.c) % P;
16| 668| return {x, y, z};
17| 668| }
18| |} node[N * 2];
19| 1.69k|auto operator+(const Node &l, u32 r) -> u32 { return (u64(l.a) * r + l.b) % P; }
20| 1.82k|auto operator+(u32 l, const Node &r) -> u32 { return (u64(r.a) * l + r.c) % P; }
21| |u32 a[N];
22| |u32 b[N];
23| |int head[N];
24| |int size[N];
25| |int depth[N];
26| |int heavy[N];
27| |int ances[N];
28| |int parent[N];
29| |int node2id[N];
30| |int id;
31| |struct {
32| | int to;
33| | int next;
34| |} edge[N * 2];
35| |
36| 198|def build_step_1(int u, int p) -> void {
37| 198| size[u] = 1;
38| 592| for (int e = head[u]; e; e = edge[e].next) {
^394
------------------
| Branch (38:25): [True: 66.55%, False: 33.45%]
------------------
39| 394| int v = edge[e].to;
40| 394| if (v != p) {
------------------
| Branch (40:9): [True: 50.00%, False: 50.00%]
------------------
41| 197| build_step_1(v, u);
42| 197| size[u] += size[v];
43| 197| if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
^100
------------------
| Branch (43:11): [True: 49.24%, False: 50.76%]
| Branch (43:28): [True: 47.00%, False: 53.00%]
------------------
44| 144| heavy[u] = v;
45| 144| }
46| 197| }
47| 394| }
48| 198|}
49| |
50| 198|def build_step_2(int u, int w, int p, int d) -> void {
51| 198| int i = id++;
52| 198| node2id[u] = i;
53| 198| depth[i] = d;
54| 198| int anc = ances[i] = node2id[w];
55| 198| parent[i] = node2id[p];
56| 198| node[i * 2 + 1] = {a[u], b[u], b[u]};
57| 198| let a = node + anc * 2;
58| 198| int k = (i - anc) * 2 + 1;
59| 276| for (int j = 1; k & (j * 2); k -= j, j *= 2) {
^78
------------------
| Branch (59:19): [True: 28.26%, False: 71.74%]
------------------
60| 78| a[k - j] = a[k - j * 2] + a[k];
61| 78| }
62| 198| if (int v = heavy[u]; v) {
------------------
| Branch (62:25): [True: 48.99%, False: 51.01%]
------------------
63| 97| build_step_2(v, w, u, d + 1);
64| 97| }
65| 592| for (int e = head[u]; e; e = edge[e].next) {
^394
------------------
| Branch (65:25): [True: 66.55%, False: 33.45%]
------------------
66| 394| int v = edge[e].to;
67| 394| if (v != p && v != heavy[u]) {
^197
------------------
| Branch (67:9): [True: 50.00%, False: 50.00%]
| Branch (67:19): [True: 50.76%, False: 49.24%]
------------------
68| 100| build_step_2(v, v, u, d + 1);
69| 100| }
70| 394| }
71| 198|}
72| |
73| 1.04k|def apply_1(int i, u32 x) -> u32 {
74| 1.04k| int anc = ances[i];
75| 1.04k| let a = node + anc * 2;
76| 1.04k| int r = (i - anc) * 2 + 2;
77| 2.26k| for (; r; r -= r & -r) {
^1.21k
------------------
| Branch (77:10): [True: 53.87%, False: 46.13%]
------------------
78| 1.21k| x = a[r - (r & -r) / 2] + x;
79| 1.21k| }
80| 1.04k| return x;
81| 1.04k|}
82| |
83| 1.03k|def apply_2(int i, u32 x) -> u32 {
84| 1.03k| int anc = ances[i];
85| 1.03k| let a = node + anc * 2;
86| 1.03k| int r = (i - anc) * 2 + 2;
87| 2.22k| for (int k = 0; k < r;) {
------------------
| Branch (87:19): [True: 53.51%, False: 46.49%]
------------------
88| 1.19k| int t = 1 << log(r - k);
89| 1.19k| x = x + a[k + t / 2];
90| 1.19k| k += t;
91| 1.19k| }
92| 1.03k| return x;
93| 1.03k|}
94| |
95| 264|def apply_1(int i, int j, u32 x) -> u32 {
96| 264| int anc = ances[i];
97| 264| if (i == anc) return apply_1(j, x);
^35
------------------
| Branch (97:7): [True: 13.26%, False: 86.74%]
------------------
98| 229| let a = node + anc * 2;
99| 229| int l = (i - anc) * 2 - 1;
100| 229| int r = (j - anc) * 2 + 3;
101| 229| int k = (-1 << log(l ^ r)) & r;
102| 457| for (--r; r > k; r -= r & -r) {
^228
------------------
| Branch (102:13): [True: 49.89%, False: 50.11%]
------------------
103| 228| x = a[r - (r & -r) / 2] + x;
104| 228| }
105| 476| for (++l; k > l;) {
------------------
| Branch (105:13): [True: 51.89%, False: 48.11%]
------------------
106| 247| int t = 1 << log(k - l);
107| 247| x = a[k - t / 2] + x;
108| 247| k -= t;
109| 247| }
110| 229| return x;
111| 264|}
112| |
113| 391|def apply_2(int i, int j, u32 x) -> u32 {
114| 391| int anc = ances[i];
115| 391| if (i == anc) return apply_2(j, x);
^43
------------------
| Branch (115:7): [True: 11.00%, False: 89.00%]
------------------
116| 348| let a = node + anc * 2;
117| 348| int l = (i - anc) * 2 - 1;
118| 348| int r = (j - anc) * 2 + 3;
119| 348| int k = (-1 << log(l ^ r)) & r;
120| 693| for (++l; l < k; l += l & -l) {
^345
------------------
| Branch (120:13): [True: 49.78%, False: 50.22%]
------------------
121| 345| x = x + a[l + (l & -l) / 2];
122| 345| }
123| 635| for (--r; k < r;) {
------------------
| Branch (123:13): [True: 45.20%, False: 54.80%]
------------------
124| 287| int t = 1 << log(r - k);
125| 287| x = x + a[k + t / 2];
126| 287| k += t;
127| 287| }
128| 348| return x;
129| 391|}
130| |
131| |} // namespace
132| |
133| 1|int main() {
134| 1| rd rd;
135| 1| wt wt;
136| 1| int n = rd.uh();
137| 1| int q = rd.uh();
138| 1|#ifdef LOCAL
139| 1| id = 0;
140| 1| std::memset(head, 0, 4 * n);
141| 1| std::memset(heavy, 0, 4 * n);
142| 1|#endif
143| 199| for (int i = 0; i < n; ++i) a[i] = rd.uw(), b[i] = rd.uw();
^198 ^198
------------------
| Branch (143:19): [True: 99.50%, False: 0.50%]
------------------
144| 198| for (int i = 1; i < n; ++i) {
^197
------------------
| Branch (144:19): [True: 99.49%, False: 0.51%]
------------------
145| 197| int u = rd.uh();
146| 197| int v = rd.uh();
147| 197| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
148| 197| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
149| 197| }
150| 1| build_step_1(0, 0);
151| 1| build_step_2(0, 0, 0, 0);
152| 1| std::memset(size, 0, sizeof(int) * n);
153| 199| for (int i = 0; i < n; ++i) size[ances[i]]++;
^198 ^198
------------------
| Branch (153:19): [True: 99.50%, False: 0.50%]
------------------
154| 1.26k| while (q--) {
------------------
| Branch (154:10): [True: 99.92%, False: 0.08%]
------------------
155| 1.25k| let t = rd.u1();
156| 1.25k| if (t == 0) {
------------------
| Branch (156:9): [True: 47.97%, False: 52.03%]
------------------
157| 604| int i = node2id[rd.uh()];
158| 604| u32 c = rd.uw();
159| 604| u32 d = rd.uw();
160| 604| int anc = ances[i];
161| 604| let a = node + anc * 2;
162| 604| int n = size[anc];
163| 604| int k = (i - anc) * 2 + 1;
164| 604| a[k] = {c, d, d};
165| 1.19k| for (int j = 1;; j *= 2) {
^590
166| 1.19k| if (k & (j * 2)) {
------------------
| Branch (166:13): [True: 25.54%, False: 74.46%]
------------------
167| 305| a[k - j] = a[k - j * 2] + a[k];
168| 305| k -= j;
169| 889| } else {
170| 889| if (k + j * 3 > n * 2) break;
^604
------------------
| Branch (170:15): [True: 67.94%, False: 32.06%]
------------------
171| 285| a[k + j] = a[k] + a[k + j * 2];
172| 285| k += j;
173| 285| }
174| 1.19k| }
175| 604| }
176| 1.25k| if (t == 1) {
------------------
| Branch (176:9): [True: 52.03%, False: 47.97%]
------------------
177| 655| int u = node2id[rd.uh()];
178| 655| int v = node2id[rd.uh()];
179| 655| u32 x = rd.uw();
180| 655| int vec[20], top{};
181| 2.65k| while (ances[u] != ances[v]) {
------------------
| Branch (181:14): [True: 75.32%, False: 24.68%]
------------------
182| 1.99k| if (depth[ances[u]] > depth[ances[v]]) {
------------------
| Branch (182:13): [True: 50.43%, False: 49.57%]
------------------
183| 1.00k| x = apply_1(u, x);
184| 1.00k| u = parent[ances[u]];
185| 1.00k| } else {
186| 991| vec[top++] = v;
187| 991| v = parent[ances[v]];
188| 991| }
189| 1.99k| }
190| 655| if (u > v) {
------------------
| Branch (190:11): [True: 40.31%, False: 59.69%]
------------------
191| 264| x = apply_1(v, u, x);
192| 391| } else {
193| 391| x = apply_2(u, v, x);
194| 391| }
195| 1.64k| while (top--) {
------------------
| Branch (195:14): [True: 60.21%, False: 39.79%]
------------------
196| 991| x = apply_2(vec[top], x);
197| 991| }
198| 655| wt.uw(x);
199| 655| }
200| 1.25k| }
201| 1| return 0;
202| 1|}