/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| 1.07k| auto operator+(const Node &t) const -> Node {
13| 1.07k| u32 x = u64(a) * t.a % P;
14| 1.07k| u32 y = (u64(a) * t.b + b) % P;
15| 1.07k| u32 z = (u64(t.a) * c + t.c) % P;
16| 1.07k| return {x, y, z};
17| 1.07k| }
18| |} node[N * 2];
19| 2.11k|auto operator+(const Node &l, u32 r) -> u32 { return (u64(l.a) * r + l.b) % P; }
20| 2.26k|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| 693|def build_step_1(int u, int p) -> void {
37| 693| size[u] = 1;
38| 2.07k| for (int e = head[u]; e; e = edge[e].next) {
^1.38k
------------------
| Branch (38:25): [True: 66.63%, False: 33.37%]
------------------
39| 1.38k| int v = edge[e].to;
40| 1.38k| if (v != p) {
------------------
| Branch (40:9): [True: 50.00%, False: 50.00%]
------------------
41| 692| build_step_1(v, u);
42| 692| size[u] += size[v];
43| 692| if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
^329
------------------
| Branch (43:11): [True: 52.46%, False: 47.54%]
| Branch (43:28): [True: 48.02%, False: 51.98%]
------------------
44| 521| heavy[u] = v;
45| 521| }
46| 692| }
47| 1.38k| }
48| 693|}
49| |
50| 693|def build_step_2(int u, int w, int p, int d) -> void {
51| 693| int i = id++;
52| 693| node2id[u] = i;
53| 693| depth[i] = d;
54| 693| int anc = ances[i] = node2id[w];
55| 693| parent[i] = node2id[p];
56| 693| node[i * 2 + 1] = {a[u], b[u], b[u]};
57| 693| let a = node + anc * 2;
58| 693| int k = (i - anc) * 2 + 1;
59| 997| for (int j = 1; k & (j * 2); k -= j, j *= 2) {
^304
------------------
| Branch (59:19): [True: 30.49%, False: 69.51%]
------------------
60| 304| a[k - j] = a[k - j * 2] + a[k];
61| 304| }
62| 693| if (int v = heavy[u]; v) {
------------------
| Branch (62:25): [True: 52.38%, False: 47.62%]
------------------
63| 363| build_step_2(v, w, u, d + 1);
64| 363| }
65| 2.07k| for (int e = head[u]; e; e = edge[e].next) {
^1.38k
------------------
| Branch (65:25): [True: 66.63%, False: 33.37%]
------------------
66| 1.38k| int v = edge[e].to;
67| 1.38k| if (v != p && v != heavy[u]) {
^692
------------------
| Branch (67:9): [True: 50.00%, False: 50.00%]
| Branch (67:19): [True: 47.54%, False: 52.46%]
------------------
68| 329| build_step_2(v, v, u, d + 1);
69| 329| }
70| 1.38k| }
71| 693|}
72| |
73| 1.33k|def apply_1(int i, u32 x) -> u32 {
74| 1.33k| int anc = ances[i];
75| 1.33k| let a = node + anc * 2;
76| 1.33k| int r = (i - anc) * 2 + 2;
77| 2.98k| for (; r; r -= r & -r) {
^1.64k
------------------
| Branch (77:10): [True: 55.08%, False: 44.92%]
------------------
78| 1.64k| x = a[r - (r & -r) / 2] + x;
79| 1.64k| }
80| 1.33k| return x;
81| 1.33k|}
82| |
83| 1.34k|def apply_2(int i, u32 x) -> u32 {
84| 1.34k| int anc = ances[i];
85| 1.34k| let a = node + anc * 2;
86| 1.34k| int r = (i - anc) * 2 + 2;
87| 2.97k| for (int k = 0; k < r;) {
------------------
| Branch (87:19): [True: 54.73%, False: 45.27%]
------------------
88| 1.62k| int t = 1 << log(r - k);
89| 1.62k| x = x + a[k + t / 2];
90| 1.62k| k += t;
91| 1.62k| }
92| 1.34k| return x;
93| 1.34k|}
94| |
95| 247|def apply_1(int i, int j, u32 x) -> u32 {
96| 247| int anc = ances[i];
97| 247| if (i == anc) return apply_1(j, x);
^22
------------------
| Branch (97:7): [True: 8.91%, False: 91.09%]
------------------
98| 225| let a = node + anc * 2;
99| 225| int l = (i - anc) * 2 - 1;
100| 225| int r = (j - anc) * 2 + 3;
101| 225| int k = (-1 << log(l ^ r)) & r;
102| 425| for (--r; r > k; r -= r & -r) {
^200
------------------
| Branch (102:13): [True: 47.06%, False: 52.94%]
------------------
103| 200| x = a[r - (r & -r) / 2] + x;
104| 200| }
105| 501| for (++l; k > l;) {
------------------
| Branch (105:13): [True: 55.09%, False: 44.91%]
------------------
106| 276| int t = 1 << log(k - l);
107| 276| x = a[k - t / 2] + x;
108| 276| k -= t;
109| 276| }
110| 225| return x;
111| 247|}
112| |
113| 406|def apply_2(int i, int j, u32 x) -> u32 {
114| 406| int anc = ances[i];
115| 406| if (i == anc) return apply_2(j, x);
^25
------------------
| Branch (115:7): [True: 6.16%, False: 93.84%]
------------------
116| 381| let a = node + anc * 2;
117| 381| int l = (i - anc) * 2 - 1;
118| 381| int r = (j - anc) * 2 + 3;
119| 381| int k = (-1 << log(l ^ r)) & r;
120| 788| for (++l; l < k; l += l & -l) {
^407
------------------
| Branch (120:13): [True: 51.65%, False: 48.35%]
------------------
121| 407| x = x + a[l + (l & -l) / 2];
122| 407| }
123| 614| for (--r; k < r;) {
------------------
| Branch (123:13): [True: 37.95%, False: 62.05%]
------------------
124| 233| int t = 1 << log(r - k);
125| 233| x = x + a[k + t / 2];
126| 233| k += t;
127| 233| }
128| 381| return x;
129| 406|}
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| 694| for (int i = 0; i < n; ++i) a[i] = rd.uw(), b[i] = rd.uw();
^693 ^693
------------------
| Branch (143:19): [True: 99.86%, False: 0.14%]
------------------
144| 693| for (int i = 1; i < n; ++i) {
^692
------------------
| Branch (144:19): [True: 99.86%, False: 0.14%]
------------------
145| 692| int u = rd.uh();
146| 692| int v = rd.uh();
147| 692| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
148| 692| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
149| 692| }
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| 694| for (int i = 0; i < n; ++i) size[ances[i]]++;
^693 ^693
------------------
| Branch (153:19): [True: 99.86%, False: 0.14%]
------------------
154| 1.32k| while (q--) {
------------------
| Branch (154:10): [True: 99.92%, False: 0.08%]
------------------
155| 1.32k| let t = rd.u1();
156| 1.32k| if (t == 0) {
------------------
| Branch (156:9): [True: 50.64%, False: 49.36%]
------------------
157| 670| int i = node2id[rd.uh()];
158| 670| u32 c = rd.uw();
159| 670| u32 d = rd.uw();
160| 670| int anc = ances[i];
161| 670| let a = node + anc * 2;
162| 670| int n = size[anc];
163| 670| int k = (i - anc) * 2 + 1;
164| 670| a[k] = {c, d, d};
165| 1.44k| for (int j = 1;; j *= 2) {
^773
166| 1.44k| if (k & (j * 2)) {
------------------
| Branch (166:13): [True: 26.96%, False: 73.04%]
------------------
167| 389| a[k - j] = a[k - j * 2] + a[k];
168| 389| k -= j;
169| 1.05k| } else {
170| 1.05k| if (k + j * 3 > n * 2) break;
^670
------------------
| Branch (170:15): [True: 63.57%, False: 36.43%]
------------------
171| 384| a[k + j] = a[k] + a[k + j * 2];
172| 384| k += j;
173| 384| }
174| 1.44k| }
175| 670| }
176| 1.32k| if (t == 1) {
------------------
| Branch (176:9): [True: 49.36%, False: 50.64%]
------------------
177| 653| int u = node2id[rd.uh()];
178| 653| int v = node2id[rd.uh()];
179| 653| u32 x = rd.uw();
180| 653| int vec[20], top{};
181| 3.29k| while (ances[u] != ances[v]) {
------------------
| Branch (181:14): [True: 80.15%, False: 19.85%]
------------------
182| 2.63k| if (depth[ances[u]] > depth[ances[v]]) {
------------------
| Branch (182:13): [True: 49.94%, False: 50.06%]
------------------
183| 1.31k| x = apply_1(u, x);
184| 1.31k| u = parent[ances[u]];
185| 1.32k| } else {
186| 1.32k| vec[top++] = v;
187| 1.32k| v = parent[ances[v]];
188| 1.32k| }
189| 2.63k| }
190| 653| if (u > v) {
------------------
| Branch (190:11): [True: 37.83%, False: 62.17%]
------------------
191| 247| x = apply_1(v, u, x);
192| 406| } else {
193| 406| x = apply_2(u, v, x);
194| 406| }
195| 1.97k| while (top--) {
------------------
| Branch (195:14): [True: 66.90%, False: 33.10%]
------------------
196| 1.32k| x = apply_2(vec[top], x);
197| 1.32k| }
198| 653| wt.uw(x);
199| 653| }
200| 1.32k| }
201| 1| return 0;
202| 1|}