/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| 83.9k| auto operator+(const Node &t) const -> Node {
13| 83.9k| u32 x = u64(a) * t.a % P;
14| 83.9k| u32 y = (u64(a) * t.b + b) % P;
15| 83.9k| u32 z = (u64(t.a) * c + t.c) % P;
16| 83.9k| return {x, y, z};
17| 83.9k| }
18| |} node[N * 2];
19| 321k|auto operator+(const Node &l, u32 r) -> u32 { return (u64(l.a) * r + l.b) % P; }
20| 331k|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| 53.3k|def build_step_1(int u, int p) -> void {
37| 53.3k| size[u] = 1;
38| 160k| for (int e = head[u]; e; e = edge[e].next) {
^106k
------------------
| Branch (38:25): [True: 66.67%, False: 33.33%]
------------------
39| 106k| int v = edge[e].to;
40| 106k| if (v != p) {
------------------
| Branch (40:9): [True: 50.00%, False: 50.00%]
------------------
41| 53.3k| build_step_1(v, u);
42| 53.3k| size[u] += size[v];
43| 53.3k| if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
^26.6k
------------------
| Branch (43:11): [True: 49.99%, False: 50.01%]
| Branch (43:28): [True: 46.69%, False: 53.31%]
------------------
44| 39.1k| heavy[u] = v;
45| 39.1k| }
46| 53.3k| }
47| 106k| }
48| 53.3k|}
49| |
50| 53.3k|def build_step_2(int u, int w, int p, int d) -> void {
51| 53.3k| int i = id++;
52| 53.3k| node2id[u] = i;
53| 53.3k| depth[i] = d;
54| 53.3k| int anc = ances[i] = node2id[w];
55| 53.3k| parent[i] = node2id[p];
56| 53.3k| node[i * 2 + 1] = {a[u], b[u], b[u]};
57| 53.3k| let a = node + anc * 2;
58| 53.3k| int k = (i - anc) * 2 + 1;
59| 75.0k| for (int j = 1; k & (j * 2); k -= j, j *= 2) {
^21.7k
------------------
| Branch (59:19): [True: 28.95%, False: 71.05%]
------------------
60| 21.7k| a[k - j] = a[k - j * 2] + a[k];
61| 21.7k| }
62| 53.3k| if (int v = heavy[u]; v) {
------------------
| Branch (62:25): [True: 49.99%, False: 50.01%]
------------------
63| 26.6k| build_step_2(v, w, u, d + 1);
64| 26.6k| }
65| 160k| for (int e = head[u]; e; e = edge[e].next) {
^106k
------------------
| Branch (65:25): [True: 66.67%, False: 33.33%]
------------------
66| 106k| int v = edge[e].to;
67| 106k| if (v != p && v != heavy[u]) {
^53.3k
------------------
| Branch (67:9): [True: 50.00%, False: 50.00%]
| Branch (67:19): [True: 50.01%, False: 49.99%]
------------------
68| 26.6k| build_step_2(v, v, u, d + 1);
69| 26.6k| }
70| 106k| }
71| 53.3k|}
72| |
73| 213k|def apply_1(int i, u32 x) -> u32 {
74| 213k| int anc = ances[i];
75| 213k| let a = node + anc * 2;
76| 213k| int r = (i - anc) * 2 + 2;
77| 482k| for (; r; r -= r & -r) {
^268k
------------------
| Branch (77:10): [True: 55.79%, False: 44.21%]
------------------
78| 268k| x = a[r - (r & -r) / 2] + x;
79| 268k| }
80| 213k| return x;
81| 213k|}
82| |
83| 213k|def apply_2(int i, u32 x) -> u32 {
84| 213k| int anc = ances[i];
85| 213k| let a = node + anc * 2;
86| 213k| int r = (i - anc) * 2 + 2;
87| 483k| for (int k = 0; k < r;) {
------------------
| Branch (87:19): [True: 55.84%, False: 44.16%]
------------------
88| 269k| int t = 1 << log(r - k);
89| 269k| x = x + a[k + t / 2];
90| 269k| k += t;
91| 269k| }
92| 213k| return x;
93| 213k|}
94| |
95| 24.8k|def apply_1(int i, int j, u32 x) -> u32 {
96| 24.8k| int anc = ances[i];
97| 24.8k| if (i == anc) return apply_1(j, x);
^1.08k
------------------
| Branch (97:7): [True: 4.36%, False: 95.64%]
------------------
98| 23.8k| let a = node + anc * 2;
99| 23.8k| int l = (i - anc) * 2 - 1;
100| 23.8k| int r = (j - anc) * 2 + 3;
101| 23.8k| int k = (-1 << log(l ^ r)) & r;
102| 51.0k| for (--r; r > k; r -= r & -r) {
^27.2k
------------------
| Branch (102:13): [True: 53.39%, False: 46.61%]
------------------
103| 27.2k| x = a[r - (r & -r) / 2] + x;
104| 27.2k| }
105| 49.0k| for (++l; k > l;) {
------------------
| Branch (105:13): [True: 51.46%, False: 48.54%]
------------------
106| 25.2k| int t = 1 << log(k - l);
107| 25.2k| x = a[k - t / 2] + x;
108| 25.2k| k -= t;
109| 25.2k| }
110| 23.8k| return x;
111| 24.8k|}
112| |
113| 34.9k|def apply_2(int i, int j, u32 x) -> u32 {
114| 34.9k| int anc = ances[i];
115| 34.9k| if (i == anc) return apply_2(j, x);
^1.43k
------------------
| Branch (115:7): [True: 4.10%, False: 95.90%]
------------------
116| 33.5k| let a = node + anc * 2;
117| 33.5k| int l = (i - anc) * 2 - 1;
118| 33.5k| int r = (j - anc) * 2 + 3;
119| 33.5k| int k = (-1 << log(l ^ r)) & r;
120| 66.7k| for (++l; l < k; l += l & -l) {
^33.2k
------------------
| Branch (120:13): [True: 49.84%, False: 50.16%]
------------------
121| 33.2k| x = x + a[l + (l & -l) / 2];
122| 33.2k| }
123| 62.2k| for (--r; k < r;) {
------------------
| Branch (123:13): [True: 46.20%, False: 53.80%]
------------------
124| 28.7k| int t = 1 << log(r - k);
125| 28.7k| x = x + a[k + t / 2];
126| 28.7k| k += t;
127| 28.7k| }
128| 33.5k| return x;
129| 34.9k|}
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| 53.3k| for (int i = 0; i < n; ++i) a[i] = rd.uw(), b[i] = rd.uw();
^53.3k^53.3k
------------------
| Branch (143:19): [True: 100.00%, False: 0.00%]
------------------
144| 53.3k| for (int i = 1; i < n; ++i) {
^53.3k
------------------
| Branch (144:19): [True: 100.00%, False: 0.00%]
------------------
145| 53.3k| int u = rd.uh();
146| 53.3k| int v = rd.uh();
147| 53.3k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
148| 53.3k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
149| 53.3k| }
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| 53.3k| for (int i = 0; i < n; ++i) size[ances[i]]++;
^53.3k^53.3k
------------------
| Branch (153:19): [True: 100.00%, False: 0.00%]
------------------
154| 120k| while (q--) {
------------------
| Branch (154:10): [True: 100.00%, False: 0.00%]
------------------
155| 120k| let t = rd.u1();
156| 120k| if (t == 0) {
------------------
| Branch (156:9): [True: 50.22%, False: 49.78%]
------------------
157| 60.3k| int i = node2id[rd.uh()];
158| 60.3k| u32 c = rd.uw();
159| 60.3k| u32 d = rd.uw();
160| 60.3k| int anc = ances[i];
161| 60.3k| let a = node + anc * 2;
162| 60.3k| int n = size[anc];
163| 60.3k| int k = (i - anc) * 2 + 1;
164| 60.3k| a[k] = {c, d, d};
165| 122k| for (int j = 1;; j *= 2) {
^62.1k
166| 122k| if (k & (j * 2)) {
------------------
| Branch (166:13): [True: 25.37%, False: 74.63%]
------------------
167| 31.0k| a[k - j] = a[k - j * 2] + a[k];
168| 31.0k| k -= j;
169| 91.4k| } else {
170| 91.4k| if (k + j * 3 > n * 2) break;
^60.3k
------------------
| Branch (170:15): [True: 66.01%, False: 33.99%]
------------------
171| 31.0k| a[k + j] = a[k] + a[k + j * 2];
172| 31.0k| k += j;
173| 31.0k| }
174| 122k| }
175| 60.3k| }
176| 120k| if (t == 1) {
------------------
| Branch (176:9): [True: 49.78%, False: 50.22%]
------------------
177| 59.8k| int u = node2id[rd.uh()];
178| 59.8k| int v = node2id[rd.uh()];
179| 59.8k| u32 x = rd.uw();
180| 59.8k| int vec[20], top{};
181| 483k| while (ances[u] != ances[v]) {
------------------
| Branch (181:14): [True: 87.63%, False: 12.37%]
------------------
182| 424k| if (depth[ances[u]] > depth[ances[v]]) {
------------------
| Branch (182:13): [True: 50.00%, False: 50.00%]
------------------
183| 212k| x = apply_1(u, x);
184| 212k| u = parent[ances[u]];
185| 212k| } else {
186| 211k| vec[top++] = v;
187| 211k| v = parent[ances[v]];
188| 211k| }
189| 424k| }
190| 59.8k| if (u > v) {
------------------
| Branch (190:11): [True: 41.61%, False: 58.39%]
------------------
191| 24.8k| x = apply_1(v, u, x);
192| 34.9k| } else {
193| 34.9k| x = apply_2(u, v, x);
194| 34.9k| }
195| 271k| while (top--) {
------------------
| Branch (195:14): [True: 77.99%, False: 22.01%]
------------------
196| 211k| x = apply_2(vec[top], x);
197| 211k| }
198| 59.8k| wt.uw(x);
199| 59.8k| }
200| 120k| }
201| 1| return 0;
202| 1|}