/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| 18.5M| auto operator+(const Node &t) const -> Node {
13| 18.5M| u32 x = u64(a) * t.a % P;
14| 18.5M| u32 y = (u64(a) * t.b + b) % P;
15| 18.5M| u32 z = (u64(t.a) * c + t.c) % P;
16| 18.5M| return {x, y, z};
17| 18.5M| }
18| |} node[N * 2];
19| 29.0M|auto operator+(const Node &l, u32 r) -> u32 { return (u64(l.a) * r + l.b) % P; }
20| 32.1M|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| 2.33M|def build_step_1(int u, int p) -> void {
37| 2.33M| size[u] = 1;
38| 7.00M| for (int e = head[u]; e; e = edge[e].next) {
^4.66M
------------------
| Branch (38:25): [True: 66.67%, False: 33.33%]
------------------
39| 4.66M| int v = edge[e].to;
40| 4.66M| if (v != p) {
------------------
| Branch (40:9): [True: 50.00%, False: 50.00%]
------------------
41| 2.33M| build_step_1(v, u);
42| 2.33M| size[u] += size[v];
43| 2.33M| if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
^682k
------------------
| Branch (43:11): [True: 70.74%, False: 29.26%]
| Branch (43:28): [True: 50.48%, False: 49.52%]
------------------
44| 1.99M| heavy[u] = v;
45| 1.99M| }
46| 2.33M| }
47| 4.66M| }
48| 2.33M|}
49| |
50| 2.33M|def build_step_2(int u, int w, int p, int d) -> void {
51| 2.33M| int i = id++;
52| 2.33M| node2id[u] = i;
53| 2.33M| node[i] = {a[u], b[u], b[u]};
54| 2.33M| depth[i] = d;
55| 2.33M| ances[i] = node2id[w];
56| 2.33M| parent[i] = node2id[p];
57| 2.33M| if (int v = heavy[u]; v) {
------------------
| Branch (57:25): [True: 70.74%, False: 29.26%]
------------------
58| 1.65M| build_step_2(v, w, u, d + 1);
59| 1.65M| }
60| 7.00M| for (int e = head[u]; e; e = edge[e].next) {
^4.66M
------------------
| Branch (60:25): [True: 66.67%, False: 33.33%]
------------------
61| 4.66M| int v = edge[e].to;
62| 4.66M| if (v != p && v != heavy[u]) {
^2.33M
------------------
| Branch (62:9): [True: 50.00%, False: 50.00%]
| Branch (62:19): [True: 29.26%, False: 70.74%]
------------------
63| 682k| build_step_2(v, v, u, d + 1);
64| 682k| }
65| 4.66M| }
66| 2.33M|}
67| |
68| |} // namespace
69| |
70| 20|int main() {
71| 20| rd rd;
72| 20| wt wt;
73| 20| int n = rd.uh();
74| 20| int q = rd.uh();
75| 20|#ifdef LOCAL
76| 20| id = 0;
77| 20| std::memset(head, 0, 4 * n);
78| 20| std::memset(heavy, 0, 4 * n);
79| 20|#endif
80| 2.33M| for (int i = 0; i < n; ++i) a[i] = rd.uw(), b[i] = rd.uw();
^2.33M^2.33M
------------------
| Branch (80:19): [True: 100.00%, False: 0.00%]
------------------
81| 2.33M| for (int i = 1; i < n; ++i) {
^2.33M
------------------
| Branch (81:19): [True: 100.00%, False: 0.00%]
------------------
82| 2.33M| int u = rd.uh();
83| 2.33M| int v = rd.uh();
84| 2.33M| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
85| 2.33M| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
86| 2.33M| }
87| 20| build_step_1(0, 0);
88| 20| build_step_2(0, 0, 0, 0);
89| 20| std::memcpy(node + n, node, sizeof(Node) * n);
90| 2.33M| for (int i = n - 1; i > 0; --i) node[i] = node[i * 2] + node[i * 2 + 1];
^2.33M^2.33M
------------------
| Branch (90:23): [True: 100.00%, False: 0.00%]
------------------
91| 5.66M| let apply_1 = [&](int l, int r, u32 x) -> u32 {
^20
92| 5.66M| l += n - 1;
93| 5.66M| r += n + 1;
94| 5.66M| int k = log(l ^ r);
95| 5.66M| int R = r >> k;
96| 19.9M| for (r = r >> __builtin_ctz(r) ^ 1; r > R; r = r >> __builtin_ctz(r) ^ 1)
^14.2M
------------------
| Branch (96:41): [True: 71.56%, False: 28.44%]
------------------
97| 14.2M| x = node[r] + x;
98| 20.4M| for (int t = ~l & ~(-1 << k), i; t > 0; t -= 1 << i) {
^14.7M
------------------
| Branch (98:38): [True: 72.31%, False: 27.69%]
------------------
99| 14.7M| i = log(t);
100| 14.7M| x = node[l >> i ^ 1] + x;
101| 14.7M| }
102| 5.66M| return x;
103| 5.66M| };
104| 6.36M| let apply_2 = [&](int l, int r, u32 x) -> u32 {
^20
105| 6.36M| l += n - 1;
106| 6.36M| r += n + 1;
107| 6.36M| int k = log(l ^ r);
108| 6.36M| int R = r >> k;
109| 22.9M| for (l = l >> __builtin_ctz(~l) ^ 1; l > R; l = l >> __builtin_ctz(~l) ^ 1)
^16.6M
------------------
| Branch (109:42): [True: 72.32%, False: 27.68%]
------------------
110| 16.6M| x = x + node[l];
111| 21.8M| for (int t = r & ~(-1 << k), i; t > 0; t -= 1 << i) {
^15.5M
------------------
| Branch (111:37): [True: 70.93%, False: 29.07%]
------------------
112| 15.5M| i = log(t);
113| 15.5M| x = x + node[r >> i ^ 1];
114| 15.5M| }
115| 6.36M| return x;
116| 6.36M| };
117| 2.42M| while (q--) {
------------------
| Branch (117:10): [True: 100.00%, False: 0.00%]
------------------
118| 2.42M| let t = rd.u1();
119| 2.42M| if (t == 0) {
------------------
| Branch (119:9): [True: 38.40%, False: 61.60%]
------------------
120| 930k| int k = n + node2id[rd.uh()];
121| 930k| u32 c = rd.uw();
122| 930k| u32 d = rd.uw();
123| 930k| node[k] = {c, d, d};
124| 17.1M| for (k /= 2; k > 0; k /= 2) {
^16.2M
------------------
| Branch (124:20): [True: 94.58%, False: 5.42%]
------------------
125| 16.2M| node[k] = node[k * 2] + node[k * 2 + 1];
126| 16.2M| }
127| 930k| }
128| 2.42M| if (t == 1) {
------------------
| Branch (128:9): [True: 61.60%, False: 38.40%]
------------------
129| 1.49M| int u = node2id[rd.uh()];
130| 1.49M| int v = node2id[rd.uh()];
131| 1.49M| u32 x = rd.uw();
132| 1.49M| std::pair<int, int> vec[20];
133| 1.49M| int c = 0;
134| 12.0M| while (ances[u] != ances[v]) {
------------------
| Branch (134:14): [True: 87.58%, False: 12.42%]
------------------
135| 10.5M| if (depth[ances[u]] > depth[ances[v]]) {
------------------
| Branch (135:13): [True: 48.10%, False: 51.90%]
------------------
136| 5.06M| x = apply_1(ances[u], u, x);
137| 5.06M| u = parent[ances[u]];
138| 5.46M| } else {
139| 5.46M| vec[++c] = {ances[v], v};
140| 5.46M| v = parent[ances[v]];
141| 5.46M| }
142| 10.5M| }
143| 1.49M| if (u > v) {
------------------
| Branch (143:11): [True: 39.99%, False: 60.01%]
------------------
144| 597k| x = apply_1(v, u, x);
145| 895k| } else {
146| 895k| x = apply_2(u, v, x);
147| 895k| }
148| 6.95M| for (; c > 0; --c) {
^5.46M
------------------
| Branch (148:14): [True: 78.54%, False: 21.46%]
------------------
149| 5.46M| def[l, r] = vec[c];
150| 5.46M| x = apply_2(l, r, x);
151| 5.46M| }
152| 1.49M| wt.uw(x);
153| 1.49M| }
154| 2.42M| }
155| 20| return 0;
156| 20|}