/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.59M| Segment(Vertex v) : a{v.a}, b{v.b}, c{v.b} {}
17| 3.84M| 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| 3.64M|auto operator+(const Segment &l, const Segment &r) -> Segment {
25| 3.64M| u32 x = u64(l.a) * r.a % P;
26| 3.64M| u32 y = (u64(l.a) * r.b + l.b) % P;
27| 3.64M| u32 z = (u64(r.a) * l.c + r.c) % P;
28| 3.64M| return {x, y, z};
29| 3.64M|}
30| 646k|auto operator+(const Segment &l, u32 r) -> u32 {
31| 646k| return (u64(l.a) * r + l.b) % P;
32| 646k|}
33| 99.9k|auto operator+(u32 l, const Segment &r) -> u32 {
34| 99.9k| return (u64(r.a) * l + r.c) % P;
35| 99.9k|}
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| 200k|def build_step_1(int u, int p) -> void {
53| 200k| 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]]) {
^74.9k
------------------
| Branch (59:11): [True: 62.52%, False: 37.48%]
| Branch (59:28): [True: 75.95%, False: 24.05%]
------------------
60| 181k| heavy[u] = v;
61| 181k| }
62| 199k| }
63| 399k| }
64| 200k|}
65| |
66| 200k|def build_step_2(int u, int w, int p, int d) -> void {
67| 200k| int i = ++id;
68| 200k| node2id[u] = i;
69| 200k| depth[i] = d;
70| 200k| ances[i] = node2id[w];
71| 200k| parent[i] = node2id[p];
72| 200k| 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: 37.48%, False: 62.52%]
------------------
76| 74.9k| build_step_2(v, v, u, d + 1);
77| 74.9k| }
78| 399k| }
79| 200k| priority[i] = u8(log(i ^ (id + 1)));
80| 200k| if (u != w) {
------------------
| Branch (80:7): [True: 62.52%, False: 37.48%]
------------------
81| 125k| int p = parent[i];
82| 125k| int l = 0;
83| 237k| while (p && priority[p] < priority[i]) {
^227k
------------------
| Branch (83:12): [True: 95.71%, False: 4.29%]
| Branch (83:17): [True: 49.53%, False: 50.47%]
------------------
84| 112k| l = p;
85| 112k| p = node[p].p;
86| 112k| }
87| 125k| node[p].r = i;
88| 125k| node[i].p = p;
89| 125k| node[i].l = l;
90| 125k| node[l].p = i;
91| 125k| }
92| 200k| if (int v = heavy[u]; v) {
------------------
| Branch (92:25): [True: 62.52%, False: 37.48%]
------------------
93| 125k| build_step_2(v, w, u, d + 1);
94| 125k| }
95| 200k|}
96| |
97| 1.03M|def maintain(int u) -> void {
98| 1.03M| node[u].s = (node[u].t = node[node[u].l].s + node[u].v) + node[node[u].r].s;
99| 1.03M|}
100| |
101| 145k|template <typename T> T apply(int k, T x) {
102| 822k| for (int u = k; u; u = node[u].p) {
^677k
------------------
| Branch (102:19): [True: 82.40%, False: 17.60%]
------------------
| Branch (102:19): [True: 82.33%, False: 17.67%]
------------------
103| 677k| if (u <= k) {
------------------
| Branch (103:9): [True: 59.44%, False: 40.56%]
------------------
| Branch (103:9): [True: 59.53%, False: 40.47%]
------------------
104| 403k| x = node[u].t + x;
105| 403k| }
106| 677k| }
107| 145k| return x;
108| 145k|}
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyIjEET_iS1_:
| 101| 72.6k|template <typename T> T apply(int k, T x) {
| 102| 412k| for (int u = k; u; u = node[u].p) {
| ^340k
| ------------------
| | Branch (102:19): [True: 82.40%, False: 17.60%]
| ------------------
| 103| 340k| if (u <= k) {
| ------------------
| | Branch (103:9): [True: 59.44%, False: 40.56%]
| ------------------
| 104| 202k| x = node[u].t + x;
| 105| 202k| }
| 106| 340k| }
| 107| 72.6k| return x;
| 108| 72.6k|}
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyINS_7SegmentEEET_iS2_:
| 101| 72.3k|template <typename T> T apply(int k, T x) {
| 102| 409k| for (int u = k; u; u = node[u].p) {
| ^337k
| ------------------
| | Branch (102:19): [True: 82.33%, False: 17.67%]
| ------------------
| 103| 337k| if (u <= k) {
| ------------------
| | Branch (103:9): [True: 59.53%, False: 40.47%]
| ------------------
| 104| 200k| x = node[u].t + x;
| 105| 200k| }
| 106| 337k| }
| 107| 72.3k| return x;
| 108| 72.3k|}
------------------
109| |
110| 99.9k|template <typename T> T apply(int l, int r, T x) {
111| 99.9k| Segment t = {1, 0, 0};
112| 99.9k| int u = l;
113| 99.9k| int v = r;
114| 2.26M| while (u != v) {
------------------
| Branch (114:10): [True: 95.58%, False: 4.42%]
------------------
| Branch (114:10): [True: 95.58%, False: 4.42%]
------------------
115| 2.16M| if (priority[u] < priority[v]) {
------------------
| Branch (115:9): [True: 38.49%, False: 61.51%]
------------------
| Branch (115:9): [True: 38.49%, False: 61.51%]
------------------
116| 832k| if (l <= u) {
------------------
| Branch (116:11): [True: 55.29%, False: 44.71%]
------------------
| Branch (116:11): [True: 55.27%, False: 44.73%]
------------------
117| 459k| t = (t + node[u].v) + node[node[u].r].s;
118| 459k| }
119| 832k| u = node[u].p;
120| 1.32M| } else {
121| 1.32M| if (v <= r) {
------------------
| Branch (121:11): [True: 52.15%, False: 47.85%]
------------------
| Branch (121:11): [True: 52.00%, False: 48.00%]
------------------
122| 692k| x = node[v].t + x;
123| 692k| }
124| 1.32M| v = node[v].p;
125| 1.32M| }
126| 2.16M| }
127| 99.9k| x = t + (node[u].v + x);
128| 99.9k| return x;
129| 99.9k|}
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyIjEET_iiS1_:
| 110| 49.6k|template <typename T> T apply(int l, int r, T x) {
| 111| 49.6k| Segment t = {1, 0, 0};
| 112| 49.6k| int u = l;
| 113| 49.6k| int v = r;
| 114| 1.12M| while (u != v) {
| ------------------
| | Branch (114:10): [True: 95.58%, False: 4.42%]
| ------------------
| 115| 1.07M| if (priority[u] < priority[v]) {
| ------------------
| | Branch (115:9): [True: 38.49%, False: 61.51%]
| ------------------
| 116| 413k| if (l <= u) {
| ------------------
| | Branch (116:11): [True: 55.29%, False: 44.71%]
| ------------------
| 117| 228k| t = (t + node[u].v) + node[node[u].r].s;
| 118| 228k| }
| 119| 413k| u = node[u].p;
| 120| 660k| } else {
| 121| 660k| if (v <= r) {
| ------------------
| | Branch (121:11): [True: 52.15%, False: 47.85%]
| ------------------
| 122| 344k| x = node[v].t + x;
| 123| 344k| }
| 124| 660k| v = node[v].p;
| 125| 660k| }
| 126| 1.07M| }
| 127| 49.6k| x = t + (node[u].v + x);
| 128| 49.6k| return x;
| 129| 49.6k|}
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyINS_7SegmentEEET_iiS2_:
| 110| 50.2k|template <typename T> T apply(int l, int r, T x) {
| 111| 50.2k| Segment t = {1, 0, 0};
| 112| 50.2k| int u = l;
| 113| 50.2k| int v = r;
| 114| 1.13M| while (u != v) {
| ------------------
| | Branch (114:10): [True: 95.58%, False: 4.42%]
| ------------------
| 115| 1.08M| if (priority[u] < priority[v]) {
| ------------------
| | Branch (115:9): [True: 38.49%, False: 61.51%]
| ------------------
| 116| 418k| if (l <= u) {
| ------------------
| | Branch (116:11): [True: 55.27%, False: 44.73%]
| ------------------
| 117| 231k| t = (t + node[u].v) + node[node[u].r].s;
| 118| 231k| }
| 119| 418k| u = node[u].p;
| 120| 669k| } else {
| 121| 669k| if (v <= r) {
| ------------------
| | Branch (121:11): [True: 52.00%, False: 48.00%]
| ------------------
| 122| 347k| x = node[v].t + x;
| 123| 347k| }
| 124| 669k| v = node[v].p;
| 125| 669k| }
| 126| 1.08M| }
| 127| 50.2k| x = t + (node[u].v + x);
| 128| 50.2k| return x;
| 129| 50.2k|}
------------------
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| 200k| for (int i = 0; i < n; ++i) a[i] = rd.uw(), b[i] = rd.uw();
^200k^200k
------------------
| Branch (145:19): [True: 100.00%, False: 0.00%]
------------------
146| 200k| 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| 200k| for (int i = 1; i <= n; ++i) {
^200k
------------------
| Branch (154:19): [True: 100.00%, False: 0.00%]
------------------
155| 200k| if (node[i].r == 0) {
------------------
| Branch (155:9): [True: 68.30%, False: 31.70%]
------------------
156| 336k| for (int u = i; u && u <= i; u = node[u].p) {
^261k ^200k
------------------
| Branch (156:23): [True: 77.73%, False: 22.27%]
| Branch (156:28): [True: 76.44%, False: 23.56%]
------------------
157| 200k| maintain(u);
158| 200k| }
159| 136k| }
160| 200k| }
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: 50.03%, False: 49.97%]
------------------
164| 100k| int u = node2id[rd.uh()];
165| 100k| node[u].v = {rd.uw(), rd.uw()};
166| 937k| for (; u; u = node[u].p) {
^837k
------------------
| Branch (166:14): [True: 89.32%, False: 10.68%]
------------------
167| 837k| maintain(u);
168| 837k| }
169| 100k| }
170| 200k| if (t == 1) {
------------------
| Branch (170:9): [True: 49.97%, False: 50.03%]
------------------
171| 99.9k| int u = node2id[rd.uh()];
172| 99.9k| int v = node2id[rd.uh()];
173| 99.9k| u32 x = rd.uw();
174| 99.9k| Segment f = {1, 0, 0};
175| 244k| while (ances[u] != ances[v]) {
------------------
| Branch (175:14): [True: 59.20%, False: 40.80%]
------------------
176| 145k| if (depth[ances[u]] >= depth[ances[v]]) {
------------------
| Branch (176:13): [True: 50.10%, False: 49.90%]
------------------
177| 72.6k| x = apply(u, x);
178| 72.6k| u = parent[ances[u]];
179| 72.6k| } else {
180| 72.3k| f = apply(v, f);
181| 72.3k| v = parent[ances[v]];
182| 72.3k| }
183| 145k| }
184| 99.9k| if (u >= v) {
------------------
| Branch (184:11): [True: 49.70%, False: 50.30%]
------------------
185| 49.6k| x = apply(v, u, x);
186| 50.2k| } else {
187| 50.2k| f = apply(u, v, f);
188| 50.2k| }
189| 99.9k| x = x + f;
190| 99.9k| wt.uw(x);
191| 99.9k| }
192| 200k| }
193| 1| return 0;
194| 1|}