/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| 184k| auto operator+(const Node &t) const -> Node {
13| 184k| u32 x = u64(a) * t.a % P;
14| 184k| u32 y = (u64(a) * t.b + b) % P;
15| 184k| u32 z = (u64(t.a) * c + t.c) % P;
16| 184k| return {x, y, z};
17| 184k| }
18| |} node[N * 2];
19| 587k|auto operator+(const Node &l, u32 r) -> u32 { return (u64(l.a) * r + l.b) % P; }
20| 613k|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]]) {
^99.9k
------------------
| Branch (43:11): [True: 50.02%, False: 49.98%]
| Branch (43:28): [True: 46.70%, False: 53.30%]
------------------
44| 146k| heavy[u] = v;
45| 146k| }
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| 281k| for (int j = 1; k & (j * 2); k -= j, j *= 2) {
^81.6k
------------------
| Branch (59:19): [True: 29.00%, False: 71.00%]
------------------
60| 81.6k| a[k - j] = a[k - j * 2] + a[k];
61| 81.6k| }
62| 200k| if (int v = heavy[u]; v) {
------------------
| Branch (62:25): [True: 50.02%, False: 49.98%]
------------------
63| 100k| build_step_2(v, w, u, d + 1);
64| 100k| }
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: 49.98%, False: 50.02%]
------------------
68| 99.9k| build_step_2(v, v, u, d + 1);
69| 99.9k| }
70| 399k| }
71| 200k|}
72| |
73| 411k|def apply_1(int i, u32 x) -> u32 {
74| 411k| int anc = ances[i];
75| 411k| let a = node + anc * 2;
76| 411k| int r = (i - anc) * 2 + 2;
77| 926k| for (; r; r -= r & -r) {
^514k
------------------
| Branch (77:10): [True: 55.53%, False: 44.47%]
------------------
78| 514k| x = a[r - (r & -r) / 2] + x;
79| 514k| }
80| 411k| return x;
81| 411k|}
82| |
83| 412k|def apply_2(int i, u32 x) -> u32 {
84| 412k| int anc = ances[i];
85| 412k| let a = node + anc * 2;
86| 412k| int r = (i - anc) * 2 + 2;
87| 928k| for (int k = 0; k < r;) {
------------------
| Branch (87:19): [True: 55.53%, False: 44.47%]
------------------
88| 515k| int t = 1 << log(r - k);
89| 515k| x = x + a[k + t / 2];
90| 515k| k += t;
91| 515k| }
92| 412k| return x;
93| 412k|}
94| |
95| 37.0k|def apply_1(int i, int j, u32 x) -> u32 {
96| 37.0k| int anc = ances[i];
97| 37.0k| if (i == anc) return apply_1(j, x);
^1.91k
------------------
| Branch (97:7): [True: 5.16%, False: 94.84%]
------------------
98| 35.1k| let a = node + anc * 2;
99| 35.1k| int l = (i - anc) * 2 - 1;
100| 35.1k| int r = (j - anc) * 2 + 3;
101| 35.1k| int k = (-1 << log(l ^ r)) & r;
102| 85.2k| for (--r; r > k; r -= r & -r) {
^50.0k
------------------
| Branch (102:13): [True: 58.73%, False: 41.27%]
------------------
103| 50.0k| x = a[r - (r & -r) / 2] + x;
104| 50.0k| }
105| 57.8k| for (++l; k > l;) {
------------------
| Branch (105:13): [True: 39.14%, False: 60.86%]
------------------
106| 22.6k| int t = 1 << log(k - l);
107| 22.6k| x = a[k - t / 2] + x;
108| 22.6k| k -= t;
109| 22.6k| }
110| 35.1k| return x;
111| 37.0k|}
112| |
113| 63.0k|def apply_2(int i, int j, u32 x) -> u32 {
114| 63.0k| int anc = ances[i];
115| 63.0k| if (i == anc) return apply_2(j, x);
^2.83k
------------------
| Branch (115:7): [True: 4.49%, False: 95.51%]
------------------
116| 60.2k| let a = node + anc * 2;
117| 60.2k| int l = (i - anc) * 2 - 1;
118| 60.2k| int r = (j - anc) * 2 + 3;
119| 60.2k| int k = (-1 << log(l ^ r)) & r;
120| 83.5k| for (++l; l < k; l += l & -l) {
^23.3k
------------------
| Branch (120:13): [True: 27.90%, False: 72.10%]
------------------
121| 23.3k| x = x + a[l + (l & -l) / 2];
122| 23.3k| }
123| 134k| for (--r; k < r;) {
------------------
| Branch (123:13): [True: 55.30%, False: 44.70%]
------------------
124| 74.5k| int t = 1 << log(r - k);
125| 74.5k| x = x + a[k + t / 2];
126| 74.5k| k += t;
127| 74.5k| }
128| 60.2k| return x;
129| 63.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: 49.91%, False: 50.09%]
------------------
157| 99.8k| int i = node2id[rd.uh()];
158| 99.8k| u32 c = rd.uw();
159| 99.8k| u32 d = rd.uw();
160| 99.8k| int anc = ances[i];
161| 99.8k| let a = node + anc * 2;
162| 99.8k| int n = size[anc];
163| 99.8k| int k = (i - anc) * 2 + 1;
164| 99.8k| a[k] = {c, d, d};
165| 202k| for (int j = 1;; j *= 2) {
^102k
166| 202k| if (k & (j * 2)) {
------------------
| Branch (166:13): [True: 25.27%, False: 74.73%]
------------------
167| 51.1k| a[k - j] = a[k - j * 2] + a[k];
168| 51.1k| k -= j;
169| 151k| } else {
170| 151k| if (k + j * 3 > n * 2) break;
^99.8k
------------------
| Branch (170:15): [True: 66.05%, False: 33.95%]
------------------
171| 51.3k| a[k + j] = a[k] + a[k + j * 2];
172| 51.3k| k += j;
173| 51.3k| }
174| 202k| }
175| 99.8k| }
176| 200k| if (t == 1) {
------------------
| Branch (176:9): [True: 50.09%, False: 49.91%]
------------------
177| 100k| int u = node2id[rd.uh()];
178| 100k| int v = node2id[rd.uh()];
179| 100k| u32 x = rd.uw();
180| 100k| int vec[20], top{};
181| 920k| while (ances[u] != ances[v]) {
------------------
| Branch (181:14): [True: 89.11%, False: 10.89%]
------------------
182| 820k| if (depth[ances[u]] > depth[ances[v]]) {
------------------
| Branch (182:13): [True: 50.00%, False: 50.00%]
------------------
183| 410k| x = apply_1(u, x);
184| 410k| u = parent[ances[u]];
185| 410k| } else {
186| 410k| vec[top++] = v;
187| 410k| v = parent[ances[v]];
188| 410k| }
189| 820k| }
190| 100k| if (u > v) {
------------------
| Branch (190:11): [True: 37.02%, False: 62.98%]
------------------
191| 37.0k| x = apply_1(v, u, x);
192| 63.0k| } else {
193| 63.0k| x = apply_2(u, v, x);
194| 63.0k| }
195| 510k| while (top--) {
------------------
| Branch (195:14): [True: 80.36%, False: 19.64%]
------------------
196| 410k| x = apply_2(vec[top], x);
197| 410k| }
198| 100k| wt.uw(x);
199| 100k| }
200| 200k| }
201| 1| return 0;
202| 1|}