/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| 5.61k| auto operator+(const Affine &t) const -> Affine {
12| 5.61k| u32 x = (u64(a) * t.a) % P;
13| 5.61k| u32 y = (u64(a) * t.b + b) % P;
14| 5.61k| u32 z = (u64(t.a) * c + t.c) % P;
15| 5.61k| return {x, y, z};
16| 5.61k| }
17| 250| 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.78k|def reverse(int x) -> void {
30| 3.78k| node[x].rev ^= 1;
31| 3.78k| std::swap(node[x].aff.b, node[x].aff.c);
32| 3.78k|}
33| |
34| 4.72k|def pushdown(int x) -> void {
35| 4.72k| if (node[x].rev) {
------------------
| Branch (35:7): [True: 35.80%, False: 64.20%]
------------------
36| 1.69k| std::swap(node[x].ch[0], node[x].ch[1]);
37| 1.69k| node[node[x].ch[0]].k = 0;
38| 1.69k| node[node[x].ch[1]].k = 1;
39| 1.69k| reverse(node[x].ch[0]);
40| 1.69k| reverse(node[x].ch[1]);
41| 1.69k| node[x].rev = 0;
42| 1.69k| }
43| 4.72k|}
44| |
45| 2.80k|def maintain(int x) -> void {
46| 2.80k| node[x].aff = node[node[x].ch[0]].aff +
47| 2.80k| Affine{node[x].a, node[x].b, node[x].b} +
48| 2.80k| node[node[x].ch[1]].aff;
49| 2.80k|}
50| |
51| 2.80k|def rotateup(int x) -> void {
52| 2.80k| int p = node[x].p;
53| 2.80k| int g = node[p].p;
54| 2.80k| int k = node[x].k;
55| 2.80k| int t = node[p].k;
56| 2.80k| node[node[x].ch[k ^ 1]].p = p;
57| 2.80k| node[node[x].ch[k ^ 1]].k = k;
58| 2.80k| node[p].ch[k] = node[x].ch[k ^ 1];
59| 2.80k| node[p].p = x;
60| 2.80k| node[p].k = k ^ 1;
61| 2.80k| node[x].ch[k ^ 1] = p;
62| 2.80k| node[x].p = g;
63| 2.80k| node[x].k = t;
64| 2.80k| if (t != -1) {
------------------
| Branch (64:7): [True: 24.18%, False: 75.82%]
------------------
65| 679| node[g].ch[t] = x;
66| 679| }
67| 2.80k| maintain(p);
68| 2.80k|}
69| |
70| 4.20k|def is_root(int x) -> bool { return node[x].k == -1; }
71| |
72| 1.97k|def splay(int x) -> void {
73| 1.97k| pushdown(x);
74| 3.08k| while (!is_root(x)) {
------------------
| Branch (74:10): [True: 36.16%, False: 63.84%]
------------------
75| 1.11k| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (75:28): [True: 53.89%, False: 46.11%]
------------------
76| 602| pushdown(p);
77| 602| pushdown(x);
78| 602| rotateup(x);
79| 602| } else {
80| 515| int g = node[p].p;
81| 515| pushdown(g);
82| 515| pushdown(p);
83| 515| pushdown(x);
84| 515| if (node[x].k == node[p].k) {
------------------
| Branch (84:11): [True: 36.70%, False: 63.30%]
------------------
85| 189| rotateup(p);
86| 189| rotateup(x);
87| 326| } else {
88| 326| rotateup(x);
89| 326| rotateup(x);
90| 326| }
91| 515| }
92| 1.11k| }
93| 1.97k|}
94| |
95| 664|def access(int x) -> void {
96| 664| splay(x);
97| 664| node[node[x].ch[1]].k = -1;
98| 664| node[x].ch[1] = 0;
99| 1.84k| while (int p = node[x].p) {
------------------
| Branch (99:14): [True: 63.91%, False: 36.09%]
------------------
100| 1.17k| splay(p);
101| 1.17k| node[node[p].ch[1]].k = -1;
102| 1.17k| node[p].ch[1] = x;
103| 1.17k| node[x].k = 1;
104| 1.17k| rotateup(x);
105| 1.17k| }
106| 664|}
107| |
108| |int head[N];
109| |struct {
110| | int to;
111| | int next;
112| |} edge[N * 2];
113| |
114| 88|def build(int u, int p) -> void {
115| 262| for (int e = head[u]; e; e = edge[e].next) {
^174
------------------
| Branch (115:25): [True: 66.41%, False: 33.59%]
------------------
116| 174| int v = edge[e].to;
117| 174| if (v != p) {
------------------
| Branch (117:9): [True: 50.00%, False: 50.00%]
------------------
118| 87| build(v, u);
119| 87| node[v + 1].p = u + 1;
120| 87| }
121| 174| }
122| 88|}
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| 89| for (int i = 1; i <= n; ++i) {
^88
------------------
| Branch (135:19): [True: 98.88%, False: 1.12%]
------------------
136| 88| node[i] = {.k = -1};
137| 88| u32 a = node[i].a = rd.uw();
138| 88| u32 b = node[i].b = rd.uw();
139| 88| node[i].aff = {a, b, b};
140| 88| }
141| 88| for (int i = 1; i != n; ++i) {
^87
------------------
| Branch (141:19): [True: 98.86%, False: 1.14%]
------------------
142| 87| int u = rd.uh();
143| 87| int v = rd.uh();
144| 87| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
145| 87| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
146| 87| }
147| 1| build(0, 0);
148| 396| while (q--) {
------------------
| Branch (148:10): [True: 99.75%, False: 0.25%]
------------------
149| 395| let t = rd.u1();
150| 395| if (t == 0) {
------------------
| Branch (150:9): [True: 34.94%, False: 65.06%]
------------------
151| 138| int u = rd.uh() + 1;
152| 138| int v = rd.uh() + 1;
153| 138| access(u);
154| 138| reverse(u);
155| 138| access(v);
156| 138| node[node[v].ch[0]].p = 0;
157| 138| node[node[v].ch[0]].k = -1;
158| 138| node[v].ch[0] = 0;
159| 138| int w = rd.uh() + 1;
160| 138| int x = rd.uh() + 1;
161| 138| access(w);
162| 138| reverse(w);
163| 138| node[w].p = x;
164| 138| }
165| 395| if (t == 1) {
------------------
| Branch (165:9): [True: 33.42%, False: 66.58%]
------------------
166| 132| int u = rd.uh() + 1;
167| 132| node[u].a = rd.uw();
168| 132| node[u].b = rd.uw();
169| 132| splay(u);
170| 132| }
171| 395| if (t == 2) {
------------------
| Branch (171:9): [True: 31.65%, False: 68.35%]
------------------
172| 125| int u = rd.uh() + 1;
173| 125| int v = rd.uh() + 1;
174| 125| u32 x = rd.uw();
175| 125| access(v);
176| 125| reverse(v);
177| 125| access(u);
178| 125| x = Affine{node[u].a, node[u].b, node[u].b} + x;
179| 125| x = node[node[u].ch[0]].aff + x;
180| 125| wt.ud(x);
181| 125| }
182| 395| }
183| 1| return 0;
184| 1|}