/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| 639| auto operator+(const Node &t) const -> Node {
13| 639| u32 x = u64(a) * t.a % P;
14| 639| u32 y = (u64(a) * t.b + b) % P;
15| 639| u32 z = (u64(t.a) * c + t.c) % P;
16| 639| return {x, y, z};
17| 639| }
18| |} node[N * 2];
19| 1.36k|auto operator+(const Node &l, u32 r) -> u32 { return (u64(l.a) * r + l.b) % P; }
20| 1.39k|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| 532|def build_step_1(int u, int p) -> void {
37| 532| size[u] = 1;
38| 1.59k| for (int e = head[u]; e; e = edge[e].next) {
^1.06k
------------------
| Branch (38:25): [True: 66.62%, False: 33.38%]
------------------
39| 1.06k| int v = edge[e].to;
40| 1.06k| if (v != p) {
------------------
| Branch (40:9): [True: 50.00%, False: 50.00%]
------------------
41| 531| build_step_1(v, u);
42| 531| size[u] += size[v];
43| 531| if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
^254
------------------
| Branch (43:11): [True: 52.17%, False: 47.83%]
| Branch (43:28): [True: 49.61%, False: 50.39%]
------------------
44| 403| heavy[u] = v;
45| 403| }
46| 531| }
47| 1.06k| }
48| 532|}
49| |
50| 532|def build_step_2(int u, int w, int p, int d) -> void {
51| 532| int i = id++;
52| 532| node2id[u] = i;
53| 532| depth[i] = d;
54| 532| int anc = ances[i] = node2id[w];
55| 532| parent[i] = node2id[p];
56| 532| node[i * 2 + 1] = {a[u], b[u], b[u]};
57| 532| let a = node + anc * 2;
58| 532| int k = (i - anc) * 2 + 1;
59| 758| for (int j = 1; k & (j * 2); k -= j, j *= 2) {
^226
------------------
| Branch (59:19): [True: 29.82%, False: 70.18%]
------------------
60| 226| a[k - j] = a[k - j * 2] + a[k];
61| 226| }
62| 532| if (int v = heavy[u]; v) {
------------------
| Branch (62:25): [True: 52.07%, False: 47.93%]
------------------
63| 277| build_step_2(v, w, u, d + 1);
64| 277| }
65| 1.59k| for (int e = head[u]; e; e = edge[e].next) {
^1.06k
------------------
| Branch (65:25): [True: 66.62%, False: 33.38%]
------------------
66| 1.06k| int v = edge[e].to;
67| 1.06k| if (v != p && v != heavy[u]) {
^531
------------------
| Branch (67:9): [True: 50.00%, False: 50.00%]
| Branch (67:19): [True: 47.83%, False: 52.17%]
------------------
68| 254| build_step_2(v, v, u, d + 1);
69| 254| }
70| 1.06k| }
71| 532|}
72| |
73| 823|def apply_1(int i, u32 x) -> u32 {
74| 823| int anc = ances[i];
75| 823| let a = node + anc * 2;
76| 823| int r = (i - anc) * 2 + 2;
77| 1.86k| for (; r; r -= r & -r) {
^1.03k
------------------
| Branch (77:10): [True: 55.80%, False: 44.20%]
------------------
78| 1.03k| x = a[r - (r & -r) / 2] + x;
79| 1.03k| }
80| 823| return x;
81| 823|}
82| |
83| 816|def apply_2(int i, u32 x) -> u32 {
84| 816| int anc = ances[i];
85| 816| let a = node + anc * 2;
86| 816| int r = (i - anc) * 2 + 2;
87| 1.80k| for (int k = 0; k < r;) {
------------------
| Branch (87:19): [True: 54.69%, False: 45.31%]
------------------
88| 985| int t = 1 << log(r - k);
89| 985| x = x + a[k + t / 2];
90| 985| k += t;
91| 985| }
92| 816| return x;
93| 816|}
94| |
95| 178|def apply_1(int i, int j, u32 x) -> u32 {
96| 178| int anc = ances[i];
97| 178| if (i == anc) return apply_1(j, x);
^11
------------------
| Branch (97:7): [True: 6.18%, False: 93.82%]
------------------
98| 167| let a = node + anc * 2;
99| 167| int l = (i - anc) * 2 - 1;
100| 167| int r = (j - anc) * 2 + 3;
101| 167| int k = (-1 << log(l ^ r)) & r;
102| 313| for (--r; r > k; r -= r & -r) {
^146
------------------
| Branch (102:13): [True: 46.65%, False: 53.35%]
------------------
103| 146| x = a[r - (r & -r) / 2] + x;
104| 146| }
105| 351| for (++l; k > l;) {
------------------
| Branch (105:13): [True: 52.42%, False: 47.58%]
------------------
106| 184| int t = 1 << log(k - l);
107| 184| x = a[k - t / 2] + x;
108| 184| k -= t;
109| 184| }
110| 167| return x;
111| 178|}
112| |
113| 254|def apply_2(int i, int j, u32 x) -> u32 {
114| 254| int anc = ances[i];
115| 254| if (i == anc) return apply_2(j, x);
^24
------------------
| Branch (115:7): [True: 9.45%, False: 90.55%]
------------------
116| 230| let a = node + anc * 2;
117| 230| int l = (i - anc) * 2 - 1;
118| 230| int r = (j - anc) * 2 + 3;
119| 230| int k = (-1 << log(l ^ r)) & r;
120| 424| for (++l; l < k; l += l & -l) {
^194
------------------
| Branch (120:13): [True: 45.75%, False: 54.25%]
------------------
121| 194| x = x + a[l + (l & -l) / 2];
122| 194| }
123| 446| for (--r; k < r;) {
------------------
| Branch (123:13): [True: 48.43%, False: 51.57%]
------------------
124| 216| int t = 1 << log(r - k);
125| 216| x = x + a[k + t / 2];
126| 216| k += t;
127| 216| }
128| 230| return x;
129| 254|}
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| 533| for (int i = 0; i < n; ++i) a[i] = rd.uw(), b[i] = rd.uw();
^532 ^532
------------------
| Branch (143:19): [True: 99.81%, False: 0.19%]
------------------
144| 532| for (int i = 1; i < n; ++i) {
^531
------------------
| Branch (144:19): [True: 99.81%, False: 0.19%]
------------------
145| 531| int u = rd.uh();
146| 531| int v = rd.uh();
147| 531| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
148| 531| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
149| 531| }
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| 533| for (int i = 0; i < n; ++i) size[ances[i]]++;
^532 ^532
------------------
| Branch (153:19): [True: 99.81%, False: 0.19%]
------------------
154| 814| while (q--) {
------------------
| Branch (154:10): [True: 99.88%, False: 0.12%]
------------------
155| 813| let t = rd.u1();
156| 813| if (t == 0) {
------------------
| Branch (156:9): [True: 46.86%, False: 53.14%]
------------------
157| 381| int i = node2id[rd.uh()];
158| 381| u32 c = rd.uw();
159| 381| u32 d = rd.uw();
160| 381| int anc = ances[i];
161| 381| let a = node + anc * 2;
162| 381| int n = size[anc];
163| 381| int k = (i - anc) * 2 + 1;
164| 381| a[k] = {c, d, d};
165| 794| for (int j = 1;; j *= 2) {
^413
166| 794| if (k & (j * 2)) {
------------------
| Branch (166:13): [True: 26.95%, False: 73.05%]
------------------
167| 214| a[k - j] = a[k - j * 2] + a[k];
168| 214| k -= j;
169| 580| } else {
170| 580| if (k + j * 3 > n * 2) break;
^381
------------------
| Branch (170:15): [True: 65.69%, False: 34.31%]
------------------
171| 199| a[k + j] = a[k] + a[k + j * 2];
172| 199| k += j;
173| 199| }
174| 794| }
175| 381| }
176| 813| if (t == 1) {
------------------
| Branch (176:9): [True: 53.14%, False: 46.86%]
------------------
177| 432| int u = node2id[rd.uh()];
178| 432| int v = node2id[rd.uh()];
179| 432| u32 x = rd.uw();
180| 432| int vec[20], top{};
181| 2.03k| while (ances[u] != ances[v]) {
------------------
| Branch (181:14): [True: 78.78%, False: 21.22%]
------------------
182| 1.60k| if (depth[ances[u]] > depth[ances[v]]) {
------------------
| Branch (182:13): [True: 50.62%, False: 49.38%]
------------------
183| 812| x = apply_1(u, x);
184| 812| u = parent[ances[u]];
185| 812| } else {
186| 792| vec[top++] = v;
187| 792| v = parent[ances[v]];
188| 792| }
189| 1.60k| }
190| 432| if (u > v) {
------------------
| Branch (190:11): [True: 41.20%, False: 58.80%]
------------------
191| 178| x = apply_1(v, u, x);
192| 254| } else {
193| 254| x = apply_2(u, v, x);
194| 254| }
195| 1.22k| while (top--) {
------------------
| Branch (195:14): [True: 64.71%, False: 35.29%]
------------------
196| 792| x = apply_2(vec[top], x);
197| 792| }
198| 432| wt.uw(x);
199| 432| }
200| 813| }
201| 1| return 0;
202| 1|}