/tmp/solutions/build/dynamic_tree_vertex_add_path_sum-main.cpp:
1| |#include <common.h>
2| |prelude;
3| |
4| |namespace {
5| |
6| |constexpr int N = 200001;
7| |
8| |struct Node {
9| | u64 val;
10| | u64 sum;
11| | int p;
12| | int ch[2];
13| | i8 rev;
14| | i8 k;
15| |} node[N];
16| |
17| 2.90k|def reverse(int x) -> void { node[x].rev ^= 1; }
18| |
19| 4.44k|def pushdown(int x) -> void {
20| 4.44k| if (node[x].rev) {
------------------
| Branch (20:7): [True: 29.38%, False: 70.62%]
------------------
21| 1.30k| std::swap(node[x].ch[0], node[x].ch[1]);
22| 1.30k| node[node[x].ch[0]].k = 0;
23| 1.30k| node[node[x].ch[1]].k = 1;
24| 1.30k| reverse(node[x].ch[0]);
25| 1.30k| reverse(node[x].ch[1]);
26| 1.30k| node[x].rev = 0;
27| 1.30k| }
28| 4.44k|}
29| |
30| 2.80k|def maintain(int x) -> void {
31| 2.80k| node[x].sum = node[node[x].ch[0]].sum + node[x].val + node[node[x].ch[1]].sum;
32| 2.80k|}
33| |
34| 2.80k|def rotateup(int x) -> void {
35| 2.80k| int p = node[x].p;
36| 2.80k| int g = node[p].p;
37| 2.80k| int k = node[x].k;
38| 2.80k| int t = node[p].k;
39| 2.80k| node[node[x].ch[k ^ 1]].p = p;
40| 2.80k| node[node[x].ch[k ^ 1]].k = k;
41| 2.80k| node[p].ch[k] = node[x].ch[k ^ 1];
42| 2.80k| node[p].p = x;
43| 2.80k| node[p].k = k ^ 1;
44| 2.80k| node[x].ch[k ^ 1] = p;
45| 2.80k| node[x].p = g;
46| 2.80k| node[x].k = t;
47| 2.80k| if (t != -1) {
------------------
| Branch (47:7): [True: 24.73%, False: 75.27%]
------------------
48| 694| node[g].ch[t] = x;
49| 694| }
50| 2.80k| maintain(p);
51| 2.80k|}
52| |
53| 3.91k|def is_root(int x) -> bool { return node[x].k == -1; }
54| |
55| 1.79k|def splay(int x) -> void {
56| 1.79k| pushdown(x);
57| 2.85k| while (!is_root(x)) {
------------------
| Branch (57:10): [True: 37.04%, False: 62.96%]
------------------
58| 1.05k| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (58:28): [True: 49.10%, False: 50.90%]
------------------
59| 519| pushdown(p);
60| 519| pushdown(x);
61| 519| rotateup(x);
62| 538| } else {
63| 538| int g = node[p].p;
64| 538| pushdown(g);
65| 538| pushdown(p);
66| 538| pushdown(x);
67| 538| if (node[x].k == node[p].k) {
------------------
| Branch (67:11): [True: 37.36%, False: 62.64%]
------------------
68| 201| rotateup(p);
69| 201| rotateup(x);
70| 337| } else {
71| 337| rotateup(x);
72| 337| rotateup(x);
73| 337| }
74| 538| }
75| 1.05k| }
76| 1.79k|}
77| |
78| 475|def access(int x) -> void {
79| 475| splay(x);
80| 475| node[node[x].ch[1]].k = -1;
81| 475| node[x].ch[1] = 0;
82| 1.68k| while (int p = node[x].p) {
------------------
| Branch (82:14): [True: 71.83%, False: 28.17%]
------------------
83| 1.21k| splay(p);
84| 1.21k| node[node[p].ch[1]].k = -1;
85| 1.21k| node[p].ch[1] = x;
86| 1.21k| node[x].k = 1;
87| 1.21k| rotateup(x);
88| 1.21k| }
89| 475|}
90| |
91| |int head[N];
92| |struct {
93| | int to;
94| | int next;
95| |} edge[N * 2];
96| |
97| 692|def build(int u, int p) -> void {
98| 2.07k| for (int e = head[u]; e; e = edge[e].next) {
^1.38k
------------------
| Branch (98:25): [True: 66.63%, False: 33.37%]
------------------
99| 1.38k| int v = edge[e].to;
100| 1.38k| if (v != p) {
------------------
| Branch (100:9): [True: 50.00%, False: 50.00%]
------------------
101| 691| build(v, u);
102| 691| node[v + 1].p = u + 1;
103| 691| }
104| 1.38k| }
105| 692|}
106| |
107| |} // namespace
108| |
109| 1|int main() {
110| 1| rd rd;
111| 1| wt wt;
112| 1| int n = rd.uh();
113| 1| int q = rd.uh();
114| 1|#ifdef LOCAL
115| 1| std::memset(head, 0, 4 * n);
116| 1|#endif
117| 693| for (int i = 1; i <= n; ++i)
^692
------------------
| Branch (117:19): [True: 99.86%, False: 0.14%]
------------------
118| 692| node[i] = {.k = -1}, node[i].sum = node[i].val = rd.uw();
119| 692| for (int i = 1; i != n; ++i) {
^691
------------------
| Branch (119:19): [True: 99.86%, False: 0.14%]
------------------
120| 691| int u = rd.uh();
121| 691| int v = rd.uh();
122| 691| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
123| 691| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
124| 691| }
125| 1| build(0, 0);
126| 299| while (q--) {
------------------
| Branch (126:10): [True: 99.67%, False: 0.33%]
------------------
127| 298| let t = rd.u1();
128| 298| if (t == 0) {
------------------
| Branch (128:9): [True: 33.89%, False: 66.11%]
------------------
129| 101| int u = rd.uh() + 1;
130| 101| int v = rd.uh() + 1;
131| 101| access(u);
132| 101| reverse(u);
133| 101| access(v);
134| 101| node[node[v].ch[0]].p = 0;
135| 101| node[node[v].ch[0]].k = -1;
136| 101| node[v].ch[0] = 0;
137| 101| int w = rd.uh() + 1;
138| 101| int x = rd.uh() + 1;
139| 101| access(w);
140| 101| reverse(w);
141| 101| node[w].p = x;
142| 101| }
143| 298| if (t == 1) {
------------------
| Branch (143:9): [True: 37.25%, False: 62.75%]
------------------
144| 111| int u = rd.uh() + 1;
145| 111| u32 x = rd.uw();
146| 111| splay(u);
147| 111| node[u].val += x;
148| 111| node[u].sum += x;
149| 111| }
150| 298| if (t == 2) {
------------------
| Branch (150:9): [True: 28.86%, False: 71.14%]
------------------
151| 86| int u = rd.uh() + 1;
152| 86| int v = rd.uh() + 1;
153| 86| access(u);
154| 86| reverse(u);
155| 86| access(v);
156| 86| wt.ud(node[node[v].ch[0]].sum + node[v].val);
157| 86| }
158| 298| }
159| 1| return 0;
160| 1|}