/tmp/solutions/build/vertex_set_path_composite-logn.cpp:
1| |#include <common.h>
2| |#include <toy/bit.h>
3| |prelude;
4| |
5| |namespace {
6| |
7| |constexpr int N = 200001;
8| |constexpr int P = 998244353;
9| |
10| |struct Vertex {
11| | u32 a, b;
12| |};
13| |struct Segment {
14| | u32 a, b, c;
15| | Segment() = default;
16| 384k| Segment(Vertex v) : a{v.a}, b{v.b}, c{v.b} {}
17| 1.32M| Segment(u32 a, u32 b, u32 c) : a{a}, b{b}, c{c} {}
18| |};
19| |struct Node {
20| | Vertex v;
21| | Segment s, t;
22| | int l, r, p;
23| |} node[N];
24| 1.17M|auto operator+(const Segment &l, const Segment &r) -> Segment {
25| 1.17M| u32 x = u64(l.a) * r.a % P;
26| 1.17M| u32 y = (u64(l.a) * r.b + l.b) % P;
27| 1.17M| u32 z = (u64(r.a) * l.c + r.c) % P;
28| 1.17M| return {x, y, z};
29| 1.17M|}
30| 589k|auto operator+(const Segment &l, u32 r) -> u32 {
31| 589k| return (u64(l.a) * r + l.b) % P;
32| 589k|}
33| 74.5k|auto operator+(u32 l, const Segment &r) -> u32 {
34| 74.5k| return (u64(r.a) * l + r.c) % P;
35| 74.5k|}
36| |u32 a[N];
37| |u32 b[N];
38| |int head[N];
39| |int size[N];
40| |int depth[N];
41| |int heavy[N];
42| |int ances[N];
43| |int parent[N];
44| |int node2id[N];
45| |u8 priority[N];
46| |int id;
47| |struct {
48| | int to;
49| | int next;
50| |} edge[N * 2];
51| |
52| 127k|def build_step_1(int u, int p) -> void {
53| 127k| size[u] = 1;
54| 383k| for (int e = head[u]; e; e = edge[e].next) {
^255k
------------------
| Branch (54:25): [True: 66.67%, False: 33.33%]
------------------
55| 255k| int v = edge[e].to;
56| 255k| if (v != p) {
------------------
| Branch (56:9): [True: 50.00%, False: 50.00%]
------------------
57| 127k| build_step_1(v, u);
58| 127k| size[u] += size[v];
59| 127k| if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
^63.9k
------------------
| Branch (59:11): [True: 49.91%, False: 50.09%]
| Branch (59:28): [True: 46.67%, False: 53.33%]
------------------
60| 93.5k| heavy[u] = v;
61| 93.5k| }
62| 127k| }
63| 255k| }
64| 127k|}
65| |
66| 127k|def build_step_2(int u, int w, int p, int d) -> void {
67| 127k| int i = ++id;
68| 127k| node2id[u] = i;
69| 127k| depth[i] = d;
70| 127k| ances[i] = node2id[w];
71| 127k| parent[i] = node2id[p];
72| 127k| node[i].v = {a[u], b[u]};
73| 383k| for (int e = head[u]; e; e = edge[e].next) {
^255k
------------------
| Branch (73:25): [True: 66.67%, False: 33.33%]
------------------
74| 255k| int v = edge[e].to;
75| 255k| if (v != p && v != heavy[u]) {
^127k
------------------
| Branch (75:9): [True: 50.00%, False: 50.00%]
| Branch (75:19): [True: 50.09%, False: 49.91%]
------------------
76| 63.9k| build_step_2(v, v, u, d + 1);
77| 63.9k| }
78| 255k| }
79| 127k| priority[i] = u8(log(i ^ (id + 1)));
80| 127k| if (u != w) {
------------------
| Branch (80:7): [True: 49.91%, False: 50.09%]
------------------
81| 63.7k| int p = parent[i];
82| 63.7k| int l = 0;
83| 98.3k| while (p && priority[p] < priority[i]) {
^82.1k
------------------
| Branch (83:12): [True: 83.48%, False: 16.52%]
| Branch (83:17): [True: 42.19%, False: 57.81%]
------------------
84| 34.6k| l = p;
85| 34.6k| p = node[p].p;
86| 34.6k| }
87| 63.7k| node[p].r = i;
88| 63.7k| node[i].p = p;
89| 63.7k| node[i].l = l;
90| 63.7k| node[l].p = i;
91| 63.7k| }
92| 127k| if (int v = heavy[u]; v) {
------------------
| Branch (92:25): [True: 49.91%, False: 50.09%]
------------------
93| 63.7k| build_step_2(v, w, u, d + 1);
94| 63.7k| }
95| 127k|}
96| |
97| 265k|def maintain(int u) -> void {
98| 265k| node[u].s = (node[u].t = node[node[u].l].s + node[u].v) + node[node[u].r].s;
99| 265k|}
100| |
101| 590k|template <typename T> T apply(int k, T x) {
102| 1.80M| for (int u = k; u; u = node[u].p) {
^1.21M
------------------
| Branch (102:19): [True: 67.28%, False: 32.72%]
------------------
| Branch (102:19): [True: 67.26%, False: 32.74%]
------------------
103| 1.21M| if (u <= k) {
------------------
| Branch (103:9): [True: 76.27%, False: 23.73%]
------------------
| Branch (103:9): [True: 76.35%, False: 23.65%]
------------------
104| 926k| x = node[u].t + x;
105| 926k| }
106| 1.21M| }
107| 590k| return x;
108| 590k|}
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyIjEET_iS1_:
| 101| 294k|template <typename T> T apply(int k, T x) {
| 102| 901k| for (int u = k; u; u = node[u].p) {
| ^606k
| ------------------
| | Branch (102:19): [True: 67.28%, False: 32.72%]
| ------------------
| 103| 606k| if (u <= k) {
| ------------------
| | Branch (103:9): [True: 76.27%, False: 23.73%]
| ------------------
| 104| 462k| x = node[u].t + x;
| 105| 462k| }
| 106| 606k| }
| 107| 294k| return x;
| 108| 294k|}
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyINS_7SegmentEEET_iS2_:
| 101| 295k|template <typename T> T apply(int k, T x) {
| 102| 902k| for (int u = k; u; u = node[u].p) {
| ^607k
| ------------------
| | Branch (102:19): [True: 67.26%, False: 32.74%]
| ------------------
| 103| 607k| if (u <= k) {
| ------------------
| | Branch (103:9): [True: 76.35%, False: 23.65%]
| ------------------
| 104| 463k| x = node[u].t + x;
| 105| 463k| }
| 106| 607k| }
| 107| 295k| return x;
| 108| 295k|}
------------------
109| |
110| 74.5k|template <typename T> T apply(int l, int r, T x) {
111| 74.5k| Segment t = {1, 0, 0};
112| 74.5k| int u = l;
113| 74.5k| int v = r;
114| 201k| while (u != v) {
------------------
| Branch (114:10): [True: 58.00%, False: 42.00%]
------------------
| Branch (114:10): [True: 68.93%, False: 31.07%]
------------------
115| 126k| if (priority[u] < priority[v]) {
------------------
| Branch (115:9): [True: 34.97%, False: 65.03%]
------------------
| Branch (115:9): [True: 35.56%, False: 64.44%]
------------------
116| 44.7k| if (l <= u) {
------------------
| Branch (116:11): [True: 99.52%, False: 0.48%]
------------------
| Branch (116:11): [True: 99.51%, False: 0.49%]
------------------
117| 44.5k| t = (t + node[u].v) + node[node[u].r].s;
118| 44.5k| }
119| 44.7k| u = node[u].p;
120| 82.1k| } else {
121| 82.1k| if (v <= r) {
------------------
| Branch (121:11): [True: 84.87%, False: 15.13%]
------------------
| Branch (121:11): [True: 84.94%, False: 15.06%]
------------------
122| 69.7k| x = node[v].t + x;
123| 69.7k| }
124| 82.1k| v = node[v].p;
125| 82.1k| }
126| 126k| }
127| 74.5k| x = t + (node[u].v + x);
128| 74.5k| return x;
129| 74.5k|}
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyIjEET_iiS1_:
| 110| 46.0k|template <typename T> T apply(int l, int r, T x) {
| 111| 46.0k| Segment t = {1, 0, 0};
| 112| 46.0k| int u = l;
| 113| 46.0k| int v = r;
| 114| 109k| while (u != v) {
| ------------------
| | Branch (114:10): [True: 58.00%, False: 42.00%]
| ------------------
| 115| 63.6k| if (priority[u] < priority[v]) {
| ------------------
| | Branch (115:9): [True: 34.97%, False: 65.03%]
| ------------------
| 116| 22.2k| if (l <= u) {
| ------------------
| | Branch (116:11): [True: 99.52%, False: 0.48%]
| ------------------
| 117| 22.1k| t = (t + node[u].v) + node[node[u].r].s;
| 118| 22.1k| }
| 119| 22.2k| u = node[u].p;
| 120| 41.3k| } else {
| 121| 41.3k| if (v <= r) {
| ------------------
| | Branch (121:11): [True: 84.87%, False: 15.13%]
| ------------------
| 122| 35.1k| x = node[v].t + x;
| 123| 35.1k| }
| 124| 41.3k| v = node[v].p;
| 125| 41.3k| }
| 126| 63.6k| }
| 127| 46.0k| x = t + (node[u].v + x);
| 128| 46.0k| return x;
| 129| 46.0k|}
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyINS_7SegmentEEET_iiS2_:
| 110| 28.5k|template <typename T> T apply(int l, int r, T x) {
| 111| 28.5k| Segment t = {1, 0, 0};
| 112| 28.5k| int u = l;
| 113| 28.5k| int v = r;
| 114| 91.7k| while (u != v) {
| ------------------
| | Branch (114:10): [True: 68.93%, False: 31.07%]
| ------------------
| 115| 63.2k| if (priority[u] < priority[v]) {
| ------------------
| | Branch (115:9): [True: 35.56%, False: 64.44%]
| ------------------
| 116| 22.4k| if (l <= u) {
| ------------------
| | Branch (116:11): [True: 99.51%, False: 0.49%]
| ------------------
| 117| 22.3k| t = (t + node[u].v) + node[node[u].r].s;
| 118| 22.3k| }
| 119| 22.4k| u = node[u].p;
| 120| 40.7k| } else {
| 121| 40.7k| if (v <= r) {
| ------------------
| | Branch (121:11): [True: 84.94%, False: 15.06%]
| ------------------
| 122| 34.6k| x = node[v].t + x;
| 123| 34.6k| }
| 124| 40.7k| v = node[v].p;
| 125| 40.7k| }
| 126| 63.2k| }
| 127| 28.5k| x = t + (node[u].v + x);
| 128| 28.5k| return x;
| 129| 28.5k|}
------------------
130| |
131| |} // namespace
132| |
133| 1|int main() {
134| 1| rd rd;
135| 1| wt wt;
136| 1| int n = rd.uh();
137| 1| int q = rd.uh();
138| 1|#ifdef LOCAL
139| 1| id = 0;
140| 1| std::memset(head, 0, 4 * n);
141| 1| std::memset(heavy, 0, 4 * n);
142| 1| std::memset(node, 0, (n + 1) * sizeof(Node));
143| 1|#endif
144| 1| node[0].s.a = 1;
145| 127k| for (int i = 0; i < n; ++i) a[i] = rd.uw(), b[i] = rd.uw();
^127k^127k
------------------
| Branch (145:19): [True: 100.00%, False: 0.00%]
------------------
146| 127k| for (int i = 1; i < n; ++i) {
^127k
------------------
| Branch (146:19): [True: 100.00%, False: 0.00%]
------------------
147| 127k| int u = rd.uh();
148| 127k| int v = rd.uh();
149| 127k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
150| 127k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
151| 127k| }
152| 1| build_step_1(0, 0);
153| 1| build_step_2(0, 0, 0, 0);
154| 127k| for (int i = 1; i <= n; ++i) {
^127k
------------------
| Branch (154:19): [True: 100.00%, False: 0.00%]
------------------
155| 127k| if (node[i].r == 0) {
------------------
| Branch (155:9): [True: 71.03%, False: 28.97%]
------------------
156| 218k| for (int u = i; u && u <= i; u = node[u].p) {
^154k ^127k
------------------
| Branch (156:23): [True: 70.71%, False: 29.29%]
| Branch (156:28): [True: 82.69%, False: 17.31%]
------------------
157| 127k| maintain(u);
158| 127k| }
159| 90.6k| }
160| 127k| }
161| 148k| while (q--) {
------------------
| Branch (161:10): [True: 100.00%, False: 0.00%]
------------------
162| 148k| let t = rd.u1();
163| 148k| if (t == 0) {
------------------
| Branch (163:9): [True: 49.87%, False: 50.13%]
------------------
164| 74.1k| int u = node2id[rd.uh()];
165| 74.1k| node[u].v = {rd.uw(), rd.uw()};
166| 211k| for (; u; u = node[u].p) {
^137k
------------------
| Branch (166:14): [True: 64.96%, False: 35.04%]
------------------
167| 137k| maintain(u);
168| 137k| }
169| 74.1k| }
170| 148k| if (t == 1) {
------------------
| Branch (170:9): [True: 50.13%, False: 49.87%]
------------------
171| 74.5k| int u = node2id[rd.uh()];
172| 74.5k| int v = node2id[rd.uh()];
173| 74.5k| u32 x = rd.uw();
174| 74.5k| Segment f = {1, 0, 0};
175| 665k| while (ances[u] != ances[v]) {
------------------
| Branch (175:14): [True: 88.79%, False: 11.21%]
------------------
176| 590k| if (depth[ances[u]] >= depth[ances[v]]) {
------------------
| Branch (176:13): [True: 49.94%, False: 50.06%]
------------------
177| 294k| x = apply(u, x);
178| 294k| u = parent[ances[u]];
179| 295k| } else {
180| 295k| f = apply(v, f);
181| 295k| v = parent[ances[v]];
182| 295k| }
183| 590k| }
184| 74.5k| if (u >= v) {
------------------
| Branch (184:11): [True: 61.78%, False: 38.22%]
------------------
185| 46.0k| x = apply(v, u, x);
186| 46.0k| } else {
187| 28.5k| f = apply(u, v, f);
188| 28.5k| }
189| 74.5k| x = x + f;
190| 74.5k| wt.uw(x);
191| 74.5k| }
192| 148k| }
193| 1| return 0;
194| 1|}