/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| 3.16M|def reverse(int x) -> void { node[x].rev ^= 1; }
19| |
20| 7.76M|def pushdown(int x) -> void {
21| 7.76M| if (node[x].rev) {
------------------
| Branch (21:7): [True: 19.10%, False: 80.90%]
------------------
22| 1.48M| std::swap(node[x].ch[0], node[x].ch[1]);
23| 1.48M| node[node[x].ch[0]].k = 0;
24| 1.48M| node[node[x].ch[1]].k = 1;
25| 1.48M| reverse(node[x].ch[0]);
26| 1.48M| reverse(node[x].ch[1]);
27| 1.48M| node[x].rev = 0;
28| 1.48M| }
29| 7.76M|}
30| |
31| 7.47M|def maintain(int x) -> void {
32| 7.47M| node[x].sum = node[node[x].ch[0]].sum + node[x].val +
33| 7.47M| node[node[x].ch[1]].sum + node[x].sum_;
34| 7.47M|}
35| |
36| 5.18M|def rotateup(int x) -> void {
37| 5.18M| int p = node[x].p;
38| 5.18M| int g = node[p].p;
39| 5.18M| int k = node[x].k;
40| 5.18M| int t = node[p].k;
41| 5.18M| node[node[x].ch[k ^ 1]].p = p;
42| 5.18M| node[node[x].ch[k ^ 1]].k = k;
43| 5.18M| node[p].ch[k] = node[x].ch[k ^ 1];
44| 5.18M| node[p].p = x;
45| 5.18M| node[p].k = k ^ 1;
46| 5.18M| node[x].ch[k ^ 1] = p;
47| 5.18M| node[x].p = g;
48| 5.18M| node[x].k = t;
49| 5.18M| if (t != -1) {
------------------
| Branch (49:7): [True: 33.08%, False: 66.92%]
------------------
50| 1.71M| node[g].ch[t] = x;
51| 1.71M| }
52| 5.18M| maintain(p);
53| 5.18M|}
54| |
55| 6.51M|def is_root(int x) -> bool { return node[x].k == -1; }
56| |
57| 2.28M|def splay(int x) -> void {
58| 2.28M| pushdown(x);
59| 4.39M| while (!is_root(x)) {
------------------
| Branch (59:10): [True: 48.02%, False: 51.98%]
------------------
60| 2.11M| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (60:28): [True: 40.61%, False: 59.39%]
------------------
61| 857k| pushdown(p);
62| 857k| pushdown(x);
63| 857k| rotateup(x);
64| 1.25M| } else {
65| 1.25M| int g = node[p].p;
66| 1.25M| pushdown(g);
67| 1.25M| pushdown(p);
68| 1.25M| pushdown(x);
69| 1.25M| if (node[x].k == node[p].k) {
------------------
| Branch (69:11): [True: 51.20%, False: 48.80%]
------------------
70| 642k| rotateup(p);
71| 642k| rotateup(x);
72| 642k| } else {
73| 612k| rotateup(x);
74| 612k| rotateup(x);
75| 612k| }
76| 1.25M| }
77| 2.11M| }
78| 2.28M|}
79| |
80| 466k|def access(int x) -> void {
81| 466k| splay(x);
82| 466k| node[node[x].ch[1]].k = -1;
83| 466k| node[x].sum_ += node[node[x].ch[1]].sum;
84| 466k| node[x].ch[1] = 0;
85| 466k| maintain(x);
86| 2.28M| while (int p = node[x].p) {
------------------
| Branch (86:14): [True: 79.61%, False: 20.39%]
------------------
87| 1.82M| splay(p);
88| 1.82M| node[node[p].ch[1]].k = -1;
89| 1.82M| node[p].sum_ += node[node[p].ch[1]].sum;
90| 1.82M| node[p].sum_ -= node[x].sum;
91| 1.82M| node[p].ch[1] = x;
92| 1.82M| node[x].k = 1;
93| 1.82M| rotateup(x);
94| 1.82M| maintain(x);
95| 1.82M| }
96| 466k|}
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.24%, False: 66.76%]
------------------
137| 66.4k| int u = rd.uh() + 1;
138| 66.4k| int v = rd.uh() + 1;
139| 66.4k| access(u);
140| 66.4k| reverse(u);
141| 66.4k| access(v);
142| 66.4k| node[node[v].ch[0]].p = 0;
143| 66.4k| node[node[v].ch[0]].k = -1;
144| 66.4k| node[v].ch[0] = 0;
145| 66.4k| int w = rd.uh() + 1;
146| 66.4k| int x = rd.uh() + 1;
147| 66.4k| access(w);
148| 66.4k| reverse(w);
149| 66.4k| access(x);
150| 66.4k| node[w].p = x;
151| 66.4k| node[x].sum_ += node[w].sum;
152| 66.4k| }
153| 200k| if (t == 1) {
------------------
| Branch (153:9): [True: 33.33%, False: 66.67%]
------------------
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.43%, False: 66.57%]
------------------
160| 66.8k| int u = rd.uh() + 1;
161| 66.8k| int p = rd.uh() + 1;
162| 66.8k| access(u);
163| 66.8k| reverse(u);
164| 66.8k| access(p);
165| 66.8k| wt.ud(node[u].sum);
166| 66.8k| }
167| 200k| }
168| 1| return 0;
169| 1|}