/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| 367k| auto operator+(const Node &t) const -> Node {
13| 367k| u32 x = u64(a) * t.a % P;
14| 367k| u32 y = (u64(a) * t.b + b) % P;
15| 367k| u32 z = (u64(t.a) * c + t.c) % P;
16| 367k| return {x, y, z};
17| 367k| }
18| |} node[N * 2];
19| 10.5M|auto operator+(const Node &l, u32 r) -> u32 { return (u64(l.a) * r + l.b) % P; }
20| 10.5M|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]]) {
^32.7k
------------------
| Branch (43:11): [True: 83.62%, False: 16.38%]
| Branch (43:28): [True: 18.21%, False: 81.79%]
------------------
44| 173k| heavy[u] = v;
45| 173k| }
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| node[i] = {a[u], b[u], b[u]};
54| 200k| depth[i] = d;
55| 200k| ances[i] = node2id[w];
56| 200k| parent[i] = node2id[p];
57| 200k| if (int v = heavy[u]; v) {
------------------
| Branch (57:25): [True: 83.62%, False: 16.38%]
------------------
58| 167k| build_step_2(v, w, u, d + 1);
59| 167k| }
60| 599k| for (int e = head[u]; e; e = edge[e].next) {
^399k
------------------
| Branch (60:25): [True: 66.67%, False: 33.33%]
------------------
61| 399k| int v = edge[e].to;
62| 399k| if (v != p && v != heavy[u]) {
^199k
------------------
| Branch (62:9): [True: 50.00%, False: 50.00%]
| Branch (62:19): [True: 16.38%, False: 83.62%]
------------------
63| 32.7k| build_step_2(v, v, u, d + 1);
64| 32.7k| }
65| 399k| }
66| 200k|}
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| 200k| for (int i = 0; i < n; ++i) a[i] = rd.uw(), b[i] = rd.uw();
^200k^200k
------------------
| Branch (80:19): [True: 100.00%, False: 0.00%]
------------------
81| 200k| for (int i = 1; i < n; ++i) {
^199k
------------------
| Branch (81:19): [True: 100.00%, False: 0.00%]
------------------
82| 199k| int u = rd.uh();
83| 199k| int v = rd.uh();
84| 199k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
85| 199k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
86| 199k| }
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| 200k| for (int i = n - 1; i > 0; --i) node[i] = node[i * 2] + node[i * 2 + 1];
^199k^199k
------------------
| Branch (90:23): [True: 100.00%, False: 0.00%]
------------------
91| 1.52M| let apply_1 = [&](int l, int r, u32 x) -> u32 {
^1
92| 1.52M| l += n - 1;
93| 1.52M| r += n + 1;
94| 1.52M| int k = log(l ^ r);
95| 1.52M| int R = r >> k;
96| 6.50M| for (r = r >> __builtin_ctz(r) ^ 1; r > R; r = r >> __builtin_ctz(r) ^ 1)
^4.98M
------------------
| Branch (96:41): [True: 76.58%, False: 23.42%]
------------------
97| 4.98M| x = node[r] + x;
98| 7.05M| for (int t = ~l & ~(-1 << k), i; t > 0; t -= 1 << i) {
^5.53M
------------------
| Branch (98:38): [True: 78.40%, False: 21.60%]
------------------
99| 5.53M| i = log(t);
100| 5.53M| x = node[l >> i ^ 1] + x;
101| 5.53M| }
102| 1.52M| return x;
103| 1.52M| };
104| 1.52M| let apply_2 = [&](int l, int r, u32 x) -> u32 {
^1
105| 1.52M| l += n - 1;
106| 1.52M| r += n + 1;
107| 1.52M| int k = log(l ^ r);
108| 1.52M| int R = r >> k;
109| 7.06M| for (l = l >> __builtin_ctz(~l) ^ 1; l > R; l = l >> __builtin_ctz(~l) ^ 1)
^5.54M
------------------
| Branch (109:42): [True: 78.42%, False: 21.58%]
------------------
110| 5.54M| x = x + node[l];
111| 6.51M| for (int t = r & ~(-1 << k), i; t > 0; t -= 1 << i) {
^4.99M
------------------
| Branch (111:37): [True: 76.60%, False: 23.40%]
------------------
112| 4.99M| i = log(t);
113| 4.99M| x = x + node[r >> i ^ 1];
114| 4.99M| }
115| 1.52M| return x;
116| 1.52M| };
117| 200k| while (q--) {
------------------
| Branch (117:10): [True: 100.00%, False: 0.00%]
------------------
118| 200k| let t = rd.u1();
119| 200k| if (t == 0) {
------------------
| Branch (119:9): [True: 4.74%, False: 95.27%]
------------------
120| 9.47k| int k = n + node2id[rd.uh()];
121| 9.47k| u32 c = rd.uw();
122| 9.47k| u32 d = rd.uw();
123| 9.47k| node[k] = {c, d, d};
124| 176k| for (k /= 2; k > 0; k /= 2) {
^167k
------------------
| Branch (124:20): [True: 94.65%, False: 5.35%]
------------------
125| 167k| node[k] = node[k * 2] + node[k * 2 + 1];
126| 167k| }
127| 9.47k| }
128| 200k| if (t == 1) {
------------------
| Branch (128:9): [True: 95.27%, False: 4.74%]
------------------
129| 190k| int u = node2id[rd.uh()];
130| 190k| int v = node2id[rd.uh()];
131| 190k| u32 x = rd.uw();
132| 190k| std::pair<int, int> vec[20];
133| 190k| int c = 0;
134| 3.04M| while (ances[u] != ances[v]) {
------------------
| Branch (134:14): [True: 93.75%, False: 6.25%]
------------------
135| 2.85M| if (depth[ances[u]] > depth[ances[v]]) {
------------------
| Branch (135:13): [True: 50.00%, False: 50.00%]
------------------
136| 1.42M| x = apply_1(ances[u], u, x);
137| 1.42M| u = parent[ances[u]];
138| 1.42M| } else {
139| 1.42M| vec[++c] = {ances[v], v};
140| 1.42M| v = parent[ances[v]];
141| 1.42M| }
142| 2.85M| }
143| 190k| if (u > v) {
------------------
| Branch (143:11): [True: 49.91%, False: 50.09%]
------------------
144| 95.1k| x = apply_1(v, u, x);
145| 95.4k| } else {
146| 95.4k| x = apply_2(u, v, x);
147| 95.4k| }
148| 1.61M| for (; c > 0; --c) {
^1.42M
------------------
| Branch (148:14): [True: 88.24%, False: 11.76%]
------------------
149| 1.42M| def[l, r] = vec[c];
150| 1.42M| x = apply_2(l, r, x);
151| 1.42M| }
152| 190k| wt.uw(x);
153| 190k| }
154| 200k| }
155| 1| return 0;
156| 1|}