/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.21k| auto operator+(const Node &t) const -> Node {
13| 1.21k| u32 x = u64(a) * t.a % P;
14| 1.21k| u32 y = (u64(a) * t.b + b) % P;
15| 1.21k| u32 z = (u64(t.a) * c + t.c) % P;
16| 1.21k| return {x, y, z};
17| 1.21k| }
18| |} node[N * 2];
19| 2.00k|auto operator+(const Node &l, u32 r) -> u32 { return (u64(l.a) * r + l.b) % P; }
20| 2.23k|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| 1.21k|def build_step_1(int u, int p) -> void {
37| 1.21k| size[u] = 1;
38| 3.64k| for (int e = head[u]; e; e = edge[e].next) {
^2.43k
------------------
| Branch (38:25): [True: 66.65%, False: 33.35%]
------------------
39| 2.43k| int v = edge[e].to;
40| 2.43k| if (v != p) {
------------------
| Branch (40:9): [True: 50.00%, False: 50.00%]
------------------
41| 1.21k| build_step_1(v, u);
42| 1.21k| size[u] += size[v];
43| 1.21k| if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
^593
------------------
| Branch (43:11): [True: 51.23%, False: 48.77%]
| Branch (43:28): [True: 47.05%, False: 52.95%]
------------------
44| 902| heavy[u] = v;
45| 902| }
46| 1.21k| }
47| 2.43k| }
48| 1.21k|}
49| |
50| 1.21k|def build_step_2(int u, int w, int p, int d) -> void {
51| 1.21k| int i = id++;
52| 1.21k| node2id[u] = i;
53| 1.21k| depth[i] = d;
54| 1.21k| int anc = ances[i] = node2id[w];
55| 1.21k| parent[i] = node2id[p];
56| 1.21k| node[i * 2 + 1] = {a[u], b[u], b[u]};
57| 1.21k| let a = node + anc * 2;
58| 1.21k| int k = (i - anc) * 2 + 1;
59| 1.73k| for (int j = 1; k & (j * 2); k -= j, j *= 2) {
^519
------------------
| Branch (59:19): [True: 29.90%, False: 70.10%]
------------------
60| 519| a[k - j] = a[k - j * 2] + a[k];
61| 519| }
62| 1.21k| if (int v = heavy[u]; v) {
------------------
| Branch (62:25): [True: 51.19%, False: 48.81%]
------------------
63| 623| build_step_2(v, w, u, d + 1);
64| 623| }
65| 3.64k| for (int e = head[u]; e; e = edge[e].next) {
^2.43k
------------------
| Branch (65:25): [True: 66.65%, False: 33.35%]
------------------
66| 2.43k| int v = edge[e].to;
67| 2.43k| if (v != p && v != heavy[u]) {
^1.21k
------------------
| Branch (67:9): [True: 50.00%, False: 50.00%]
| Branch (67:19): [True: 48.77%, False: 51.23%]
------------------
68| 593| build_step_2(v, v, u, d + 1);
69| 593| }
70| 2.43k| }
71| 1.21k|}
72| |
73| 1.27k|def apply_1(int i, u32 x) -> u32 {
74| 1.27k| int anc = ances[i];
75| 1.27k| let a = node + anc * 2;
76| 1.27k| int r = (i - anc) * 2 + 2;
77| 2.81k| for (; r; r -= r & -r) {
^1.54k
------------------
| Branch (77:10): [True: 54.80%, False: 45.20%]
------------------
78| 1.54k| x = a[r - (r & -r) / 2] + x;
79| 1.54k| }
80| 1.27k| return x;
81| 1.27k|}
82| |
83| 1.28k|def apply_2(int i, u32 x) -> u32 {
84| 1.28k| int anc = ances[i];
85| 1.28k| let a = node + anc * 2;
86| 1.28k| int r = (i - anc) * 2 + 2;
87| 2.86k| for (int k = 0; k < r;) {
------------------
| Branch (87:19): [True: 55.01%, False: 44.99%]
------------------
88| 1.57k| int t = 1 << log(r - k);
89| 1.57k| x = x + a[k + t / 2];
90| 1.57k| k += t;
91| 1.57k| }
92| 1.28k| return x;
93| 1.28k|}
94| |
95| 221|def apply_1(int i, int j, u32 x) -> u32 {
96| 221| int anc = ances[i];
97| 221| if (i == anc) return apply_1(j, x);
^19
------------------
| Branch (97:7): [True: 8.60%, False: 91.40%]
------------------
98| 202| let a = node + anc * 2;
99| 202| int l = (i - anc) * 2 - 1;
100| 202| int r = (j - anc) * 2 + 3;
101| 202| int k = (-1 << log(l ^ r)) & r;
102| 447| for (--r; r > k; r -= r & -r) {
^245
------------------
| Branch (102:13): [True: 54.81%, False: 45.19%]
------------------
103| 245| x = a[r - (r & -r) / 2] + x;
104| 245| }
105| 425| for (++l; k > l;) {
------------------
| Branch (105:13): [True: 52.47%, False: 47.53%]
------------------
106| 223| int t = 1 << log(k - l);
107| 223| x = a[k - t / 2] + x;
108| 223| k -= t;
109| 223| }
110| 202| return x;
111| 221|}
112| |
113| 396|def apply_2(int i, int j, u32 x) -> u32 {
114| 396| int anc = ances[i];
115| 396| if (i == anc) return apply_2(j, x);
^41
------------------
| Branch (115:7): [True: 10.35%, False: 89.65%]
------------------
116| 355| let a = node + anc * 2;
117| 355| int l = (i - anc) * 2 - 1;
118| 355| int r = (j - anc) * 2 + 3;
119| 355| int k = (-1 << log(l ^ r)) & r;
120| 628| for (++l; l < k; l += l & -l) {
^273
------------------
| Branch (120:13): [True: 43.47%, False: 56.53%]
------------------
121| 273| x = x + a[l + (l & -l) / 2];
122| 273| }
123| 744| for (--r; k < r;) {
------------------
| Branch (123:13): [True: 52.28%, False: 47.72%]
------------------
124| 389| int t = 1 << log(r - k);
125| 389| x = x + a[k + t / 2];
126| 389| k += t;
127| 389| }
128| 355| return x;
129| 396|}
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| 1.21k| for (int i = 0; i < n; ++i) a[i] = rd.uw(), b[i] = rd.uw();
^1.21k^1.21k
------------------
| Branch (143:19): [True: 99.92%, False: 0.08%]
------------------
144| 1.21k| for (int i = 1; i < n; ++i) {
^1.21k
------------------
| Branch (144:19): [True: 99.92%, False: 0.08%]
------------------
145| 1.21k| int u = rd.uh();
146| 1.21k| int v = rd.uh();
147| 1.21k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
148| 1.21k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
149| 1.21k| }
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| 1.21k| for (int i = 0; i < n; ++i) size[ances[i]]++;
^1.21k^1.21k
------------------
| Branch (153:19): [True: 99.92%, False: 0.08%]
------------------
154| 1.26k| while (q--) {
------------------
| Branch (154:10): [True: 99.92%, False: 0.08%]
------------------
155| 1.26k| let t = rd.u1();
156| 1.26k| if (t == 0) {
------------------
| Branch (156:9): [True: 51.15%, False: 48.85%]
------------------
157| 646| int i = node2id[rd.uh()];
158| 646| u32 c = rd.uw();
159| 646| u32 d = rd.uw();
160| 646| int anc = ances[i];
161| 646| let a = node + anc * 2;
162| 646| int n = size[anc];
163| 646| int k = (i - anc) * 2 + 1;
164| 646| a[k] = {c, d, d};
165| 1.34k| for (int j = 1;; j *= 2) {
^697
166| 1.34k| if (k & (j * 2)) {
------------------
| Branch (166:13): [True: 27.03%, False: 72.97%]
------------------
167| 363| a[k - j] = a[k - j * 2] + a[k];
168| 363| k -= j;
169| 980| } else {
170| 980| if (k + j * 3 > n * 2) break;
^646
------------------
| Branch (170:15): [True: 65.92%, False: 34.08%]
------------------
171| 334| a[k + j] = a[k] + a[k + j * 2];
172| 334| k += j;
173| 334| }
174| 1.34k| }
175| 646| }
176| 1.26k| if (t == 1) {
------------------
| Branch (176:9): [True: 48.85%, False: 51.15%]
------------------
177| 617| int u = node2id[rd.uh()];
178| 617| int v = node2id[rd.uh()];
179| 617| u32 x = rd.uw();
180| 617| int vec[20], top{};
181| 3.11k| while (ances[u] != ances[v]) {
------------------
| Branch (181:14): [True: 80.20%, False: 19.80%]
------------------
182| 2.49k| if (depth[ances[u]] > depth[ances[v]]) {
------------------
| Branch (182:13): [True: 50.10%, False: 49.90%]
------------------
183| 1.25k| x = apply_1(u, x);
184| 1.25k| u = parent[ances[u]];
185| 1.25k| } else {
186| 1.24k| vec[top++] = v;
187| 1.24k| v = parent[ances[v]];
188| 1.24k| }
189| 2.49k| }
190| 617| if (u > v) {
------------------
| Branch (190:11): [True: 35.82%, False: 64.18%]
------------------
191| 221| x = apply_1(v, u, x);
192| 396| } else {
193| 396| x = apply_2(u, v, x);
194| 396| }
195| 1.86k| while (top--) {
------------------
| Branch (195:14): [True: 66.90%, False: 33.10%]
------------------
196| 1.24k| x = apply_2(vec[top], x);
197| 1.24k| }
198| 617| wt.uw(x);
199| 617| }
200| 1.26k| }
201| 1| return 0;
202| 1|}