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