/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| 6| auto operator+(const Node &t) const -> Node {
13| 6| u32 x = u64(a) * t.a % P;
14| 6| u32 y = (u64(a) * t.b + b) % P;
15| 6| u32 z = (u64(t.a) * c + t.c) % P;
16| 6| return {x, y, z};
17| 6| }
18| |} node[N * 2];
19| 0|auto operator+(const Node &l, u32 r) -> u32 { return (u64(l.a) * r + l.b) % P; }
20| 9|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| 5|def build_step_1(int u, int p) -> void {
37| 5| size[u] = 1;
38| 13| for (int e = head[u]; e; e = edge[e].next) {
^8
------------------
| Branch (38:25): [True: 61.54%, False: 38.46%]
------------------
39| 8| int v = edge[e].to;
40| 8| if (v != p) {
------------------
| Branch (40:9): [True: 50.00%, False: 50.00%]
------------------
41| 4| build_step_1(v, u);
42| 4| size[u] += size[v];
43| 4| if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
^1
------------------
| Branch (43:11): [True: 75.00%, False: 25.00%]
| Branch (43:28): [True: 0.00%, False: 100.00%]
------------------
44| 3| heavy[u] = v;
45| 3| }
46| 4| }
47| 8| }
48| 5|}
49| |
50| 5|def build_step_2(int u, int w, int p, int d) -> void {
51| 5| int i = id++;
52| 5| node2id[u] = i;
53| 5| node[i] = {a[u], b[u], b[u]};
54| 5| depth[i] = d;
55| 5| ances[i] = node2id[w];
56| 5| parent[i] = node2id[p];
57| 5| if (int v = heavy[u]; v) {
------------------
| Branch (57:25): [True: 60.00%, False: 40.00%]
------------------
58| 3| build_step_2(v, w, u, d + 1);
59| 3| }
60| 13| for (int e = head[u]; e; e = edge[e].next) {
^8
------------------
| Branch (60:25): [True: 61.54%, False: 38.46%]
------------------
61| 8| int v = edge[e].to;
62| 8| if (v != p && v != heavy[u]) {
^4
------------------
| Branch (62:9): [True: 50.00%, False: 50.00%]
| Branch (62:19): [True: 25.00%, False: 75.00%]
------------------
63| 1| build_step_2(v, v, u, d + 1);
64| 1| }
65| 8| }
66| 5|}
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| 6| for (int i = 0; i < n; ++i) a[i] = rd.uw(), b[i] = rd.uw();
^5 ^5
------------------
| Branch (80:19): [True: 83.33%, False: 16.67%]
------------------
81| 5| for (int i = 1; i < n; ++i) {
^4
------------------
| Branch (81:19): [True: 80.00%, False: 20.00%]
------------------
82| 4| int u = rd.uh();
83| 4| int v = rd.uh();
84| 4| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
85| 4| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
86| 4| }
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| 5| for (int i = n - 1; i > 0; --i) node[i] = node[i * 2] + node[i * 2 + 1];
^4 ^4
------------------
| Branch (90:23): [True: 80.00%, False: 20.00%]
------------------
91| 1| let apply_1 = [&](int l, int r, u32 x) -> u32 {
92| 0| l += n - 1;
93| 0| r += n + 1;
94| 0| int k = log(l ^ r);
95| 0| int R = r >> k;
96| 0| for (r = r >> __builtin_ctz(r) ^ 1; r > R; r = r >> __builtin_ctz(r) ^ 1)
------------------
| Branch (96:41): [True: 0.00%, False: 0.00%]
------------------
97| 0| x = node[r] + x;
98| 0| for (int t = ~l & ~(-1 << k), i; t > 0; t -= 1 << i) {
------------------
| Branch (98:38): [True: 0.00%, False: 0.00%]
------------------
99| 0| i = log(t);
100| 0| x = node[l >> i ^ 1] + x;
101| 0| }
102| 0| return x;
103| 0| };
104| 5| let apply_2 = [&](int l, int r, u32 x) -> u32 {
^1
105| 5| l += n - 1;
106| 5| r += n + 1;
107| 5| int k = log(l ^ r);
108| 5| int R = r >> k;
109| 12| for (l = l >> __builtin_ctz(~l) ^ 1; l > R; l = l >> __builtin_ctz(~l) ^ 1)
^7
------------------
| Branch (109:42): [True: 58.33%, False: 41.67%]
------------------
110| 7| x = x + node[l];
111| 7| for (int t = r & ~(-1 << k), i; t > 0; t -= 1 << i) {
^2
------------------
| Branch (111:37): [True: 28.57%, False: 71.43%]
------------------
112| 2| i = log(t);
113| 2| x = x + node[r >> i ^ 1];
114| 2| }
115| 5| return x;
116| 5| };
117| 6| while (q--) {
------------------
| Branch (117:10): [True: 83.33%, False: 16.67%]
------------------
118| 5| let t = rd.u1();
119| 5| if (t == 0) {
------------------
| Branch (119:9): [True: 20.00%, False: 80.00%]
------------------
120| 1| int k = n + node2id[rd.uh()];
121| 1| u32 c = rd.uw();
122| 1| u32 d = rd.uw();
123| 1| node[k] = {c, d, d};
124| 3| for (k /= 2; k > 0; k /= 2) {
^2
------------------
| Branch (124:20): [True: 66.67%, False: 33.33%]
------------------
125| 2| node[k] = node[k * 2] + node[k * 2 + 1];
126| 2| }
127| 1| }
128| 5| if (t == 1) {
------------------
| Branch (128:9): [True: 80.00%, False: 20.00%]
------------------
129| 4| int u = node2id[rd.uh()];
130| 4| int v = node2id[rd.uh()];
131| 4| u32 x = rd.uw();
132| 4| std::pair<int, int> vec[20];
133| 4| int c = 0;
134| 5| while (ances[u] != ances[v]) {
------------------
| Branch (134:14): [True: 20.00%, False: 80.00%]
------------------
135| 1| if (depth[ances[u]] > depth[ances[v]]) {
------------------
| Branch (135:13): [True: 0.00%, False: 100.00%]
------------------
136| 0| x = apply_1(ances[u], u, x);
137| 0| u = parent[ances[u]];
138| 1| } else {
139| 1| vec[++c] = {ances[v], v};
140| 1| v = parent[ances[v]];
141| 1| }
142| 1| }
143| 4| if (u > v) {
------------------
| Branch (143:11): [True: 0.00%, False: 100.00%]
------------------
144| 0| x = apply_1(v, u, x);
145| 4| } else {
146| 4| x = apply_2(u, v, x);
147| 4| }
148| 5| for (; c > 0; --c) {
^1
------------------
| Branch (148:14): [True: 20.00%, False: 80.00%]
------------------
149| 1| def[l, r] = vec[c];
150| 1| x = apply_2(l, r, x);
151| 1| }
152| 4| wt.uw(x);
153| 4| }
154| 5| }
155| 1| return 0;
156| 1|}