/tmp/solutions/build/dynamic_tree_vertex_set_path_composite-main.cpp:
1| |#include <common.h>
2| |prelude;
3| |
4| |namespace {
5| |
6| |constexpr int N = 200001;
7| |constexpr u32 P = 998244353;
8| |
9| |struct Affine {
10| | u32 a, b, c;
11| 6.40k| auto operator+(const Affine &t) const -> Affine {
12| 6.40k| u32 x = (u64(a) * t.a) % P;
13| 6.40k| u32 y = (u64(a) * t.b + b) % P;
14| 6.40k| u32 z = (u64(t.a) * c + t.c) % P;
15| 6.40k| return {x, y, z};
16| 6.40k| }
17| 218| auto operator+(const u32 &t) const -> u32 { return (u64(a) * t + b) % P; }
18| |};
19| |struct Node {
20| | u32 a;
21| | u32 b;
22| | Affine aff;
23| | int p;
24| | int ch[2];
25| | i8 rev;
26| | i8 k;
27| |} node[N];
28| |
29| 3.11k|def reverse(int x) -> void {
30| 3.11k| node[x].rev ^= 1;
31| 3.11k| std::swap(node[x].aff.b, node[x].aff.c);
32| 3.11k|}
33| |
34| 5.02k|def pushdown(int x) -> void {
35| 5.02k| if (node[x].rev) {
------------------
| Branch (35:7): [True: 28.09%, False: 71.91%]
------------------
36| 1.41k| std::swap(node[x].ch[0], node[x].ch[1]);
37| 1.41k| node[node[x].ch[0]].k = 0;
38| 1.41k| node[node[x].ch[1]].k = 1;
39| 1.41k| reverse(node[x].ch[0]);
40| 1.41k| reverse(node[x].ch[1]);
41| 1.41k| node[x].rev = 0;
42| 1.41k| }
43| 5.02k|}
44| |
45| 3.20k|def maintain(int x) -> void {
46| 3.20k| node[x].aff = node[node[x].ch[0]].aff +
47| 3.20k| Affine{node[x].a, node[x].b, node[x].b} +
48| 3.20k| node[node[x].ch[1]].aff;
49| 3.20k|}
50| |
51| 3.20k|def rotateup(int x) -> void {
52| 3.20k| int p = node[x].p;
53| 3.20k| int g = node[p].p;
54| 3.20k| int k = node[x].k;
55| 3.20k| int t = node[p].k;
56| 3.20k| node[node[x].ch[k ^ 1]].p = p;
57| 3.20k| node[node[x].ch[k ^ 1]].k = k;
58| 3.20k| node[p].ch[k] = node[x].ch[k ^ 1];
59| 3.20k| node[p].p = x;
60| 3.20k| node[p].k = k ^ 1;
61| 3.20k| node[x].ch[k ^ 1] = p;
62| 3.20k| node[x].p = g;
63| 3.20k| node[x].k = t;
64| 3.20k| if (t != -1) {
------------------
| Branch (64:7): [True: 24.72%, False: 75.28%]
------------------
65| 792| node[g].ch[t] = x;
66| 792| }
67| 3.20k| maintain(p);
68| 3.20k|}
69| |
70| 4.40k|def is_root(int x) -> bool { return node[x].k == -1; }
71| |
72| 1.96k|def splay(int x) -> void {
73| 1.96k| pushdown(x);
74| 3.18k| while (!is_root(x)) {
------------------
| Branch (74:10): [True: 38.44%, False: 61.56%]
------------------
75| 1.22k| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (75:28): [True: 50.00%, False: 50.00%]
------------------
76| 612| pushdown(p);
77| 612| pushdown(x);
78| 612| rotateup(x);
79| 612| } else {
80| 612| int g = node[p].p;
81| 612| pushdown(g);
82| 612| pushdown(p);
83| 612| pushdown(x);
84| 612| if (node[x].k == node[p].k) {
------------------
| Branch (84:11): [True: 39.22%, False: 60.78%]
------------------
85| 240| rotateup(p);
86| 240| rotateup(x);
87| 372| } else {
88| 372| rotateup(x);
89| 372| rotateup(x);
90| 372| }
91| 612| }
92| 1.22k| }
93| 1.96k|}
94| |
95| 494|def access(int x) -> void {
96| 494| splay(x);
97| 494| node[node[x].ch[1]].k = -1;
98| 494| node[x].ch[1] = 0;
99| 1.86k| while (int p = node[x].p) {
------------------
| Branch (99:14): [True: 73.47%, False: 26.53%]
------------------
100| 1.36k| splay(p);
101| 1.36k| node[node[p].ch[1]].k = -1;
102| 1.36k| node[p].ch[1] = x;
103| 1.36k| node[x].k = 1;
104| 1.36k| rotateup(x);
105| 1.36k| }
106| 494|}
107| |
108| |int head[N];
109| |struct {
110| | int to;
111| | int next;
112| |} edge[N * 2];
113| |
114| 693|def build(int u, int p) -> void {
115| 2.07k| for (int e = head[u]; e; e = edge[e].next) {
^1.38k
------------------
| Branch (115:25): [True: 66.63%, False: 33.37%]
------------------
116| 1.38k| int v = edge[e].to;
117| 1.38k| if (v != p) {
------------------
| Branch (117:9): [True: 50.00%, False: 50.00%]
------------------
118| 692| build(v, u);
119| 692| node[v + 1].p = u + 1;
120| 692| }
121| 1.38k| }
122| 693|}
123| |
124| |} // namespace
125| |
126| 1|int main() {
127| 1| node[0].aff.a = 1;
128| 1| rd rd;
129| 1| wt wt;
130| 1| int n = rd.uh();
131| 1| int q = rd.uh();
132| 1|#ifdef LOCAL
133| 1| std::memset(head, 0, 4 * n);
134| 1|#endif
135| 694| for (int i = 1; i <= n; ++i) {
^693
------------------
| Branch (135:19): [True: 99.86%, False: 0.14%]
------------------
136| 693| node[i] = {.k = -1};
137| 693| u32 a = node[i].a = rd.uw();
138| 693| u32 b = node[i].b = rd.uw();
139| 693| node[i].aff = {a, b, b};
140| 693| }
141| 693| for (int i = 1; i != n; ++i) {
^692
------------------
| Branch (141:19): [True: 99.86%, False: 0.14%]
------------------
142| 692| int u = rd.uh();
143| 692| int v = rd.uh();
144| 692| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
145| 692| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
146| 692| }
147| 1| build(0, 0);
148| 300| while (q--) {
------------------
| Branch (148:10): [True: 99.67%, False: 0.33%]
------------------
149| 299| let t = rd.u1();
150| 299| if (t == 0) {
------------------
| Branch (150:9): [True: 30.77%, False: 69.23%]
------------------
151| 92| int u = rd.uh() + 1;
152| 92| int v = rd.uh() + 1;
153| 92| access(u);
154| 92| reverse(u);
155| 92| access(v);
156| 92| node[node[v].ch[0]].p = 0;
157| 92| node[node[v].ch[0]].k = -1;
158| 92| node[v].ch[0] = 0;
159| 92| int w = rd.uh() + 1;
160| 92| int x = rd.uh() + 1;
161| 92| access(w);
162| 92| reverse(w);
163| 92| node[w].p = x;
164| 92| }
165| 299| if (t == 1) {
------------------
| Branch (165:9): [True: 32.78%, False: 67.22%]
------------------
166| 98| int u = rd.uh() + 1;
167| 98| node[u].a = rd.uw();
168| 98| node[u].b = rd.uw();
169| 98| splay(u);
170| 98| }
171| 299| if (t == 2) {
------------------
| Branch (171:9): [True: 36.45%, False: 63.55%]
------------------
172| 109| int u = rd.uh() + 1;
173| 109| int v = rd.uh() + 1;
174| 109| u32 x = rd.uw();
175| 109| access(v);
176| 109| reverse(v);
177| 109| access(u);
178| 109| x = Affine{node[u].a, node[u].b, node[u].b} + x;
179| 109| x = node[node[u].ch[0]].aff + x;
180| 109| wt.ud(x);
181| 109| }
182| 299| }
183| 1| return 0;
184| 1|}