/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| 10.3M|def reverse(int x) -> void { node[x].rev ^= 1; }
19| |
20| 12.2M|def pushdown(int x) -> void {
21| 12.2M| if (node[x].rev) {
------------------
| Branch (21:7): [True: 41.17%, False: 58.83%]
------------------
22| 5.05M| std::swap(node[x].ch[0], node[x].ch[1]);
23| 5.05M| node[node[x].ch[0]].k = 0;
24| 5.05M| node[node[x].ch[1]].k = 1;
25| 5.05M| reverse(node[x].ch[0]);
26| 5.05M| reverse(node[x].ch[1]);
27| 5.05M| node[x].rev = 0;
28| 5.05M| }
29| 12.2M|}
30| |
31| 9.16M|def maintain(int x) -> void {
32| 9.16M| node[x].sum = node[node[x].ch[0]].sum + node[x].val +
33| 9.16M| node[node[x].ch[1]].sum + node[x].sum_;
34| 9.16M|}
35| |
36| 8.00M|def rotateup(int x) -> void {
37| 8.00M| int p = node[x].p;
38| 8.00M| int g = node[p].p;
39| 8.00M| int k = node[x].k;
40| 8.00M| int t = node[p].k;
41| 8.00M| node[node[x].ch[k ^ 1]].p = p;
42| 8.00M| node[node[x].ch[k ^ 1]].k = k;
43| 8.00M| node[p].ch[k] = node[x].ch[k ^ 1];
44| 8.00M| node[p].p = x;
45| 8.00M| node[p].k = k ^ 1;
46| 8.00M| node[x].ch[k ^ 1] = p;
47| 8.00M| node[x].p = g;
48| 8.00M| node[x].k = t;
49| 8.00M| if (t != -1) {
------------------
| Branch (49:7): [True: 83.98%, False: 16.02%]
------------------
50| 6.71M| node[g].ch[t] = x;
51| 6.71M| }
52| 8.00M| maintain(p);
53| 8.00M|}
54| |
55| 8.77M|def is_root(int x) -> bool { return node[x].k == -1; }
56| |
57| 1.16M|def splay(int x) -> void {
58| 1.16M| pushdown(x);
59| 4.96M| while (!is_root(x)) {
------------------
| Branch (59:10): [True: 76.58%, False: 23.42%]
------------------
60| 3.80M| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (60:28): [True: 8.00%, False: 92.00%]
------------------
61| 304k| pushdown(p);
62| 304k| pushdown(x);
63| 304k| rotateup(x);
64| 3.49M| } else {
65| 3.49M| int g = node[p].p;
66| 3.49M| pushdown(g);
67| 3.49M| pushdown(p);
68| 3.49M| pushdown(x);
69| 3.49M| if (node[x].k == node[p].k) {
------------------
| Branch (69:11): [True: 62.03%, False: 37.97%]
------------------
70| 2.17M| rotateup(p);
71| 2.17M| rotateup(x);
72| 2.17M| } else {
73| 1.32M| rotateup(x);
74| 1.32M| rotateup(x);
75| 1.32M| }
76| 3.49M| }
77| 3.80M| }
78| 1.16M|}
79| |
80| 467k|def access(int x) -> void {
81| 467k| splay(x);
82| 467k| node[node[x].ch[1]].k = -1;
83| 467k| node[x].sum_ += node[node[x].ch[1]].sum;
84| 467k| node[x].ch[1] = 0;
85| 467k| maintain(x);
86| 1.16M| while (int p = node[x].p) {
------------------
| Branch (86:14): [True: 59.82%, False: 40.18%]
------------------
87| 696k| splay(p);
88| 696k| node[node[p].ch[1]].k = -1;
89| 696k| node[p].sum_ += node[node[p].ch[1]].sum;
90| 696k| node[p].sum_ -= node[x].sum;
91| 696k| node[p].ch[1] = x;
92| 696k| node[x].k = 1;
93| 696k| rotateup(x);
94| 696k| maintain(x);
95| 696k| }
96| 467k|}
97| |
98| |int head[N];
99| |struct {
100| | int to;
101| | int next;
102| |} edge[N * 2];
103| |
104| 200k|def build(int u, int p) -> void {
105| 599k| for (int e = head[u]; e; e = edge[e].next) {
^399k
------------------
| Branch (105:25): [True: 66.67%, False: 33.33%]
------------------
106| 399k| int v = edge[e].to;
107| 399k| if (v != p) {
------------------
| Branch (107:9): [True: 50.00%, False: 50.00%]
------------------
108| 199k| build(v, u);
109| 199k| node[v + 1].p = u + 1;
110| 199k| node[u + 1].sum_ += node[v + 1].sum;
111| 199k| }
112| 399k| }
113| 200k| node[u + 1].sum = node[u + 1].val + node[u + 1].sum_;
114| 200k|}
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| 200k| for (int i = 1; i <= n; ++i) node[i] = {.val = rd.uw(), .k = -1};
^200k^200k
------------------
| Branch (126:19): [True: 100.00%, False: 0.00%]
------------------
127| 200k| for (int i = 1; i != n; ++i) {
^199k
------------------
| Branch (127:19): [True: 100.00%, False: 0.00%]
------------------
128| 199k| int u = rd.uh();
129| 199k| int v = rd.uh();
130| 199k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
131| 199k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
132| 199k| }
133| 1| build(0, 0);
134| 200k| while (q--) {
------------------
| Branch (134:10): [True: 100.00%, False: 0.00%]
------------------
135| 200k| let t = rd.u1();
136| 200k| if (t == 0) {
------------------
| Branch (136:9): [True: 33.56%, False: 66.44%]
------------------
137| 67.1k| int u = rd.uh() + 1;
138| 67.1k| int v = rd.uh() + 1;
139| 67.1k| access(u);
140| 67.1k| reverse(u);
141| 67.1k| access(v);
142| 67.1k| node[node[v].ch[0]].p = 0;
143| 67.1k| node[node[v].ch[0]].k = -1;
144| 67.1k| node[v].ch[0] = 0;
145| 67.1k| int w = rd.uh() + 1;
146| 67.1k| int x = rd.uh() + 1;
147| 67.1k| access(w);
148| 67.1k| reverse(w);
149| 67.1k| access(x);
150| 67.1k| node[w].p = x;
151| 67.1k| node[x].sum_ += node[w].sum;
152| 67.1k| }
153| 200k| if (t == 1) {
------------------
| Branch (153:9): [True: 33.31%, False: 66.69%]
------------------
154| 66.6k| int u = rd.uh() + 1;
155| 66.6k| u32 x = rd.uw();
156| 66.6k| access(u);
157| 66.6k| node[u].val += x;
158| 66.6k| }
159| 200k| if (t == 2) {
------------------
| Branch (159:9): [True: 33.13%, False: 66.87%]
------------------
160| 66.2k| int u = rd.uh() + 1;
161| 66.2k| int p = rd.uh() + 1;
162| 66.2k| access(u);
163| 66.2k| reverse(u);
164| 66.2k| access(p);
165| 66.2k| wt.ud(node[u].sum);
166| 66.2k| }
167| 200k| }
168| 1| return 0;
169| 1|}