/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| 207k| auto operator+(const Node &t) const -> Node {
13| 207k| u32 x = u64(a) * t.a % P;
14| 207k| u32 y = (u64(a) * t.b + b) % P;
15| 207k| u32 z = (u64(t.a) * c + t.c) % P;
16| 207k| return {x, y, z};
17| 207k| }
18| |} node[N * 2];
19| 6.71M|auto operator+(const Node &l, u32 r) -> u32 { return (u64(l.a) * r + l.b) % P; }
20| 6.72M|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| 200k|def build_step_1(int u, int p) -> void {
37| 200k| size[u] = 1;
38| 599k| for (int e = head[u]; e; e = edge[e].next) {
^399k
------------------
| Branch (38:25): [True: 66.67%, False: 33.33%]
------------------
39| 399k| int v = edge[e].to;
40| 399k| if (v != p) {
------------------
| Branch (40:9): [True: 50.00%, False: 50.00%]
------------------
41| 199k| build_step_1(v, u);
42| 199k| size[u] += size[v];
43| 199k| if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
^32.7k
------------------
| Branch (43:11): [True: 83.62%, False: 16.38%]
| Branch (43:28): [True: 18.21%, False: 81.79%]
------------------
44| 173k| heavy[u] = v;
45| 173k| }
46| 199k| }
47| 399k| }
48| 200k|}
49| |
50| 200k|def build_step_2(int u, int w, int p, int d) -> void {
51| 200k| int i = id++;
52| 200k| node2id[u] = i;
53| 200k| depth[i] = d;
54| 200k| int anc = ances[i] = node2id[w];
55| 200k| parent[i] = node2id[p];
56| 200k| node[i * 2 + 1] = {a[u], b[u], b[u]};
57| 200k| let a = node + anc * 2;
58| 200k| int k = (i - anc) * 2 + 1;
59| 356k| for (int j = 1; k & (j * 2); k -= j, j *= 2) {
^156k
------------------
| Branch (59:19): [True: 43.95%, False: 56.05%]
------------------
60| 156k| a[k - j] = a[k - j * 2] + a[k];
61| 156k| }
62| 200k| if (int v = heavy[u]; v) {
------------------
| Branch (62:25): [True: 83.62%, False: 16.38%]
------------------
63| 167k| build_step_2(v, w, u, d + 1);
64| 167k| }
65| 599k| for (int e = head[u]; e; e = edge[e].next) {
^399k
------------------
| Branch (65:25): [True: 66.67%, False: 33.33%]
------------------
66| 399k| int v = edge[e].to;
67| 399k| if (v != p && v != heavy[u]) {
^199k
------------------
| Branch (67:9): [True: 50.00%, False: 50.00%]
| Branch (67:19): [True: 16.38%, False: 83.62%]
------------------
68| 32.7k| build_step_2(v, v, u, d + 1);
69| 32.7k| }
70| 399k| }
71| 200k|}
72| |
73| 1.42M|def apply_1(int i, u32 x) -> u32 {
74| 1.42M| int anc = ances[i];
75| 1.42M| let a = node + anc * 2;
76| 1.42M| int r = (i - anc) * 2 + 2;
77| 6.37M| for (; r; r -= r & -r) {
^4.94M
------------------
| Branch (77:10): [True: 77.57%, False: 22.43%]
------------------
78| 4.94M| x = a[r - (r & -r) / 2] + x;
79| 4.94M| }
80| 1.42M| return x;
81| 1.42M|}
82| |
83| 1.42M|def apply_2(int i, u32 x) -> u32 {
84| 1.42M| int anc = ances[i];
85| 1.42M| let a = node + anc * 2;
86| 1.42M| int r = (i - anc) * 2 + 2;
87| 6.37M| for (int k = 0; k < r;) {
------------------
| Branch (87:19): [True: 77.59%, False: 22.41%]
------------------
88| 4.94M| int t = 1 << log(r - k);
89| 4.94M| x = x + a[k + t / 2];
90| 4.94M| k += t;
91| 4.94M| }
92| 1.42M| return x;
93| 1.42M|}
94| |
95| 95.1k|def apply_1(int i, int j, u32 x) -> u32 {
96| 95.1k| int anc = ances[i];
97| 95.1k| if (i == anc) return apply_1(j, x);
^0
------------------
| Branch (97:7): [True: 0.00%, False: 100.00%]
------------------
98| 95.1k| let a = node + anc * 2;
99| 95.1k| int l = (i - anc) * 2 - 1;
100| 95.1k| int r = (j - anc) * 2 + 3;
101| 95.1k| int k = (-1 << log(l ^ r)) & r;
102| 699k| for (--r; r > k; r -= r & -r) {
^604k
------------------
| Branch (102:13): [True: 86.40%, False: 13.60%]
------------------
103| 604k| x = a[r - (r & -r) / 2] + x;
104| 604k| }
105| 1.26M| for (++l; k > l;) {
------------------
| Branch (105:13): [True: 92.45%, False: 7.55%]
------------------
106| 1.16M| int t = 1 << log(k - l);
107| 1.16M| x = a[k - t / 2] + x;
108| 1.16M| k -= t;
109| 1.16M| }
110| 95.1k| return x;
111| 95.1k|}
112| |
113| 95.4k|def apply_2(int i, int j, u32 x) -> u32 {
114| 95.4k| int anc = ances[i];
115| 95.4k| if (i == anc) return apply_2(j, x);
^0
------------------
| Branch (115:7): [True: 0.00%, False: 100.00%]
------------------
116| 95.4k| let a = node + anc * 2;
117| 95.4k| int l = (i - anc) * 2 - 1;
118| 95.4k| int r = (j - anc) * 2 + 3;
119| 95.4k| int k = (-1 << log(l ^ r)) & r;
120| 1.26M| for (++l; l < k; l += l & -l) {
^1.16M
------------------
| Branch (120:13): [True: 92.45%, False: 7.55%]
------------------
121| 1.16M| x = x + a[l + (l & -l) / 2];
122| 1.16M| }
123| 703k| for (--r; k < r;) {
------------------
| Branch (123:13): [True: 86.43%, False: 13.57%]
------------------
124| 607k| int t = 1 << log(r - k);
125| 607k| x = x + a[k + t / 2];
126| 607k| k += t;
127| 607k| }
128| 95.4k| return x;
129| 95.4k|}
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| 200k| for (int i = 0; i < n; ++i) a[i] = rd.uw(), b[i] = rd.uw();
^200k^200k
------------------
| Branch (143:19): [True: 100.00%, False: 0.00%]
------------------
144| 200k| for (int i = 1; i < n; ++i) {
^199k
------------------
| Branch (144:19): [True: 100.00%, False: 0.00%]
------------------
145| 199k| int u = rd.uh();
146| 199k| int v = rd.uh();
147| 199k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
148| 199k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
149| 199k| }
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| 200k| for (int i = 0; i < n; ++i) size[ances[i]]++;
^200k^200k
------------------
| Branch (153:19): [True: 100.00%, False: 0.00%]
------------------
154| 200k| while (q--) {
------------------
| Branch (154:10): [True: 100.00%, False: 0.00%]
------------------
155| 200k| let t = rd.u1();
156| 200k| if (t == 0) {
------------------
| Branch (156:9): [True: 4.74%, False: 95.27%]
------------------
157| 9.47k| int i = node2id[rd.uh()];
158| 9.47k| u32 c = rd.uw();
159| 9.47k| u32 d = rd.uw();
160| 9.47k| int anc = ances[i];
161| 9.47k| let a = node + anc * 2;
162| 9.47k| int n = size[anc];
163| 9.47k| int k = (i - anc) * 2 + 1;
164| 9.47k| a[k] = {c, d, d};
165| 59.7k| for (int j = 1;; j *= 2) {
^50.2k
166| 59.7k| if (k & (j * 2)) {
------------------
| Branch (166:13): [True: 42.22%, False: 57.78%]
------------------
167| 25.2k| a[k - j] = a[k - j * 2] + a[k];
168| 25.2k| k -= j;
169| 34.4k| } else {
170| 34.4k| if (k + j * 3 > n * 2) break;
^9.47k
------------------
| Branch (170:15): [True: 27.45%, False: 72.55%]
------------------
171| 25.0k| a[k + j] = a[k] + a[k + j * 2];
172| 25.0k| k += j;
173| 25.0k| }
174| 59.7k| }
175| 9.47k| }
176| 200k| if (t == 1) {
------------------
| Branch (176:9): [True: 95.27%, False: 4.74%]
------------------
177| 190k| int u = node2id[rd.uh()];
178| 190k| int v = node2id[rd.uh()];
179| 190k| u32 x = rd.uw();
180| 190k| int vec[20], top{};
181| 3.04M| while (ances[u] != ances[v]) {
------------------
| Branch (181:14): [True: 93.75%, False: 6.25%]
------------------
182| 2.85M| if (depth[ances[u]] > depth[ances[v]]) {
------------------
| Branch (182:13): [True: 50.00%, False: 50.00%]
------------------
183| 1.42M| x = apply_1(u, x);
184| 1.42M| u = parent[ances[u]];
185| 1.42M| } else {
186| 1.42M| vec[top++] = v;
187| 1.42M| v = parent[ances[v]];
188| 1.42M| }
189| 2.85M| }
190| 190k| if (u > v) {
------------------
| Branch (190:11): [True: 49.91%, False: 50.09%]
------------------
191| 95.1k| x = apply_1(v, u, x);
192| 95.4k| } else {
193| 95.4k| x = apply_2(u, v, x);
194| 95.4k| }
195| 1.61M| while (top--) {
------------------
| Branch (195:14): [True: 88.24%, False: 11.76%]
------------------
196| 1.42M| x = apply_2(vec[top], x);
197| 1.42M| }
198| 190k| wt.uw(x);
199| 190k| }
200| 200k| }
201| 1| return 0;
202| 1|}