/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| 456k| Segment(Vertex v) : a{v.a}, b{v.b}, c{v.b} {}
17| 1.44M| 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.29M|auto operator+(const Segment &l, const Segment &r) -> Segment {
25| 1.29M| u32 x = u64(l.a) * r.a % P;
26| 1.29M| u32 y = (u64(l.a) * r.b + l.b) % P;
27| 1.29M| u32 z = (u64(r.a) * l.c + r.c) % P;
28| 1.29M| return {x, y, z};
29| 1.29M|}
30| 550k|auto operator+(const Segment &l, u32 r) -> u32 {
31| 550k| return (u64(l.a) * r + l.b) % P;
32| 550k|}
33| 74.6k|auto operator+(u32 l, const Segment &r) -> u32 {
34| 74.6k| return (u64(r.a) * l + r.c) % P;
35| 74.6k|}
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| 150k|def build_step_1(int u, int p) -> void {
53| 150k| size[u] = 1;
54| 452k| for (int e = head[u]; e; e = edge[e].next) {
^301k
------------------
| Branch (54:25): [True: 66.67%, False: 33.33%]
------------------
55| 301k| int v = edge[e].to;
56| 301k| if (v != p) {
------------------
| Branch (56:9): [True: 50.00%, False: 50.00%]
------------------
57| 150k| build_step_1(v, u);
58| 150k| size[u] += size[v];
59| 150k| if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
^75.4k
------------------
| Branch (59:11): [True: 49.93%, False: 50.07%]
| Branch (59:28): [True: 46.82%, False: 53.18%]
------------------
60| 110k| heavy[u] = v;
61| 110k| }
62| 150k| }
63| 301k| }
64| 150k|}
65| |
66| 150k|def build_step_2(int u, int w, int p, int d) -> void {
67| 150k| int i = ++id;
68| 150k| node2id[u] = i;
69| 150k| depth[i] = d;
70| 150k| ances[i] = node2id[w];
71| 150k| parent[i] = node2id[p];
72| 150k| node[i].v = {a[u], b[u]};
73| 452k| for (int e = head[u]; e; e = edge[e].next) {
^301k
------------------
| Branch (73:25): [True: 66.67%, False: 33.33%]
------------------
74| 301k| int v = edge[e].to;
75| 301k| if (v != p && v != heavy[u]) {
^150k
------------------
| Branch (75:9): [True: 50.00%, False: 50.00%]
| Branch (75:19): [True: 50.07%, False: 49.93%]
------------------
76| 75.4k| build_step_2(v, v, u, d + 1);
77| 75.4k| }
78| 301k| }
79| 150k| priority[i] = u8(log(i ^ (id + 1)));
80| 150k| if (u != w) {
------------------
| Branch (80:7): [True: 49.93%, False: 50.07%]
------------------
81| 75.2k| int p = parent[i];
82| 75.2k| int l = 0;
83| 116k| while (p && priority[p] < priority[i]) {
^97.1k
------------------
| Branch (83:12): [True: 83.61%, False: 16.39%]
| Branch (83:17): [True: 42.12%, False: 57.88%]
------------------
84| 40.9k| l = p;
85| 40.9k| p = node[p].p;
86| 40.9k| }
87| 75.2k| node[p].r = i;
88| 75.2k| node[i].p = p;
89| 75.2k| node[i].l = l;
90| 75.2k| node[l].p = i;
91| 75.2k| }
92| 150k| if (int v = heavy[u]; v) {
------------------
| Branch (92:25): [True: 49.93%, False: 50.07%]
------------------
93| 75.2k| build_step_2(v, w, u, d + 1);
94| 75.2k| }
95| 150k|}
96| |
97| 287k|def maintain(int u) -> void {
98| 287k| node[u].s = (node[u].t = node[node[u].l].s + node[u].v) + node[node[u].r].s;
99| 287k|}
100| |
101| 567k|template <typename T> T apply(int k, T x) {
102| 1.74M| for (int u = k; u; u = node[u].p) {
^1.17M
------------------
| Branch (102:19): [True: 67.49%, False: 32.51%]
------------------
| Branch (102:19): [True: 67.50%, False: 32.50%]
------------------
103| 1.17M| if (u <= k) {
------------------
| Branch (103:9): [True: 74.81%, False: 25.19%]
------------------
| Branch (103:9): [True: 74.77%, False: 25.23%]
------------------
104| 881k| x = node[u].t + x;
105| 881k| }
106| 1.17M| }
107| 567k| return x;
108| 567k|}
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyIjEET_iS1_:
| 101| 283k|template <typename T> T apply(int k, T x) {
| 102| 872k| for (int u = k; u; u = node[u].p) {
| ^589k
| ------------------
| | Branch (102:19): [True: 67.49%, False: 32.51%]
| ------------------
| 103| 589k| if (u <= k) {
| ------------------
| | Branch (103:9): [True: 74.81%, False: 25.19%]
| ------------------
| 104| 440k| x = node[u].t + x;
| 105| 440k| }
| 106| 589k| }
| 107| 283k| return x;
| 108| 283k|}
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyINS_7SegmentEEET_iS2_:
| 101| 283k|template <typename T> T apply(int k, T x) {
| 102| 873k| for (int u = k; u; u = node[u].p) {
| ^589k
| ------------------
| | Branch (102:19): [True: 67.50%, False: 32.50%]
| ------------------
| 103| 589k| if (u <= k) {
| ------------------
| | Branch (103:9): [True: 74.77%, False: 25.23%]
| ------------------
| 104| 440k| x = node[u].t + x;
| 105| 440k| }
| 106| 589k| }
| 107| 283k| return x;
| 108| 283k|}
------------------
109| |
110| 74.6k|template <typename T> T apply(int l, int r, T x) {
111| 74.6k| Segment t = {1, 0, 0};
112| 74.6k| int u = l;
113| 74.6k| int v = r;
114| 241k| while (u != v) {
------------------
| Branch (114:10): [True: 66.64%, False: 33.36%]
------------------
| Branch (114:10): [True: 71.88%, False: 28.12%]
------------------
115| 167k| if (priority[u] < priority[v]) {
------------------
| Branch (115:9): [True: 61.50%, False: 38.50%]
------------------
| Branch (115:9): [True: 61.53%, False: 38.47%]
------------------
116| 102k| if (l <= u) {
------------------
| Branch (116:11): [True: 90.70%, False: 9.30%]
------------------
| Branch (116:11): [True: 91.06%, False: 8.94%]
------------------
117| 93.5k| t = (t + node[u].v) + node[node[u].r].s;
118| 93.5k| }
119| 102k| u = node[u].p;
120| 102k| } else {
121| 64.4k| if (v <= r) {
------------------
| Branch (121:11): [True: 81.30%, False: 18.70%]
------------------
| Branch (121:11): [True: 81.57%, False: 18.43%]
------------------
122| 52.4k| x = node[v].t + x;
123| 52.4k| }
124| 64.4k| v = node[v].p;
125| 64.4k| }
126| 167k| }
127| 74.6k| x = t + (node[u].v + x);
128| 74.6k| return x;
129| 74.6k|}
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyIjEET_iiS1_:
| 110| 41.7k|template <typename T> T apply(int l, int r, T x) {
| 111| 41.7k| Segment t = {1, 0, 0};
| 112| 41.7k| int u = l;
| 113| 41.7k| int v = r;
| 114| 125k| while (u != v) {
| ------------------
| | Branch (114:10): [True: 66.64%, False: 33.36%]
| ------------------
| 115| 83.3k| if (priority[u] < priority[v]) {
| ------------------
| | Branch (115:9): [True: 61.50%, False: 38.50%]
| ------------------
| 116| 51.2k| if (l <= u) {
| ------------------
| | Branch (116:11): [True: 90.70%, False: 9.30%]
| ------------------
| 117| 46.4k| t = (t + node[u].v) + node[node[u].r].s;
| 118| 46.4k| }
| 119| 51.2k| u = node[u].p;
| 120| 51.2k| } else {
| 121| 32.0k| if (v <= r) {
| ------------------
| | Branch (121:11): [True: 81.30%, False: 18.70%]
| ------------------
| 122| 26.0k| x = node[v].t + x;
| 123| 26.0k| }
| 124| 32.0k| v = node[v].p;
| 125| 32.0k| }
| 126| 83.3k| }
| 127| 41.7k| x = t + (node[u].v + x);
| 128| 41.7k| return x;
| 129| 41.7k|}
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyINS_7SegmentEEET_iiS2_:
| 110| 32.8k|template <typename T> T apply(int l, int r, T x) {
| 111| 32.8k| Segment t = {1, 0, 0};
| 112| 32.8k| int u = l;
| 113| 32.8k| int v = r;
| 114| 116k| while (u != v) {
| ------------------
| | Branch (114:10): [True: 71.88%, False: 28.12%]
| ------------------
| 115| 84.0k| if (priority[u] < priority[v]) {
| ------------------
| | Branch (115:9): [True: 61.53%, False: 38.47%]
| ------------------
| 116| 51.7k| if (l <= u) {
| ------------------
| | Branch (116:11): [True: 91.06%, False: 8.94%]
| ------------------
| 117| 47.0k| t = (t + node[u].v) + node[node[u].r].s;
| 118| 47.0k| }
| 119| 51.7k| u = node[u].p;
| 120| 51.7k| } else {
| 121| 32.3k| if (v <= r) {
| ------------------
| | Branch (121:11): [True: 81.57%, False: 18.43%]
| ------------------
| 122| 26.3k| x = node[v].t + x;
| 123| 26.3k| }
| 124| 32.3k| v = node[v].p;
| 125| 32.3k| }
| 126| 84.0k| }
| 127| 32.8k| x = t + (node[u].v + x);
| 128| 32.8k| return x;
| 129| 32.8k|}
------------------
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| 150k| for (int i = 0; i < n; ++i) a[i] = rd.uw(), b[i] = rd.uw();
^150k^150k
------------------
| Branch (145:19): [True: 100.00%, False: 0.00%]
------------------
146| 150k| for (int i = 1; i < n; ++i) {
^150k
------------------
| Branch (146:19): [True: 100.00%, False: 0.00%]
------------------
147| 150k| int u = rd.uh();
148| 150k| int v = rd.uh();
149| 150k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
150| 150k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
151| 150k| }
152| 1| build_step_1(0, 0);
153| 1| build_step_2(0, 0, 0, 0);
154| 150k| for (int i = 1; i <= n; ++i) {
^150k
------------------
| Branch (154:19): [True: 100.00%, False: 0.00%]
------------------
155| 150k| if (node[i].r == 0) {
------------------
| Branch (155:9): [True: 70.96%, False: 29.04%]
------------------
156| 257k| for (int u = i; u && u <= i; u = node[u].p) {
^182k ^150k
------------------
| Branch (156:23): [True: 70.71%, False: 29.29%]
| Branch (156:28): [True: 82.73%, False: 17.27%]
------------------
157| 150k| maintain(u);
158| 150k| }
159| 106k| }
160| 150k| }
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.85%, False: 50.15%]
------------------
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.91%, False: 35.09%]
------------------
167| 137k| maintain(u);
168| 137k| }
169| 74.1k| }
170| 148k| if (t == 1) {
------------------
| Branch (170:9): [True: 50.15%, False: 49.85%]
------------------
171| 74.6k| int u = node2id[rd.uh()];
172| 74.6k| int v = node2id[rd.uh()];
173| 74.6k| u32 x = rd.uw();
174| 74.6k| Segment f = {1, 0, 0};
175| 642k| while (ances[u] != ances[v]) {
------------------
| Branch (175:14): [True: 88.38%, False: 11.62%]
------------------
176| 567k| if (depth[ances[u]] >= depth[ances[v]]) {
------------------
| Branch (176:13): [True: 49.98%, False: 50.02%]
------------------
177| 283k| x = apply(u, x);
178| 283k| u = parent[ances[u]];
179| 283k| } else {
180| 283k| f = apply(v, f);
181| 283k| v = parent[ances[v]];
182| 283k| }
183| 567k| }
184| 74.6k| if (u >= v) {
------------------
| Branch (184:11): [True: 55.93%, False: 44.07%]
------------------
185| 41.7k| x = apply(v, u, x);
186| 41.7k| } else {
187| 32.8k| f = apply(u, v, f);
188| 32.8k| }
189| 74.6k| x = x + f;
190| 74.6k| wt.uw(x);
191| 74.6k| }
192| 148k| }
193| 1| return 0;
194| 1|}