/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| 362k|def reverse(int x) -> void { node[x].rev ^= 1; }
19| |
20| 888k|def pushdown(int x) -> void {
21| 888k| if (node[x].rev) {
------------------
| Branch (21:7): [True: 18.93%, False: 81.07%]
------------------
22| 168k| std::swap(node[x].ch[0], node[x].ch[1]);
23| 168k| node[node[x].ch[0]].k = 0;
24| 168k| node[node[x].ch[1]].k = 1;
25| 168k| reverse(node[x].ch[0]);
26| 168k| reverse(node[x].ch[1]);
27| 168k| node[x].rev = 0;
28| 168k| }
29| 888k|}
30| |
31| 904k|def maintain(int x) -> void {
32| 904k| node[x].sum = node[node[x].ch[0]].sum + node[x].val +
33| 904k| node[node[x].ch[1]].sum + node[x].sum_;
34| 904k|}
35| |
36| 599k|def rotateup(int x) -> void {
37| 599k| int p = node[x].p;
38| 599k| int g = node[p].p;
39| 599k| int k = node[x].k;
40| 599k| int t = node[p].k;
41| 599k| node[node[x].ch[k ^ 1]].p = p;
42| 599k| node[node[x].ch[k ^ 1]].k = k;
43| 599k| node[p].ch[k] = node[x].ch[k ^ 1];
44| 599k| node[p].p = x;
45| 599k| node[p].k = k ^ 1;
46| 599k| node[x].ch[k ^ 1] = p;
47| 599k| node[x].p = g;
48| 599k| node[x].k = t;
49| 599k| if (t != -1) {
------------------
| Branch (49:7): [True: 27.14%, False: 72.86%]
------------------
50| 162k| node[g].ch[t] = x;
51| 162k| }
52| 599k| maintain(p);
53| 599k|}
54| |
55| 760k|def is_root(int x) -> bool { return node[x].k == -1; }
56| |
57| 304k|def splay(int x) -> void {
58| 304k| pushdown(x);
59| 532k| while (!is_root(x)) {
------------------
| Branch (59:10): [True: 42.82%, False: 57.18%]
------------------
60| 227k| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (60:28): [True: 44.01%, False: 55.99%]
------------------
61| 100k| pushdown(p);
62| 100k| pushdown(x);
63| 100k| rotateup(x);
64| 127k| } else {
65| 127k| int g = node[p].p;
66| 127k| pushdown(g);
67| 127k| pushdown(p);
68| 127k| pushdown(x);
69| 127k| if (node[x].k == node[p].k) {
------------------
| Branch (69:11): [True: 52.12%, False: 47.88%]
------------------
70| 66.5k| rotateup(p);
71| 66.5k| rotateup(x);
72| 66.5k| } else {
73| 61.1k| rotateup(x);
74| 61.1k| rotateup(x);
75| 61.1k| }
76| 127k| }
77| 227k| }
78| 304k|}
79| |
80| 60.4k|def access(int x) -> void {
81| 60.4k| splay(x);
82| 60.4k| node[node[x].ch[1]].k = -1;
83| 60.4k| node[x].sum_ += node[node[x].ch[1]].sum;
84| 60.4k| node[x].ch[1] = 0;
85| 60.4k| maintain(x);
86| 304k| while (int p = node[x].p) {
------------------
| Branch (86:14): [True: 80.16%, False: 19.84%]
------------------
87| 244k| splay(p);
88| 244k| node[node[p].ch[1]].k = -1;
89| 244k| node[p].sum_ += node[node[p].ch[1]].sum;
90| 244k| node[p].sum_ -= node[x].sum;
91| 244k| node[p].ch[1] = x;
92| 244k| node[x].k = 1;
93| 244k| rotateup(x);
94| 244k| maintain(x);
95| 244k| }
96| 60.4k|}
97| |
98| |int head[N];
99| |struct {
100| | int to;
101| | int next;
102| |} edge[N * 2];
103| |
104| 167k|def build(int u, int p) -> void {
105| 501k| for (int e = head[u]; e; e = edge[e].next) {
^334k
------------------
| Branch (105:25): [True: 66.67%, False: 33.33%]
------------------
106| 334k| int v = edge[e].to;
107| 334k| if (v != p) {
------------------
| Branch (107:9): [True: 50.00%, False: 50.00%]
------------------
108| 167k| build(v, u);
109| 167k| node[v + 1].p = u + 1;
110| 167k| node[u + 1].sum_ += node[v + 1].sum;
111| 167k| }
112| 334k| }
113| 167k| node[u + 1].sum = node[u + 1].val + node[u + 1].sum_;
114| 167k|}
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| 167k| for (int i = 1; i <= n; ++i) node[i] = {.val = rd.uw(), .k = -1};
^167k^167k
------------------
| Branch (126:19): [True: 100.00%, False: 0.00%]
------------------
127| 167k| for (int i = 1; i != n; ++i) {
^167k
------------------
| Branch (127:19): [True: 100.00%, False: 0.00%]
------------------
128| 167k| int u = rd.uh();
129| 167k| int v = rd.uh();
130| 167k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
131| 167k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
132| 167k| }
133| 1| build(0, 0);
134| 25.8k| while (q--) {
------------------
| Branch (134:10): [True: 100.00%, False: 0.00%]
------------------
135| 25.8k| let t = rd.u1();
136| 25.8k| if (t == 0) {
------------------
| Branch (136:9): [True: 33.51%, False: 66.49%]
------------------
137| 8.65k| int u = rd.uh() + 1;
138| 8.65k| int v = rd.uh() + 1;
139| 8.65k| access(u);
140| 8.65k| reverse(u);
141| 8.65k| access(v);
142| 8.65k| node[node[v].ch[0]].p = 0;
143| 8.65k| node[node[v].ch[0]].k = -1;
144| 8.65k| node[v].ch[0] = 0;
145| 8.65k| int w = rd.uh() + 1;
146| 8.65k| int x = rd.uh() + 1;
147| 8.65k| access(w);
148| 8.65k| reverse(w);
149| 8.65k| access(x);
150| 8.65k| node[w].p = x;
151| 8.65k| node[x].sum_ += node[w].sum;
152| 8.65k| }
153| 25.8k| if (t == 1) {
------------------
| Branch (153:9): [True: 33.25%, False: 66.75%]
------------------
154| 8.59k| int u = rd.uh() + 1;
155| 8.59k| u32 x = rd.uw();
156| 8.59k| access(u);
157| 8.59k| node[u].val += x;
158| 8.59k| }
159| 25.8k| if (t == 2) {
------------------
| Branch (159:9): [True: 33.24%, False: 66.76%]
------------------
160| 8.59k| int u = rd.uh() + 1;
161| 8.59k| int p = rd.uh() + 1;
162| 8.59k| access(u);
163| 8.59k| reverse(u);
164| 8.59k| access(p);
165| 8.59k| wt.ud(node[u].sum);
166| 8.59k| }
167| 25.8k| }
168| 1| return 0;
169| 1|}