/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| 592k|auto operator+(const Node &l, u32 r) -> u32 { return (u64(l.a) * r + l.b) % P; }
20| 609k|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.6k
------------------
| Branch (43:11): [True: 50.18%, False: 49.82%]
| Branch (43:28): [True: 46.84%, False: 53.16%]
------------------
44| 147k| heavy[u] = v;
45| 147k| }
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.7k
------------------
| Branch (59:19): [True: 29.01%, False: 70.99%]
------------------
60| 81.7k| a[k - j] = a[k - j * 2] + a[k];
61| 81.7k| }
62| 200k| if (int v = heavy[u]; v) {
------------------
| Branch (62:25): [True: 50.18%, False: 49.82%]
------------------
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.82%, False: 50.18%]
------------------
68| 99.6k| build_step_2(v, v, u, d + 1);
69| 99.6k| }
70| 399k| }
71| 200k|}
72| |
73| 408k|def apply_1(int i, u32 x) -> u32 {
74| 408k| int anc = ances[i];
75| 408k| let a = node + anc * 2;
76| 408k| int r = (i - anc) * 2 + 2;
77| 918k| for (; r; r -= r & -r) {
^510k
------------------
| Branch (77:10): [True: 55.57%, False: 44.43%]
------------------
78| 510k| x = a[r - (r & -r) / 2] + x;
79| 510k| }
80| 408k| return x;
81| 408k|}
82| |
83| 409k|def apply_2(int i, u32 x) -> u32 {
84| 409k| int anc = ances[i];
85| 409k| let a = node + anc * 2;
86| 409k| int r = (i - anc) * 2 + 2;
87| 921k| for (int k = 0; k < r;) {
------------------
| Branch (87:19): [True: 55.55%, False: 44.45%]
------------------
88| 511k| int t = 1 << log(r - k);
89| 511k| x = x + a[k + t / 2];
90| 511k| k += t;
91| 511k| }
92| 409k| return x;
93| 409k|}
94| |
95| 41.2k|def apply_1(int i, int j, u32 x) -> u32 {
96| 41.2k| int anc = ances[i];
97| 41.2k| if (i == anc) return apply_1(j, x);
^3.30k
------------------
| Branch (97:7): [True: 8.00%, False: 92.00%]
------------------
98| 37.9k| let a = node + anc * 2;
99| 37.9k| int l = (i - anc) * 2 - 1;
100| 37.9k| int r = (j - anc) * 2 + 3;
101| 37.9k| int k = (-1 << log(l ^ r)) & r;
102| 88.6k| for (--r; r > k; r -= r & -r) {
^50.6k
------------------
| Branch (102:13): [True: 57.17%, False: 42.83%]
------------------
103| 50.6k| x = a[r - (r & -r) / 2] + x;
104| 50.6k| }
105| 68.7k| for (++l; k > l;) {
------------------
| Branch (105:13): [True: 44.82%, False: 55.18%]
------------------
106| 30.8k| int t = 1 << log(k - l);
107| 30.8k| x = a[k - t / 2] + x;
108| 30.8k| k -= t;
109| 30.8k| }
110| 37.9k| return x;
111| 41.2k|}
112| |
113| 58.7k|def apply_2(int i, int j, u32 x) -> u32 {
114| 58.7k| int anc = ances[i];
115| 58.7k| if (i == anc) return apply_2(j, x);
^4.82k
------------------
| Branch (115:7): [True: 8.21%, False: 91.79%]
------------------
116| 53.9k| let a = node + anc * 2;
117| 53.9k| int l = (i - anc) * 2 - 1;
118| 53.9k| int r = (j - anc) * 2 + 3;
119| 53.9k| int k = (-1 << log(l ^ r)) & r;
120| 85.7k| for (++l; l < k; l += l & -l) {
^31.8k
------------------
| Branch (120:13): [True: 37.16%, False: 62.84%]
------------------
121| 31.8k| x = x + a[l + (l & -l) / 2];
122| 31.8k| }
123| 119k| for (--r; k < r;) {
------------------
| Branch (123:13): [True: 54.78%, False: 45.22%]
------------------
124| 65.2k| int t = 1 << log(r - k);
125| 65.2k| x = x + a[k + t / 2];
126| 65.2k| k += t;
127| 65.2k| }
128| 53.9k| return x;
129| 58.7k|}
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.01%, False: 49.99%]
------------------
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| 202k| for (int j = 1;; j *= 2) {
^102k
166| 202k| if (k & (j * 2)) {
------------------
| Branch (166:13): [True: 25.54%, False: 74.46%]
------------------
167| 51.8k| a[k - j] = a[k - j * 2] + a[k];
168| 51.8k| k -= j;
169| 151k| } else {
170| 151k| if (k + j * 3 > n * 2) break;
^100k
------------------
| Branch (170:15): [True: 66.19%, False: 33.81%]
------------------
171| 51.0k| a[k + j] = a[k] + a[k + j * 2];
172| 51.0k| k += j;
173| 51.0k| }
174| 202k| }
175| 100k| }
176| 200k| if (t == 1) {
------------------
| Branch (176:9): [True: 49.99%, False: 50.01%]
------------------
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| 909k| while (ances[u] != ances[v]) {
------------------
| Branch (181:14): [True: 89.01%, False: 10.99%]
------------------
182| 809k| if (depth[ances[u]] > depth[ances[v]]) {
------------------
| Branch (182:13): [True: 50.01%, False: 49.99%]
------------------
183| 404k| x = apply_1(u, x);
184| 404k| u = parent[ances[u]];
185| 404k| } else {
186| 404k| vec[top++] = v;
187| 404k| v = parent[ances[v]];
188| 404k| }
189| 809k| }
190| 99.9k| if (u > v) {
------------------
| Branch (190:11): [True: 41.27%, False: 58.73%]
------------------
191| 41.2k| x = apply_1(v, u, x);
192| 58.7k| } else {
193| 58.7k| x = apply_2(u, v, x);
194| 58.7k| }
195| 504k| while (top--) {
------------------
| Branch (195:14): [True: 80.19%, False: 19.81%]
------------------
196| 404k| x = apply_2(vec[top], x);
197| 404k| }
198| 99.9k| wt.uw(x);
199| 99.9k| }
200| 200k| }
201| 1| return 0;
202| 1|}