/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| 42|def reverse(int x) -> void { node[x].rev ^= 1; }
18| |
19| 47|def pushdown(int x) -> void {
20| 47| if (node[x].rev) {
------------------
| Branch (20:7): [True: 36.17%, False: 63.83%]
------------------
21| 17| std::swap(node[x].ch[0], node[x].ch[1]);
22| 17| node[node[x].ch[0]].k = 0;
23| 17| node[node[x].ch[1]].k = 1;
24| 17| reverse(node[x].ch[0]);
25| 17| reverse(node[x].ch[1]);
26| 17| node[x].rev = 0;
27| 17| }
28| 47|}
29| |
30| 23|def maintain(int x) -> void {
31| 23| node[x].sum = node[node[x].ch[0]].sum + node[x].val + node[node[x].ch[1]].sum;
32| 23|}
33| |
34| 23|def rotateup(int x) -> void {
35| 23| int p = node[x].p;
36| 23| int g = node[p].p;
37| 23| int k = node[x].k;
38| 23| int t = node[p].k;
39| 23| node[node[x].ch[k ^ 1]].p = p;
40| 23| node[node[x].ch[k ^ 1]].k = k;
41| 23| node[p].ch[k] = node[x].ch[k ^ 1];
42| 23| node[p].p = x;
43| 23| node[p].k = k ^ 1;
44| 23| node[x].ch[k ^ 1] = p;
45| 23| node[x].p = g;
46| 23| node[x].k = t;
47| 23| if (t != -1) {
------------------
| Branch (47:7): [True: 17.39%, False: 82.61%]
------------------
48| 4| node[g].ch[t] = x;
49| 4| }
50| 23| maintain(p);
51| 23|}
52| |
53| 43|def is_root(int x) -> bool { return node[x].k == -1; }
54| |
55| 25|def splay(int x) -> void {
56| 25| pushdown(x);
57| 34| while (!is_root(x)) {
------------------
| Branch (57:10): [True: 26.47%, False: 73.53%]
------------------
58| 9| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (58:28): [True: 55.56%, False: 44.44%]
------------------
59| 5| pushdown(p);
60| 5| pushdown(x);
61| 5| rotateup(x);
62| 5| } else {
63| 4| int g = node[p].p;
64| 4| pushdown(g);
65| 4| pushdown(p);
66| 4| pushdown(x);
67| 4| if (node[x].k == node[p].k) {
------------------
| Branch (67:11): [True: 25.00%, False: 75.00%]
------------------
68| 1| rotateup(p);
69| 1| rotateup(x);
70| 3| } else {
71| 3| rotateup(x);
72| 3| rotateup(x);
73| 3| }
74| 4| }
75| 9| }
76| 25|}
77| |
78| 14|def access(int x) -> void {
79| 14| splay(x);
80| 14| node[node[x].ch[1]].k = -1;
81| 14| node[x].ch[1] = 0;
82| 24| while (int p = node[x].p) {
------------------
| Branch (82:14): [True: 41.67%, False: 58.33%]
------------------
83| 10| splay(p);
84| 10| node[node[p].ch[1]].k = -1;
85| 10| node[p].ch[1] = x;
86| 10| node[x].k = 1;
87| 10| rotateup(x);
88| 10| }
89| 14|}
90| |
91| |int head[N];
92| |struct {
93| | int to;
94| | int next;
95| |} edge[N * 2];
96| |
97| 5|def build(int u, int p) -> void {
98| 13| for (int e = head[u]; e; e = edge[e].next) {
^8
------------------
| Branch (98:25): [True: 61.54%, False: 38.46%]
------------------
99| 8| int v = edge[e].to;
100| 8| if (v != p) {
------------------
| Branch (100:9): [True: 50.00%, False: 50.00%]
------------------
101| 4| build(v, u);
102| 4| node[v + 1].p = u + 1;
103| 4| }
104| 8| }
105| 5|}
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| 6| for (int i = 1; i <= n; ++i)
^5
------------------
| Branch (117:19): [True: 83.33%, False: 16.67%]
------------------
118| 5| node[i] = {.k = -1}, node[i].sum = node[i].val = rd.uw();
119| 5| for (int i = 1; i != n; ++i) {
^4
------------------
| Branch (119:19): [True: 80.00%, False: 20.00%]
------------------
120| 4| int u = rd.uh();
121| 4| int v = rd.uh();
122| 4| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
123| 4| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
124| 4| }
125| 1| build(0, 0);
126| 8| while (q--) {
------------------
| Branch (126:10): [True: 87.50%, False: 12.50%]
------------------
127| 7| let t = rd.u1();
128| 7| if (t == 0) {
------------------
| Branch (128:9): [True: 28.57%, False: 71.43%]
------------------
129| 2| int u = rd.uh() + 1;
130| 2| int v = rd.uh() + 1;
131| 2| access(u);
132| 2| reverse(u);
133| 2| access(v);
134| 2| node[node[v].ch[0]].p = 0;
135| 2| node[node[v].ch[0]].k = -1;
136| 2| node[v].ch[0] = 0;
137| 2| int w = rd.uh() + 1;
138| 2| int x = rd.uh() + 1;
139| 2| access(w);
140| 2| reverse(w);
141| 2| node[w].p = x;
142| 2| }
143| 7| if (t == 1) {
------------------
| Branch (143:9): [True: 14.29%, False: 85.71%]
------------------
144| 1| int u = rd.uh() + 1;
145| 1| u32 x = rd.uw();
146| 1| splay(u);
147| 1| node[u].val += x;
148| 1| node[u].sum += x;
149| 1| }
150| 7| if (t == 2) {
------------------
| Branch (150:9): [True: 57.14%, False: 42.86%]
------------------
151| 4| int u = rd.uh() + 1;
152| 4| int v = rd.uh() + 1;
153| 4| access(u);
154| 4| reverse(u);
155| 4| access(v);
156| 4| wt.ud(node[node[v].ch[0]].sum + node[v].val);
157| 4| }
158| 7| }
159| 1| return 0;
160| 1|}