/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| 886k| auto operator+(const Affine &t) const -> Affine {
12| 886k| u32 x = (u64(a) * t.a) % P;
13| 886k| u32 y = (u64(a) * t.b + b) % P;
14| 886k| u32 z = (u64(t.a) * c + t.c) % P;
15| 886k| return {x, y, z};
16| 886k| }
17| 17.0k| 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| 291k|def reverse(int x) -> void {
30| 291k| node[x].rev ^= 1;
31| 291k| std::swap(node[x].aff.b, node[x].aff.c);
32| 291k|}
33| |
34| 664k|def pushdown(int x) -> void {
35| 664k| if (node[x].rev) {
------------------
| Branch (35:7): [True: 20.00%, False: 80.00%]
------------------
36| 132k| std::swap(node[x].ch[0], node[x].ch[1]);
37| 132k| node[node[x].ch[0]].k = 0;
38| 132k| node[node[x].ch[1]].k = 1;
39| 132k| reverse(node[x].ch[0]);
40| 132k| reverse(node[x].ch[1]);
41| 132k| node[x].rev = 0;
42| 132k| }
43| 664k|}
44| |
45| 443k|def maintain(int x) -> void {
46| 443k| node[x].aff = node[node[x].ch[0]].aff +
47| 443k| Affine{node[x].a, node[x].b, node[x].b} +
48| 443k| node[node[x].ch[1]].aff;
49| 443k|}
50| |
51| 443k|def rotateup(int x) -> void {
52| 443k| int p = node[x].p;
53| 443k| int g = node[p].p;
54| 443k| int k = node[x].k;
55| 443k| int t = node[p].k;
56| 443k| node[node[x].ch[k ^ 1]].p = p;
57| 443k| node[node[x].ch[k ^ 1]].k = k;
58| 443k| node[p].ch[k] = node[x].ch[k ^ 1];
59| 443k| node[p].p = x;
60| 443k| node[p].k = k ^ 1;
61| 443k| node[x].ch[k ^ 1] = p;
62| 443k| node[x].p = g;
63| 443k| node[x].k = t;
64| 443k| if (t != -1) {
------------------
| Branch (64:7): [True: 23.44%, False: 76.56%]
------------------
65| 103k| node[g].ch[t] = x;
66| 103k| }
67| 443k| maintain(p);
68| 443k|}
69| |
70| 580k|def is_root(int x) -> bool { return node[x].k == -1; }
71| |
72| 242k|def splay(int x) -> void {
73| 242k| pushdown(x);
74| 411k| while (!is_root(x)) {
------------------
| Branch (74:10): [True: 41.14%, False: 58.86%]
------------------
75| 169k| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (75:28): [True: 50.43%, False: 49.57%]
------------------
76| 85.3k| pushdown(p);
77| 85.3k| pushdown(x);
78| 85.3k| rotateup(x);
79| 85.3k| } else {
80| 83.8k| int g = node[p].p;
81| 83.8k| pushdown(g);
82| 83.8k| pushdown(p);
83| 83.8k| pushdown(x);
84| 83.8k| if (node[x].k == node[p].k) {
------------------
| Branch (84:11): [True: 42.60%, False: 57.40%]
------------------
85| 35.7k| rotateup(p);
86| 35.7k| rotateup(x);
87| 48.1k| } else {
88| 48.1k| rotateup(x);
89| 48.1k| rotateup(x);
90| 48.1k| }
91| 83.8k| }
92| 169k| }
93| 242k|}
94| |
95| 43.0k|def access(int x) -> void {
96| 43.0k| splay(x);
97| 43.0k| node[node[x].ch[1]].k = -1;
98| 43.0k| node[x].ch[1] = 0;
99| 233k| while (int p = node[x].p) {
------------------
| Branch (99:14): [True: 81.56%, False: 18.44%]
------------------
100| 190k| splay(p);
101| 190k| node[node[p].ch[1]].k = -1;
102| 190k| node[p].ch[1] = x;
103| 190k| node[x].k = 1;
104| 190k| rotateup(x);
105| 190k| }
106| 43.0k|}
107| |
108| |int head[N];
109| |struct {
110| | int to;
111| | int next;
112| |} edge[N * 2];
113| |
114| 167k|def build(int u, int p) -> void {
115| 501k| for (int e = head[u]; e; e = edge[e].next) {
^334k
------------------
| Branch (115:25): [True: 66.67%, False: 33.33%]
------------------
116| 334k| int v = edge[e].to;
117| 334k| if (v != p) {
------------------
| Branch (117:9): [True: 50.00%, False: 50.00%]
------------------
118| 167k| build(v, u);
119| 167k| node[v + 1].p = u + 1;
120| 167k| }
121| 334k| }
122| 167k|}
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| 167k| for (int i = 1; i <= n; ++i) {
^167k
------------------
| Branch (135:19): [True: 100.00%, False: 0.00%]
------------------
136| 167k| node[i] = {.k = -1};
137| 167k| u32 a = node[i].a = rd.uw();
138| 167k| u32 b = node[i].b = rd.uw();
139| 167k| node[i].aff = {a, b, b};
140| 167k| }
141| 167k| for (int i = 1; i != n; ++i) {
^167k
------------------
| Branch (141:19): [True: 100.00%, False: 0.00%]
------------------
142| 167k| int u = rd.uh();
143| 167k| int v = rd.uh();
144| 167k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
145| 167k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
146| 167k| }
147| 1| build(0, 0);
148| 25.8k| while (q--) {
------------------
| Branch (148:10): [True: 100.00%, False: 0.00%]
------------------
149| 25.8k| let t = rd.u1();
150| 25.8k| if (t == 0) {
------------------
| Branch (150:9): [True: 33.51%, False: 66.49%]
------------------
151| 8.65k| int u = rd.uh() + 1;
152| 8.65k| int v = rd.uh() + 1;
153| 8.65k| access(u);
154| 8.65k| reverse(u);
155| 8.65k| access(v);
156| 8.65k| node[node[v].ch[0]].p = 0;
157| 8.65k| node[node[v].ch[0]].k = -1;
158| 8.65k| node[v].ch[0] = 0;
159| 8.65k| int w = rd.uh() + 1;
160| 8.65k| int x = rd.uh() + 1;
161| 8.65k| access(w);
162| 8.65k| reverse(w);
163| 8.65k| node[w].p = x;
164| 8.65k| }
165| 25.8k| if (t == 1) {
------------------
| Branch (165:9): [True: 33.51%, False: 66.49%]
------------------
166| 8.65k| int u = rd.uh() + 1;
167| 8.65k| node[u].a = rd.uw();
168| 8.65k| node[u].b = rd.uw();
169| 8.65k| splay(u);
170| 8.65k| }
171| 25.8k| if (t == 2) {
------------------
| Branch (171:9): [True: 32.98%, False: 67.02%]
------------------
172| 8.52k| int u = rd.uh() + 1;
173| 8.52k| int v = rd.uh() + 1;
174| 8.52k| u32 x = rd.uw();
175| 8.52k| access(v);
176| 8.52k| reverse(v);
177| 8.52k| access(u);
178| 8.52k| x = Affine{node[u].a, node[u].b, node[u].b} + x;
179| 8.52k| x = node[node[u].ch[0]].aff + x;
180| 8.52k| wt.ud(x);
181| 8.52k| }
182| 25.8k| }
183| 1| return 0;
184| 1|}