/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| 1.99M| Segment(Vertex v) : a{v.a}, b{v.b}, c{v.b} {}
17| 6.39M| 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| 5.99M|auto operator+(const Segment &l, const Segment &r) -> Segment {
25| 5.99M| u32 x = u64(l.a) * r.a % P;
26| 5.99M| u32 y = (u64(l.a) * r.b + l.b) % P;
27| 5.99M| u32 z = (u64(r.a) * l.c + r.c) % P;
28| 5.99M| return {x, y, z};
29| 5.99M|}
30| 0|auto operator+(const Segment &l, u32 r) -> u32 {
31| 0| return (u64(l.a) * r + l.b) % P;
32| 0|}
33| 200k|auto operator+(u32 l, const Segment &r) -> u32 {
34| 200k| return (u64(r.a) * l + r.c) % P;
35| 200k|}
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| 199k|def build_step_1(int u, int p) -> void {
53| 199k| size[u] = 1;
54| 599k| for (int e = head[u]; e; e = edge[e].next) {
^399k
------------------
| Branch (54:25): [True: 66.67%, False: 33.33%]
------------------
55| 399k| int v = edge[e].to;
56| 399k| if (v != p) {
------------------
| Branch (56:9): [True: 50.00%, False: 50.00%]
------------------
57| 199k| build_step_1(v, u);
58| 199k| size[u] += size[v];
59| 199k| if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
^446
------------------
| Branch (59:11): [True: 99.78%, False: 0.22%]
| Branch (59:28): [True: 99.55%, False: 0.45%]
------------------
60| 199k| heavy[u] = v;
61| 199k| }
62| 199k| }
63| 399k| }
64| 199k|}
65| |
66| 199k|def build_step_2(int u, int w, int p, int d) -> void {
67| 199k| int i = ++id;
68| 199k| node2id[u] = i;
69| 199k| depth[i] = d;
70| 199k| ances[i] = node2id[w];
71| 199k| parent[i] = node2id[p];
72| 199k| node[i].v = {a[u], b[u]};
73| 599k| for (int e = head[u]; e; e = edge[e].next) {
^399k
------------------
| Branch (73:25): [True: 66.67%, False: 33.33%]
------------------
74| 399k| int v = edge[e].to;
75| 399k| if (v != p && v != heavy[u]) {
^199k
------------------
| Branch (75:9): [True: 50.00%, False: 50.00%]
| Branch (75:19): [True: 0.22%, False: 99.78%]
------------------
76| 446| build_step_2(v, v, u, d + 1);
77| 446| }
78| 399k| }
79| 199k| priority[i] = u8(log(i ^ (id + 1)));
80| 199k| if (u != w) {
------------------
| Branch (80:7): [True: 99.78%, False: 0.22%]
------------------
81| 199k| int p = parent[i];
82| 199k| int l = 0;
83| 396k| while (p && priority[p] < priority[i]) {
^395k
------------------
| Branch (83:12): [True: 99.58%, False: 0.42%]
| Branch (83:17): [True: 49.98%, False: 50.02%]
------------------
84| 197k| l = p;
85| 197k| p = node[p].p;
86| 197k| }
87| 199k| node[p].r = i;
88| 199k| node[i].p = p;
89| 199k| node[i].l = l;
90| 199k| node[l].p = i;
91| 199k| }
92| 199k| if (int v = heavy[u]; v) {
------------------
| Branch (92:25): [True: 99.78%, False: 0.22%]
------------------
93| 199k| build_step_2(v, w, u, d + 1);
94| 199k| }
95| 199k|}
96| |
97| 199k|def maintain(int u) -> void {
98| 199k| node[u].s = (node[u].t = node[node[u].l].s + node[u].v) + node[node[u].r].s;
99| 199k|}
100| |
101| 400k|template <typename T> T apply(int k, T x) {
102| 800k| for (int u = k; u; u = node[u].p) {
^400k
------------------
| Branch (102:19): [True: 0.00%, False: 0.00%]
------------------
| Branch (102:19): [True: 50.00%, False: 50.00%]
------------------
103| 400k| if (u <= k) {
------------------
| Branch (103:9): [True: 0.00%, False: 0.00%]
------------------
| Branch (103:9): [True: 100.00%, False: 0.00%]
------------------
104| 400k| x = node[u].t + x;
105| 400k| }
106| 400k| }
107| 400k| return x;
108| 400k|}
------------------
| Unexecuted instantiation: vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyIjEET_iS1_
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyINS_7SegmentEEET_iS2_:
| 101| 400k|template <typename T> T apply(int k, T x) {
| 102| 800k| for (int u = k; u; u = node[u].p) {
| ^400k
| ------------------
| | Branch (102:19): [True: 50.00%, False: 50.00%]
| ------------------
| 103| 400k| if (u <= k) {
| ------------------
| | Branch (103:9): [True: 100.00%, False: 0.00%]
| ------------------
| 104| 400k| x = node[u].t + x;
| 105| 400k| }
| 106| 400k| }
| 107| 400k| return x;
| 108| 400k|}
------------------
109| |
110| 200k|template <typename T> T apply(int l, int r, T x) {
111| 200k| Segment t = {1, 0, 0};
112| 200k| int u = l;
113| 200k| int v = r;
114| 3.60M| while (u != v) {
------------------
| Branch (114:10): [True: 0.00%, False: 0.00%]
------------------
| Branch (114:10): [True: 94.44%, False: 5.56%]
------------------
115| 3.40M| if (priority[u] < priority[v]) {
------------------
| Branch (115:9): [True: 0.00%, False: 0.00%]
------------------
| Branch (115:9): [True: 47.06%, False: 52.94%]
------------------
116| 1.60M| if (l <= u) {
------------------
| Branch (116:11): [True: 0.00%, False: 0.00%]
------------------
| Branch (116:11): [True: 100.00%, False: 0.00%]
------------------
117| 1.60M| t = (t + node[u].v) + node[node[u].r].s;
118| 1.60M| }
119| 1.60M| u = node[u].p;
120| 1.80M| } else {
121| 1.80M| if (v <= r) {
------------------
| Branch (121:11): [True: 0.00%, False: 0.00%]
------------------
| Branch (121:11): [True: 88.89%, False: 11.11%]
------------------
122| 1.60M| x = node[v].t + x;
123| 1.60M| }
124| 1.80M| v = node[v].p;
125| 1.80M| }
126| 3.40M| }
127| 200k| x = t + (node[u].v + x);
128| 200k| return x;
129| 200k|}
------------------
| Unexecuted instantiation: vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyIjEET_iiS1_
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyINS_7SegmentEEET_iiS2_:
| 110| 200k|template <typename T> T apply(int l, int r, T x) {
| 111| 200k| Segment t = {1, 0, 0};
| 112| 200k| int u = l;
| 113| 200k| int v = r;
| 114| 3.60M| while (u != v) {
| ------------------
| | Branch (114:10): [True: 94.44%, False: 5.56%]
| ------------------
| 115| 3.40M| if (priority[u] < priority[v]) {
| ------------------
| | Branch (115:9): [True: 47.06%, False: 52.94%]
| ------------------
| 116| 1.60M| if (l <= u) {
| ------------------
| | Branch (116:11): [True: 100.00%, False: 0.00%]
| ------------------
| 117| 1.60M| t = (t + node[u].v) + node[node[u].r].s;
| 118| 1.60M| }
| 119| 1.60M| u = node[u].p;
| 120| 1.80M| } else {
| 121| 1.80M| if (v <= r) {
| ------------------
| | Branch (121:11): [True: 88.89%, False: 11.11%]
| ------------------
| 122| 1.60M| x = node[v].t + x;
| 123| 1.60M| }
| 124| 1.80M| v = node[v].p;
| 125| 1.80M| }
| 126| 3.40M| }
| 127| 200k| x = t + (node[u].v + x);
| 128| 200k| return x;
| 129| 200k|}
------------------
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| 199k| for (int i = 0; i < n; ++i) a[i] = rd.uw(), b[i] = rd.uw();
^199k^199k
------------------
| Branch (145:19): [True: 100.00%, False: 0.00%]
------------------
146| 199k| for (int i = 1; i < n; ++i) {
^199k
------------------
| Branch (146:19): [True: 100.00%, False: 0.00%]
------------------
147| 199k| int u = rd.uh();
148| 199k| int v = rd.uh();
149| 199k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
150| 199k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
151| 199k| }
152| 1| build_step_1(0, 0);
153| 1| build_step_2(0, 0, 0, 0);
154| 199k| for (int i = 1; i <= n; ++i) {
^199k
------------------
| Branch (154:19): [True: 100.00%, False: 0.00%]
------------------
155| 199k| if (node[i].r == 0) {
------------------
| Branch (155:9): [True: 50.11%, False: 49.89%]
------------------
156| 299k| for (int u = i; u && u <= i; u = node[u].p) {
^299k ^199k
------------------
| Branch (156:23): [True: 99.85%, False: 0.15%]
| Branch (156:28): [True: 66.72%, False: 33.28%]
------------------
157| 199k| maintain(u);
158| 199k| }
159| 100k| }
160| 199k| }
161| 200k| while (q--) {
------------------
| Branch (161:10): [True: 100.00%, False: 0.00%]
------------------
162| 200k| let t = rd.u1();
163| 200k| if (t == 0) {
------------------
| Branch (163:9): [True: 0.00%, False: 100.00%]
------------------
164| 0| int u = node2id[rd.uh()];
165| 0| node[u].v = {rd.uw(), rd.uw()};
166| 0| for (; u; u = node[u].p) {
------------------
| Branch (166:14): [True: 0.00%, False: 0.00%]
------------------
167| 0| maintain(u);
168| 0| }
169| 0| }
170| 200k| if (t == 1) {
------------------
| Branch (170:9): [True: 100.00%, False: 0.00%]
------------------
171| 200k| int u = node2id[rd.uh()];
172| 200k| int v = node2id[rd.uh()];
173| 200k| u32 x = rd.uw();
174| 200k| Segment f = {1, 0, 0};
175| 600k| while (ances[u] != ances[v]) {
------------------
| Branch (175:14): [True: 66.67%, False: 33.33%]
------------------
176| 400k| if (depth[ances[u]] >= depth[ances[v]]) {
------------------
| Branch (176:13): [True: 0.00%, False: 100.00%]
------------------
177| 0| x = apply(u, x);
178| 0| u = parent[ances[u]];
179| 400k| } else {
180| 400k| f = apply(v, f);
181| 400k| v = parent[ances[v]];
182| 400k| }
183| 400k| }
184| 200k| if (u >= v) {
------------------
| Branch (184:11): [True: 0.00%, False: 100.00%]
------------------
185| 0| x = apply(v, u, x);
186| 200k| } else {
187| 200k| f = apply(u, v, f);
188| 200k| }
189| 200k| x = x + f;
190| 200k| wt.uw(x);
191| 200k| }
192| 200k| }
193| 1| return 0;
194| 1|}