/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| 579k| Segment(Vertex v) : a{v.a}, b{v.b}, c{v.b} {}
17| 1.91M| 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.71M|auto operator+(const Segment &l, const Segment &r) -> Segment {
25| 1.71M| u32 x = u64(l.a) * r.a % P;
26| 1.71M| u32 y = (u64(l.a) * r.b + l.b) % P;
27| 1.71M| u32 z = (u64(r.a) * l.c + r.c) % P;
28| 1.71M| return {x, y, z};
29| 1.71M|}
30| 787k|auto operator+(const Segment &l, u32 r) -> u32 {
31| 787k| return (u64(l.a) * r + l.b) % P;
32| 787k|}
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]]) {
^99.6k
------------------
| Branch (59:11): [True: 50.18%, False: 49.82%]
| Branch (59:28): [True: 46.84%, False: 53.16%]
------------------
60| 147k| heavy[u] = v;
61| 147k| }
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: 49.82%, False: 50.18%]
------------------
76| 99.6k| build_step_2(v, v, u, d + 1);
77| 99.6k| }
78| 399k| }
79| 200k| priority[i] = u8(log(i ^ (id + 1)));
80| 200k| if (u != w) {
------------------
| Branch (80:7): [True: 50.18%, False: 49.82%]
------------------
81| 100k| int p = parent[i];
82| 100k| int l = 0;
83| 155k| while (p && priority[p] < priority[i]) {
^129k
------------------
| Branch (83:12): [True: 83.50%, False: 16.50%]
| Branch (83:17): [True: 42.31%, False: 57.69%]
------------------
84| 54.8k| l = p;
85| 54.8k| p = node[p].p;
86| 54.8k| }
87| 100k| node[p].r = i;
88| 100k| node[i].p = p;
89| 100k| node[i].l = l;
90| 100k| node[l].p = i;
91| 100k| }
92| 200k| if (int v = heavy[u]; v) {
------------------
| Branch (92:25): [True: 50.18%, False: 49.82%]
------------------
93| 100k| build_step_2(v, w, u, d + 1);
94| 100k| }
95| 200k|}
96| |
97| 386k|def maintain(int u) -> void {
98| 386k| node[u].s = (node[u].t = node[node[u].l].s + node[u].v) + node[node[u].r].s;
99| 386k|}
100| |
101| 809k|template <typename T> T apply(int k, T x) {
102| 2.46M| for (int u = k; u; u = node[u].p) {
^1.65M
------------------
| Branch (102:19): [True: 67.18%, False: 32.82%]
------------------
| Branch (102:19): [True: 67.19%, False: 32.81%]
------------------
103| 1.65M| if (u <= k) {
------------------
| Branch (103:9): [True: 76.75%, False: 23.25%]
------------------
| Branch (103:9): [True: 76.76%, False: 23.24%]
------------------
104| 1.27M| x = node[u].t + x;
105| 1.27M| }
106| 1.65M| }
107| 809k| return x;
108| 809k|}
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyIjEET_iS1_:
| 101| 404k|template <typename T> T apply(int k, T x) {
| 102| 1.23M| for (int u = k; u; u = node[u].p) {
| ^828k
| ------------------
| | Branch (102:19): [True: 67.18%, False: 32.82%]
| ------------------
| 103| 828k| if (u <= k) {
| ------------------
| | Branch (103:9): [True: 76.75%, False: 23.25%]
| ------------------
| 104| 636k| x = node[u].t + x;
| 105| 636k| }
| 106| 828k| }
| 107| 404k| return x;
| 108| 404k|}
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyINS_7SegmentEEET_iS2_:
| 101| 404k|template <typename T> T apply(int k, T x) {
| 102| 1.23M| for (int u = k; u; u = node[u].p) {
| ^829k
| ------------------
| | Branch (102:19): [True: 67.19%, False: 32.81%]
| ------------------
| 103| 829k| if (u <= k) {
| ------------------
| | Branch (103:9): [True: 76.76%, False: 23.24%]
| ------------------
| 104| 636k| x = node[u].t + x;
| 105| 636k| }
| 106| 829k| }
| 107| 404k| return x;
| 108| 404k|}
------------------
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| 310k| while (u != v) {
------------------
| Branch (114:10): [True: 64.27%, False: 35.73%]
------------------
| Branch (114:10): [True: 71.77%, False: 28.23%]
------------------
115| 210k| if (priority[u] < priority[v]) {
------------------
| Branch (115:9): [True: 48.29%, False: 51.71%]
------------------
| Branch (115:9): [True: 48.17%, False: 51.83%]
------------------
116| 101k| if (l <= u) {
------------------
| Branch (116:11): [True: 91.43%, False: 8.57%]
------------------
| Branch (116:11): [True: 91.41%, False: 8.59%]
------------------
117| 92.8k| t = (t + node[u].v) + node[node[u].r].s;
118| 92.8k| }
119| 101k| u = node[u].p;
120| 109k| } else {
121| 109k| if (v <= r) {
------------------
| Branch (121:11): [True: 62.49%, False: 37.51%]
------------------
| Branch (121:11): [True: 62.56%, False: 37.44%]
------------------
122| 68.1k| x = node[v].t + x;
123| 68.1k| }
124| 109k| v = node[v].p;
125| 109k| }
126| 210k| }
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| 58.6k|template <typename T> T apply(int l, int r, T x) {
| 111| 58.6k| Segment t = {1, 0, 0};
| 112| 58.6k| int u = l;
| 113| 58.6k| int v = r;
| 114| 164k| while (u != v) {
| ------------------
| | Branch (114:10): [True: 64.27%, False: 35.73%]
| ------------------
| 115| 105k| if (priority[u] < priority[v]) {
| ------------------
| | Branch (115:9): [True: 48.29%, False: 51.71%]
| ------------------
| 116| 50.9k| if (l <= u) {
| ------------------
| | Branch (116:11): [True: 91.43%, False: 8.57%]
| ------------------
| 117| 46.5k| t = (t + node[u].v) + node[node[u].r].s;
| 118| 46.5k| }
| 119| 50.9k| u = node[u].p;
| 120| 54.5k| } else {
| 121| 54.5k| if (v <= r) {
| ------------------
| | Branch (121:11): [True: 62.49%, False: 37.51%]
| ------------------
| 122| 34.0k| x = node[v].t + x;
| 123| 34.0k| }
| 124| 54.5k| v = node[v].p;
| 125| 54.5k| }
| 126| 105k| }
| 127| 58.6k| x = t + (node[u].v + x);
| 128| 58.6k| return x;
| 129| 58.6k|}
------------------
| vertex_set_path_composite-logn.cpp:_ZN12_GLOBAL__N_15applyINS_7SegmentEEET_iiS2_:
| 110| 41.3k|template <typename T> T apply(int l, int r, T x) {
| 111| 41.3k| Segment t = {1, 0, 0};
| 112| 41.3k| int u = l;
| 113| 41.3k| int v = r;
| 114| 146k| while (u != v) {
| ------------------
| | Branch (114:10): [True: 71.77%, False: 28.23%]
| ------------------
| 115| 105k| if (priority[u] < priority[v]) {
| ------------------
| | Branch (115:9): [True: 48.17%, False: 51.83%]
| ------------------
| 116| 50.6k| if (l <= u) {
| ------------------
| | Branch (116:11): [True: 91.41%, False: 8.59%]
| ------------------
| 117| 46.3k| t = (t + node[u].v) + node[node[u].r].s;
| 118| 46.3k| }
| 119| 50.6k| u = node[u].p;
| 120| 54.5k| } else {
| 121| 54.5k| if (v <= r) {
| ------------------
| | Branch (121:11): [True: 62.56%, False: 37.44%]
| ------------------
| 122| 34.1k| x = node[v].t + x;
| 123| 34.1k| }
| 124| 54.5k| v = node[v].p;
| 125| 54.5k| }
| 126| 105k| }
| 127| 41.3k| x = t + (node[u].v + x);
| 128| 41.3k| return x;
| 129| 41.3k|}
------------------
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: 70.90%, False: 29.10%]
------------------
156| 341k| for (int u = i; u && u <= i; u = node[u].p) {
^242k ^200k
------------------
| Branch (156:23): [True: 70.85%, False: 29.15%]
| Branch (156:28): [True: 82.59%, False: 17.41%]
------------------
157| 200k| maintain(u);
158| 200k| }
159| 141k| }
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.01%, False: 49.99%]
------------------
164| 100k| int u = node2id[rd.uh()];
165| 100k| node[u].v = {rd.uw(), rd.uw()};
166| 286k| for (; u; u = node[u].p) {
^186k
------------------
| Branch (166:14): [True: 65.06%, False: 34.94%]
------------------
167| 186k| maintain(u);
168| 186k| }
169| 100k| }
170| 200k| if (t == 1) {
------------------
| Branch (170:9): [True: 49.99%, False: 50.01%]
------------------
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| 909k| while (ances[u] != ances[v]) {
------------------
| Branch (175:14): [True: 89.01%, False: 10.99%]
------------------
176| 809k| if (depth[ances[u]] >= depth[ances[v]]) {
------------------
| Branch (176:13): [True: 50.01%, False: 49.99%]
------------------
177| 404k| x = apply(u, x);
178| 404k| u = parent[ances[u]];
179| 404k| } else {
180| 404k| f = apply(v, f);
181| 404k| v = parent[ances[v]];
182| 404k| }
183| 809k| }
184| 99.9k| if (u >= v) {
------------------
| Branch (184:11): [True: 58.63%, False: 41.37%]
------------------
185| 58.6k| x = apply(v, u, x);
186| 58.6k| } else {
187| 41.3k| f = apply(u, v, f);
188| 41.3k| }
189| 99.9k| x = x + f;
190| 99.9k| wt.uw(x);
191| 99.9k| }
192| 200k| }
193| 1| return 0;
194| 1|}