/tmp/solutions/build/vertex_set_path_composite-slow.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| 4.86k| auto operator+(const Node &t) const -> Node {
13| 4.86k| u32 x = u64(a) * t.a % P;
14| 4.86k| u32 y = (u64(a) * t.b + b) % P;
15| 4.86k| u32 z = (u64(t.a) * c + t.c) % P;
16| 4.86k| return {x, y, z};
17| 4.86k| }
18| |} node[N * 2];
19| 1.96k|auto operator+(const Node &l, u32 r) -> u32 { return (u64(l.a) * r + l.b) % P; }
20| 2.06k|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| 198|def build_step_1(int u, int p) -> void {
37| 198| size[u] = 1;
38| 592| for (int e = head[u]; e; e = edge[e].next) {
^394
------------------
| Branch (38:25): [True: 66.55%, False: 33.45%]
------------------
39| 394| int v = edge[e].to;
40| 394| if (v != p) {
------------------
| Branch (40:9): [True: 50.00%, False: 50.00%]
------------------
41| 197| build_step_1(v, u);
42| 197| size[u] += size[v];
43| 197| if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
^100
------------------
| Branch (43:11): [True: 49.24%, False: 50.76%]
| Branch (43:28): [True: 47.00%, False: 53.00%]
------------------
44| 144| heavy[u] = v;
45| 144| }
46| 197| }
47| 394| }
48| 198|}
49| |
50| 198|def build_step_2(int u, int w, int p, int d) -> void {
51| 198| int i = id++;
52| 198| node2id[u] = i;
53| 198| node[i] = {a[u], b[u], b[u]};
54| 198| depth[i] = d;
55| 198| ances[i] = node2id[w];
56| 198| parent[i] = node2id[p];
57| 198| if (int v = heavy[u]; v) {
------------------
| Branch (57:25): [True: 48.99%, False: 51.01%]
------------------
58| 97| build_step_2(v, w, u, d + 1);
59| 97| }
60| 592| for (int e = head[u]; e; e = edge[e].next) {
^394
------------------
| Branch (60:25): [True: 66.55%, False: 33.45%]
------------------
61| 394| int v = edge[e].to;
62| 394| if (v != p && v != heavy[u]) {
^197
------------------
| Branch (62:9): [True: 50.00%, False: 50.00%]
| Branch (62:19): [True: 50.76%, False: 49.24%]
------------------
63| 100| build_step_2(v, v, u, d + 1);
64| 100| }
65| 394| }
66| 198|}
67| |
68| |} // namespace
69| |
70| 1|int main() {
71| 1| rd rd;
72| 1| wt wt;
73| 1| int n = rd.uh();
74| 1| int q = rd.uh();
75| 1|#ifdef LOCAL
76| 1| id = 0;
77| 1| std::memset(head, 0, 4 * n);
78| 1| std::memset(heavy, 0, 4 * n);
79| 1|#endif
80| 199| for (int i = 0; i < n; ++i) a[i] = rd.uw(), b[i] = rd.uw();
^198 ^198
------------------
| Branch (80:19): [True: 99.50%, False: 0.50%]
------------------
81| 198| for (int i = 1; i < n; ++i) {
^197
------------------
| Branch (81:19): [True: 99.49%, False: 0.51%]
------------------
82| 197| int u = rd.uh();
83| 197| int v = rd.uh();
84| 197| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
85| 197| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
86| 197| }
87| 1| build_step_1(0, 0);
88| 1| build_step_2(0, 0, 0, 0);
89| 1| std::memcpy(node + n, node, sizeof(Node) * n);
90| 198| for (int i = n - 1; i > 0; --i) node[i] = node[i * 2] + node[i * 2 + 1];
^197 ^197
------------------
| Branch (90:23): [True: 99.49%, False: 0.51%]
------------------
91| 1.27k| let apply_1 = [&](int l, int r, u32 x) -> u32 {
^1
92| 1.27k| l += n - 1;
93| 1.27k| r += n + 1;
94| 1.27k| int k = log(l ^ r);
95| 1.27k| int R = r >> k;
96| 2.13k| for (r = r >> __builtin_ctz(r) ^ 1; r > R; r = r >> __builtin_ctz(r) ^ 1)
^862
------------------
| Branch (96:41): [True: 40.39%, False: 59.61%]
------------------
97| 862| x = node[r] + x;
98| 2.37k| for (int t = ~l & ~(-1 << k), i; t > 0; t -= 1 << i) {
^1.10k
------------------
| Branch (98:38): [True: 46.51%, False: 53.49%]
------------------
99| 1.10k| i = log(t);
100| 1.10k| x = node[l >> i ^ 1] + x;
101| 1.10k| }
102| 1.27k| return x;
103| 1.27k| };
104| 1.38k| let apply_2 = [&](int l, int r, u32 x) -> u32 {
^1
105| 1.38k| l += n - 1;
106| 1.38k| r += n + 1;
107| 1.38k| int k = log(l ^ r);
108| 1.38k| int R = r >> k;
109| 2.53k| for (l = l >> __builtin_ctz(~l) ^ 1; l > R; l = l >> __builtin_ctz(~l) ^ 1)
^1.15k
------------------
| Branch (109:42): [True: 45.55%, False: 54.45%]
------------------
110| 1.15k| x = x + node[l];
111| 2.28k| for (int t = r & ~(-1 << k), i; t > 0; t -= 1 << i) {
^907
------------------
| Branch (111:37): [True: 39.62%, False: 60.38%]
------------------
112| 907| i = log(t);
113| 907| x = x + node[r >> i ^ 1];
114| 907| }
115| 1.38k| return x;
116| 1.38k| };
117| 1.26k| while (q--) {
------------------
| Branch (117:10): [True: 99.92%, False: 0.08%]
------------------
118| 1.25k| let t = rd.u1();
119| 1.25k| if (t == 0) {
------------------
| Branch (119:9): [True: 47.97%, False: 52.03%]
------------------
120| 604| int k = n + node2id[rd.uh()];
121| 604| u32 c = rd.uw();
122| 604| u32 d = rd.uw();
123| 604| node[k] = {c, d, d};
124| 5.27k| for (k /= 2; k > 0; k /= 2) {
^4.67k
------------------
| Branch (124:20): [True: 88.55%, False: 11.45%]
------------------
125| 4.67k| node[k] = node[k * 2] + node[k * 2 + 1];
126| 4.67k| }
127| 604| }
128| 1.25k| if (t == 1) {
------------------
| Branch (128:9): [True: 52.03%, False: 47.97%]
------------------
129| 655| int u = node2id[rd.uh()];
130| 655| int v = node2id[rd.uh()];
131| 655| u32 x = rd.uw();
132| 655| std::pair<int, int> vec[20];
133| 655| int c = 0;
134| 2.65k| while (ances[u] != ances[v]) {
------------------
| Branch (134:14): [True: 75.32%, False: 24.68%]
------------------
135| 1.99k| if (depth[ances[u]] > depth[ances[v]]) {
------------------
| Branch (135:13): [True: 50.43%, False: 49.57%]
------------------
136| 1.00k| x = apply_1(ances[u], u, x);
137| 1.00k| u = parent[ances[u]];
138| 1.00k| } else {
139| 991| vec[++c] = {ances[v], v};
140| 991| v = parent[ances[v]];
141| 991| }
142| 1.99k| }
143| 655| if (u > v) {
------------------
| Branch (143:11): [True: 40.31%, False: 59.69%]
------------------
144| 264| x = apply_1(v, u, x);
145| 391| } else {
146| 391| x = apply_2(u, v, x);
147| 391| }
148| 1.64k| for (; c > 0; --c) {
^991
------------------
| Branch (148:14): [True: 60.21%, False: 39.79%]
------------------
149| 991| def[l, r] = vec[c];
150| 991| x = apply_2(l, r, x);
151| 991| }
152| 655| wt.uw(x);
153| 655| }
154| 1.25k| }
155| 1| return 0;
156| 1|}