/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| 7.88k| auto operator+(const Node &t) const -> Node {
13| 7.88k| u32 x = u64(a) * t.a % P;
14| 7.88k| u32 y = (u64(a) * t.b + b) % P;
15| 7.88k| u32 z = (u64(t.a) * c + t.c) % P;
16| 7.88k| return {x, y, z};
17| 7.88k| }
18| |} node[N * 2];
19| 2.24k|auto operator+(const Node &l, u32 r) -> u32 { return (u64(l.a) * r + l.b) % P; }
20| 2.46k|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| 1.21k|def build_step_1(int u, int p) -> void {
37| 1.21k| size[u] = 1;
38| 3.64k| for (int e = head[u]; e; e = edge[e].next) {
^2.43k
------------------
| Branch (38:25): [True: 66.65%, False: 33.35%]
------------------
39| 2.43k| int v = edge[e].to;
40| 2.43k| if (v != p) {
------------------
| Branch (40:9): [True: 50.00%, False: 50.00%]
------------------
41| 1.21k| build_step_1(v, u);
42| 1.21k| size[u] += size[v];
43| 1.21k| if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
^593
------------------
| Branch (43:11): [True: 51.23%, False: 48.77%]
| Branch (43:28): [True: 47.05%, False: 52.95%]
------------------
44| 902| heavy[u] = v;
45| 902| }
46| 1.21k| }
47| 2.43k| }
48| 1.21k|}
49| |
50| 1.21k|def build_step_2(int u, int w, int p, int d) -> void {
51| 1.21k| int i = id++;
52| 1.21k| node2id[u] = i;
53| 1.21k| node[i] = {a[u], b[u], b[u]};
54| 1.21k| depth[i] = d;
55| 1.21k| ances[i] = node2id[w];
56| 1.21k| parent[i] = node2id[p];
57| 1.21k| if (int v = heavy[u]; v) {
------------------
| Branch (57:25): [True: 51.19%, False: 48.81%]
------------------
58| 623| build_step_2(v, w, u, d + 1);
59| 623| }
60| 3.64k| for (int e = head[u]; e; e = edge[e].next) {
^2.43k
------------------
| Branch (60:25): [True: 66.65%, False: 33.35%]
------------------
61| 2.43k| int v = edge[e].to;
62| 2.43k| if (v != p && v != heavy[u]) {
^1.21k
------------------
| Branch (62:9): [True: 50.00%, False: 50.00%]
| Branch (62:19): [True: 48.77%, False: 51.23%]
------------------
63| 593| build_step_2(v, v, u, d + 1);
64| 593| }
65| 2.43k| }
66| 1.21k|}
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| 1.21k| for (int i = 0; i < n; ++i) a[i] = rd.uw(), b[i] = rd.uw();
^1.21k^1.21k
------------------
| Branch (80:19): [True: 99.92%, False: 0.08%]
------------------
81| 1.21k| for (int i = 1; i < n; ++i) {
^1.21k
------------------
| Branch (81:19): [True: 99.92%, False: 0.08%]
------------------
82| 1.21k| int u = rd.uh();
83| 1.21k| int v = rd.uh();
84| 1.21k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
85| 1.21k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
86| 1.21k| }
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| 1.21k| for (int i = n - 1; i > 0; --i) node[i] = node[i * 2] + node[i * 2 + 1];
^1.21k^1.21k
------------------
| Branch (90:23): [True: 99.92%, False: 0.08%]
------------------
91| 1.47k| let apply_1 = [&](int l, int r, u32 x) -> u32 {
^1
92| 1.47k| l += n - 1;
93| 1.47k| r += n + 1;
94| 1.47k| int k = log(l ^ r);
95| 1.47k| int R = r >> k;
96| 2.61k| for (r = r >> __builtin_ctz(r) ^ 1; r > R; r = r >> __builtin_ctz(r) ^ 1)
^1.14k
------------------
| Branch (96:41): [True: 43.71%, False: 56.29%]
------------------
97| 1.14k| x = node[r] + x;
98| 2.57k| for (int t = ~l & ~(-1 << k), i; t > 0; t -= 1 << i) {
^1.10k
------------------
| Branch (98:38): [True: 42.75%, False: 57.25%]
------------------
99| 1.10k| i = log(t);
100| 1.10k| x = node[l >> i ^ 1] + x;
101| 1.10k| }
102| 1.47k| return x;
103| 1.47k| };
104| 1.64k| let apply_2 = [&](int l, int r, u32 x) -> u32 {
^1
105| 1.64k| l += n - 1;
106| 1.64k| r += n + 1;
107| 1.64k| int k = log(l ^ r);
108| 1.64k| int R = r >> k;
109| 2.89k| for (l = l >> __builtin_ctz(~l) ^ 1; l > R; l = l >> __builtin_ctz(~l) ^ 1)
^1.25k
------------------
| Branch (109:42): [True: 43.27%, False: 56.73%]
------------------
110| 1.25k| x = x + node[l];
111| 2.85k| for (int t = r & ~(-1 << k), i; t > 0; t -= 1 << i) {
^1.21k
------------------
| Branch (111:37): [True: 42.47%, False: 57.53%]
------------------
112| 1.21k| i = log(t);
113| 1.21k| x = x + node[r >> i ^ 1];
114| 1.21k| }
115| 1.64k| return x;
116| 1.64k| };
117| 1.26k| while (q--) {
------------------
| Branch (117:10): [True: 99.92%, False: 0.08%]
------------------
118| 1.26k| let t = rd.u1();
119| 1.26k| if (t == 0) {
------------------
| Branch (119:9): [True: 51.15%, False: 48.85%]
------------------
120| 646| int k = n + node2id[rd.uh()];
121| 646| u32 c = rd.uw();
122| 646| u32 d = rd.uw();
123| 646| node[k] = {c, d, d};
124| 7.31k| for (k /= 2; k > 0; k /= 2) {
^6.66k
------------------
| Branch (124:20): [True: 91.16%, False: 8.84%]
------------------
125| 6.66k| node[k] = node[k * 2] + node[k * 2 + 1];
126| 6.66k| }
127| 646| }
128| 1.26k| if (t == 1) {
------------------
| Branch (128:9): [True: 48.85%, False: 51.15%]
------------------
129| 617| int u = node2id[rd.uh()];
130| 617| int v = node2id[rd.uh()];
131| 617| u32 x = rd.uw();
132| 617| std::pair<int, int> vec[20];
133| 617| int c = 0;
134| 3.11k| while (ances[u] != ances[v]) {
------------------
| Branch (134:14): [True: 80.20%, False: 19.80%]
------------------
135| 2.49k| if (depth[ances[u]] > depth[ances[v]]) {
------------------
| Branch (135:13): [True: 50.10%, False: 49.90%]
------------------
136| 1.25k| x = apply_1(ances[u], u, x);
137| 1.25k| u = parent[ances[u]];
138| 1.25k| } else {
139| 1.24k| vec[++c] = {ances[v], v};
140| 1.24k| v = parent[ances[v]];
141| 1.24k| }
142| 2.49k| }
143| 617| if (u > v) {
------------------
| Branch (143:11): [True: 35.82%, False: 64.18%]
------------------
144| 221| x = apply_1(v, u, x);
145| 396| } else {
146| 396| x = apply_2(u, v, x);
147| 396| }
148| 1.86k| for (; c > 0; --c) {
^1.24k
------------------
| Branch (148:14): [True: 66.90%, False: 33.10%]
------------------
149| 1.24k| def[l, r] = vec[c];
150| 1.24k| x = apply_2(l, r, x);
151| 1.24k| }
152| 617| wt.uw(x);
153| 617| }
154| 1.26k| }
155| 1| return 0;
156| 1|}