/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| 5| auto operator+(const Node &t) const -> Node {
13| 5| u32 x = u64(a) * t.a % P;
14| 5| u32 y = (u64(a) * t.b + b) % P;
15| 5| u32 z = (u64(t.a) * c + t.c) % P;
16| 5| return {x, y, z};
17| 5| }
18| |} node[N * 2];
19| 0|auto operator+(const Node &l, u32 r) -> u32 { return (u64(l.a) * r + l.b) % P; }
20| 6|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| 5|def build_step_1(int u, int p) -> void {
37| 5| size[u] = 1;
38| 13| for (int e = head[u]; e; e = edge[e].next) {
^8
------------------
| Branch (38:25): [True: 61.54%, False: 38.46%]
------------------
39| 8| int v = edge[e].to;
40| 8| if (v != p) {
------------------
| Branch (40:9): [True: 50.00%, False: 50.00%]
------------------
41| 4| build_step_1(v, u);
42| 4| size[u] += size[v];
43| 4| if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
^1
------------------
| Branch (43:11): [True: 75.00%, False: 25.00%]
| Branch (43:28): [True: 0.00%, False: 100.00%]
------------------
44| 3| heavy[u] = v;
45| 3| }
46| 4| }
47| 8| }
48| 5|}
49| |
50| 5|def build_step_2(int u, int w, int p, int d) -> void {
51| 5| int i = id++;
52| 5| node2id[u] = i;
53| 5| depth[i] = d;
54| 5| int anc = ances[i] = node2id[w];
55| 5| parent[i] = node2id[p];
56| 5| node[i * 2 + 1] = {a[u], b[u], b[u]};
57| 5| let a = node + anc * 2;
58| 5| int k = (i - anc) * 2 + 1;
59| 8| for (int j = 1; k & (j * 2); k -= j, j *= 2) {
^3
------------------
| Branch (59:19): [True: 37.50%, False: 62.50%]
------------------
60| 3| a[k - j] = a[k - j * 2] + a[k];
61| 3| }
62| 5| if (int v = heavy[u]; v) {
------------------
| Branch (62:25): [True: 60.00%, False: 40.00%]
------------------
63| 3| build_step_2(v, w, u, d + 1);
64| 3| }
65| 13| for (int e = head[u]; e; e = edge[e].next) {
^8
------------------
| Branch (65:25): [True: 61.54%, False: 38.46%]
------------------
66| 8| int v = edge[e].to;
67| 8| if (v != p && v != heavy[u]) {
^4
------------------
| Branch (67:9): [True: 50.00%, False: 50.00%]
| Branch (67:19): [True: 25.00%, False: 75.00%]
------------------
68| 1| build_step_2(v, v, u, d + 1);
69| 1| }
70| 8| }
71| 5|}
72| |
73| 0|def apply_1(int i, u32 x) -> u32 {
74| 0| int anc = ances[i];
75| 0| let a = node + anc * 2;
76| 0| int r = (i - anc) * 2 + 2;
77| 0| for (; r; r -= r & -r) {
------------------
| Branch (77:10): [True: 0.00%, False: 0.00%]
------------------
78| 0| x = a[r - (r & -r) / 2] + x;
79| 0| }
80| 0| return x;
81| 0|}
82| |
83| 3|def apply_2(int i, u32 x) -> u32 {
84| 3| int anc = ances[i];
85| 3| let a = node + anc * 2;
86| 3| int r = (i - anc) * 2 + 2;
87| 7| for (int k = 0; k < r;) {
------------------
| Branch (87:19): [True: 57.14%, False: 42.86%]
------------------
88| 4| int t = 1 << log(r - k);
89| 4| x = x + a[k + t / 2];
90| 4| k += t;
91| 4| }
92| 3| return x;
93| 3|}
94| |
95| 0|def apply_1(int i, int j, u32 x) -> u32 {
96| 0| int anc = ances[i];
97| 0| if (i == anc) return apply_1(j, x);
------------------
| Branch (97:7): [True: 0.00%, False: 0.00%]
------------------
98| 0| let a = node + anc * 2;
99| 0| int l = (i - anc) * 2 - 1;
100| 0| int r = (j - anc) * 2 + 3;
101| 0| int k = (-1 << log(l ^ r)) & r;
102| 0| for (--r; r > k; r -= r & -r) {
------------------
| Branch (102:13): [True: 0.00%, False: 0.00%]
------------------
103| 0| x = a[r - (r & -r) / 2] + x;
104| 0| }
105| 0| for (++l; k > l;) {
------------------
| Branch (105:13): [True: 0.00%, False: 0.00%]
------------------
106| 0| int t = 1 << log(k - l);
107| 0| x = a[k - t / 2] + x;
108| 0| k -= t;
109| 0| }
110| 0| return x;
111| 0|}
112| |
113| 4|def apply_2(int i, int j, u32 x) -> u32 {
114| 4| int anc = ances[i];
115| 4| if (i == anc) return apply_2(j, x);
^2
------------------
| Branch (115:7): [True: 50.00%, False: 50.00%]
------------------
116| 2| let a = node + anc * 2;
117| 2| int l = (i - anc) * 2 - 1;
118| 2| int r = (j - anc) * 2 + 3;
119| 2| int k = (-1 << log(l ^ r)) & r;
120| 3| for (++l; l < k; l += l & -l) {
^1
------------------
| Branch (120:13): [True: 33.33%, False: 66.67%]
------------------
121| 1| x = x + a[l + (l & -l) / 2];
122| 1| }
123| 3| for (--r; k < r;) {
------------------
| Branch (123:13): [True: 33.33%, False: 66.67%]
------------------
124| 1| int t = 1 << log(r - k);
125| 1| x = x + a[k + t / 2];
126| 1| k += t;
127| 1| }
128| 2| return x;
129| 4|}
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| 6| for (int i = 0; i < n; ++i) a[i] = rd.uw(), b[i] = rd.uw();
^5 ^5
------------------
| Branch (143:19): [True: 83.33%, False: 16.67%]
------------------
144| 5| for (int i = 1; i < n; ++i) {
^4
------------------
| Branch (144:19): [True: 80.00%, False: 20.00%]
------------------
145| 4| int u = rd.uh();
146| 4| int v = rd.uh();
147| 4| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
148| 4| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
149| 4| }
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| 6| for (int i = 0; i < n; ++i) size[ances[i]]++;
^5 ^5
------------------
| Branch (153:19): [True: 83.33%, False: 16.67%]
------------------
154| 6| while (q--) {
------------------
| Branch (154:10): [True: 83.33%, False: 16.67%]
------------------
155| 5| let t = rd.u1();
156| 5| if (t == 0) {
------------------
| Branch (156:9): [True: 20.00%, False: 80.00%]
------------------
157| 1| int i = node2id[rd.uh()];
158| 1| u32 c = rd.uw();
159| 1| u32 d = rd.uw();
160| 1| int anc = ances[i];
161| 1| let a = node + anc * 2;
162| 1| int n = size[anc];
163| 1| int k = (i - anc) * 2 + 1;
164| 1| a[k] = {c, d, d};
165| 3| for (int j = 1;; j *= 2) {
^2
166| 3| if (k & (j * 2)) {
------------------
| Branch (166:13): [True: 33.33%, False: 66.67%]
------------------
167| 1| a[k - j] = a[k - j * 2] + a[k];
168| 1| k -= j;
169| 2| } else {
170| 2| if (k + j * 3 > n * 2) break;
^1
------------------
| Branch (170:15): [True: 50.00%, False: 50.00%]
------------------
171| 1| a[k + j] = a[k] + a[k + j * 2];
172| 1| k += j;
173| 1| }
174| 3| }
175| 1| }
176| 5| if (t == 1) {
------------------
| Branch (176:9): [True: 80.00%, False: 20.00%]
------------------
177| 4| int u = node2id[rd.uh()];
178| 4| int v = node2id[rd.uh()];
179| 4| u32 x = rd.uw();
180| 4| int vec[20], top{};
181| 5| while (ances[u] != ances[v]) {
------------------
| Branch (181:14): [True: 20.00%, False: 80.00%]
------------------
182| 1| if (depth[ances[u]] > depth[ances[v]]) {
------------------
| Branch (182:13): [True: 0.00%, False: 100.00%]
------------------
183| 0| x = apply_1(u, x);
184| 0| u = parent[ances[u]];
185| 1| } else {
186| 1| vec[top++] = v;
187| 1| v = parent[ances[v]];
188| 1| }
189| 1| }
190| 4| if (u > v) {
------------------
| Branch (190:11): [True: 0.00%, False: 100.00%]
------------------
191| 0| x = apply_1(v, u, x);
192| 4| } else {
193| 4| x = apply_2(u, v, x);
194| 4| }
195| 5| while (top--) {
------------------
| Branch (195:14): [True: 20.00%, False: 80.00%]
------------------
196| 1| x = apply_2(vec[top], x);
197| 1| }
198| 4| wt.uw(x);
199| 4| }
200| 5| }
201| 1| return 0;
202| 1|}