/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.72M| auto operator+(const Node &t) const -> Node {
13| 1.72M| u32 x = u64(a) * t.a % P;
14| 1.72M| u32 y = (u64(a) * t.b + b) % P;
15| 1.72M| u32 z = (u64(t.a) * c + t.c) % P;
16| 1.72M| return {x, y, z};
17| 1.72M| }
18| |} node[N * 2];
19| 768k|auto operator+(const Node &l, u32 r) -> u32 { return (u64(l.a) * r + l.b) % P; }
20| 766k|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]]) {
^1
------------------
| Branch (43:11): [True: 100.00%, False: 0.00%]
| Branch (43:28): [True: 0.00%, False: 100.00%]
------------------
44| 199k| heavy[u] = v;
45| 199k| }
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| 399k| for (int j = 1; k & (j * 2); k -= j, j *= 2) {
^199k
------------------
| Branch (59:19): [True: 50.00%, False: 50.00%]
------------------
60| 199k| a[k - j] = a[k - j * 2] + a[k];
61| 199k| }
62| 200k| if (int v = heavy[u]; v) {
------------------
| Branch (62:25): [True: 100.00%, False: 0.00%]
------------------
63| 199k| build_step_2(v, w, u, d + 1);
64| 199k| }
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: 0.00%, False: 100.00%]
------------------
68| 1| build_step_2(v, v, u, d + 1);
69| 1| }
70| 399k| }
71| 200k|}
72| |
73| 49.7k|def apply_1(int i, u32 x) -> u32 {
74| 49.7k| int anc = ances[i];
75| 49.7k| let a = node + anc * 2;
76| 49.7k| int r = (i - anc) * 2 + 2;
77| 453k| for (; r; r -= r & -r) {
^403k
------------------
| Branch (77:10): [True: 89.03%, False: 10.97%]
------------------
78| 403k| x = a[r - (r & -r) / 2] + x;
79| 403k| }
80| 49.7k| return x;
81| 49.7k|}
82| |
83| 49.7k|def apply_2(int i, u32 x) -> u32 {
84| 49.7k| int anc = ances[i];
85| 49.7k| let a = node + anc * 2;
86| 49.7k| int r = (i - anc) * 2 + 2;
87| 454k| for (int k = 0; k < r;) {
------------------
| Branch (87:19): [True: 89.06%, False: 10.94%]
------------------
88| 404k| int t = 1 << log(r - k);
89| 404k| x = x + a[k + t / 2];
90| 404k| k += t;
91| 404k| }
92| 49.7k| return x;
93| 49.7k|}
94| |
95| 49.9k|def apply_1(int i, int j, u32 x) -> u32 {
96| 49.9k| int anc = ances[i];
97| 49.9k| if (i == anc) return apply_1(j, x);
^24.7k
------------------
| Branch (97:7): [True: 49.52%, False: 50.48%]
------------------
98| 25.1k| let a = node + anc * 2;
99| 25.1k| int l = (i - anc) * 2 - 1;
100| 25.1k| int r = (j - anc) * 2 + 3;
101| 25.1k| int k = (-1 << log(l ^ r)) & r;
102| 203k| for (--r; r > k; r -= r & -r) {
^178k
------------------
| Branch (102:13): [True: 87.65%, False: 12.35%]
------------------
103| 178k| x = a[r - (r & -r) / 2] + x;
104| 178k| }
105| 211k| for (++l; k > l;) {
------------------
| Branch (105:13): [True: 88.08%, False: 11.92%]
------------------
106| 186k| int t = 1 << log(k - l);
107| 186k| x = a[k - t / 2] + x;
108| 186k| k -= t;
109| 186k| }
110| 25.1k| return x;
111| 49.9k|}
112| |
113| 50.0k|def apply_2(int i, int j, u32 x) -> u32 {
114| 50.0k| int anc = ances[i];
115| 50.0k| if (i == anc) return apply_2(j, x);
^25.0k
------------------
| Branch (115:7): [True: 50.02%, False: 49.98%]
------------------
116| 24.9k| let a = node + anc * 2;
117| 24.9k| int l = (i - anc) * 2 - 1;
118| 24.9k| int r = (j - anc) * 2 + 3;
119| 24.9k| int k = (-1 << log(l ^ r)) & r;
120| 209k| for (++l; l < k; l += l & -l) {
^184k
------------------
| Branch (120:13): [True: 88.07%, False: 11.93%]
------------------
121| 184k| x = x + a[l + (l & -l) / 2];
122| 184k| }
123| 202k| for (--r; k < r;) {
------------------
| Branch (123:13): [True: 87.63%, False: 12.37%]
------------------
124| 177k| int t = 1 << log(r - k);
125| 177k| x = x + a[k + t / 2];
126| 177k| k += t;
127| 177k| }
128| 24.9k| return x;
129| 50.0k|}
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: 50.04%, False: 49.96%]
------------------
157| 100k| int i = node2id[rd.uh()];
158| 100k| u32 c = rd.uw();
159| 100k| u32 d = rd.uw();
160| 100k| int anc = ances[i];
161| 100k| let a = node + anc * 2;
162| 100k| int n = size[anc];
163| 100k| int k = (i - anc) * 2 + 1;
164| 100k| a[k] = {c, d, d};
165| 1.62M| for (int j = 1;; j *= 2) {
^1.52M
166| 1.62M| if (k & (j * 2)) {
------------------
| Branch (166:13): [True: 46.92%, False: 53.08%]
------------------
167| 764k| a[k - j] = a[k - j * 2] + a[k];
168| 764k| k -= j;
169| 864k| } else {
170| 864k| if (k + j * 3 > n * 2) break;
^100k
------------------
| Branch (170:15): [True: 11.57%, False: 88.43%]
------------------
171| 764k| a[k + j] = a[k] + a[k + j * 2];
172| 764k| k += j;
173| 764k| }
174| 1.62M| }
175| 100k| }
176| 200k| if (t == 1) {
------------------
| Branch (176:9): [True: 49.96%, False: 50.04%]
------------------
177| 99.9k| int u = node2id[rd.uh()];
178| 99.9k| int v = node2id[rd.uh()];
179| 99.9k| u32 x = rd.uw();
180| 99.9k| int vec[20], top{};
181| 149k| while (ances[u] != ances[v]) {
------------------
| Branch (181:14): [True: 33.23%, False: 66.77%]
------------------
182| 49.7k| if (depth[ances[u]] > depth[ances[v]]) {
------------------
| Branch (182:13): [True: 50.30%, False: 49.70%]
------------------
183| 25.0k| x = apply_1(u, x);
184| 25.0k| u = parent[ances[u]];
185| 25.0k| } else {
186| 24.7k| vec[top++] = v;
187| 24.7k| v = parent[ances[v]];
188| 24.7k| }
189| 49.7k| }
190| 99.9k| if (u > v) {
------------------
| Branch (190:11): [True: 49.95%, False: 50.05%]
------------------
191| 49.9k| x = apply_1(v, u, x);
192| 50.0k| } else {
193| 50.0k| x = apply_2(u, v, x);
194| 50.0k| }
195| 124k| while (top--) {
------------------
| Branch (195:14): [True: 19.83%, False: 80.17%]
------------------
196| 24.7k| x = apply_2(vec[top], x);
197| 24.7k| }
198| 99.9k| wt.uw(x);
199| 99.9k| }
200| 200k| }
201| 1| return 0;
202| 1|}