/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| 2.03M|def reverse(int x) -> void { node[x].rev ^= 1; }
19| |
20| 4.40M|def pushdown(int x) -> void {
21| 4.40M| if (node[x].rev) {
------------------
| Branch (21:7): [True: 21.71%, False: 78.29%]
------------------
22| 955k| std::swap(node[x].ch[0], node[x].ch[1]);
23| 955k| node[node[x].ch[0]].k = 0;
24| 955k| node[node[x].ch[1]].k = 1;
25| 955k| reverse(node[x].ch[0]);
26| 955k| reverse(node[x].ch[1]);
27| 955k| node[x].rev = 0;
28| 955k| }
29| 4.40M|}
30| |
31| 4.16M|def maintain(int x) -> void {
32| 4.16M| node[x].sum = node[node[x].ch[0]].sum + node[x].val +
33| 4.16M| node[node[x].ch[1]].sum + node[x].sum_;
34| 4.16M|}
35| |
36| 2.91M|def rotateup(int x) -> void {
37| 2.91M| int p = node[x].p;
38| 2.91M| int g = node[p].p;
39| 2.91M| int k = node[x].k;
40| 2.91M| int t = node[p].k;
41| 2.91M| node[node[x].ch[k ^ 1]].p = p;
42| 2.91M| node[node[x].ch[k ^ 1]].k = k;
43| 2.91M| node[p].ch[k] = node[x].ch[k ^ 1];
44| 2.91M| node[p].p = x;
45| 2.91M| node[p].k = k ^ 1;
46| 2.91M| node[x].ch[k ^ 1] = p;
47| 2.91M| node[x].p = g;
48| 2.91M| node[x].k = t;
49| 2.91M| if (t != -1) {
------------------
| Branch (49:7): [True: 36.63%, False: 63.37%]
------------------
50| 1.06M| node[g].ch[t] = x;
51| 1.06M| }
52| 2.91M| maintain(p);
53| 2.91M|}
54| |
55| 3.65M|def is_root(int x) -> bool { return node[x].k == -1; }
56| |
57| 1.24M|def splay(int x) -> void {
58| 1.24M| pushdown(x);
59| 2.45M| while (!is_root(x)) {
------------------
| Branch (59:10): [True: 49.16%, False: 50.84%]
------------------
60| 1.20M| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (60:28): [True: 38.02%, False: 61.98%]
------------------
61| 458k| pushdown(p);
62| 458k| pushdown(x);
63| 458k| rotateup(x);
64| 746k| } else {
65| 746k| int g = node[p].p;
66| 746k| pushdown(g);
67| 746k| pushdown(p);
68| 746k| pushdown(x);
69| 746k| if (node[x].k == node[p].k) {
------------------
| Branch (69:11): [True: 50.20%, False: 49.80%]
------------------
70| 374k| rotateup(p);
71| 374k| rotateup(x);
72| 374k| } else {
73| 371k| rotateup(x);
74| 371k| rotateup(x);
75| 371k| }
76| 746k| }
77| 1.20M| }
78| 1.24M|}
79| |
80| 280k|def access(int x) -> void {
81| 280k| splay(x);
82| 280k| node[node[x].ch[1]].k = -1;
83| 280k| node[x].sum_ += node[node[x].ch[1]].sum;
84| 280k| node[x].ch[1] = 0;
85| 280k| maintain(x);
86| 1.24M| while (int p = node[x].p) {
------------------
| Branch (86:14): [True: 77.46%, False: 22.54%]
------------------
87| 965k| splay(p);
88| 965k| node[node[p].ch[1]].k = -1;
89| 965k| node[p].sum_ += node[node[p].ch[1]].sum;
90| 965k| node[p].sum_ -= node[x].sum;
91| 965k| node[p].ch[1] = x;
92| 965k| node[x].k = 1;
93| 965k| rotateup(x);
94| 965k| maintain(x);
95| 965k| }
96| 280k|}
97| |
98| |int head[N];
99| |struct {
100| | int to;
101| | int next;
102| |} edge[N * 2];
103| |
104| 53.3k|def build(int u, int p) -> void {
105| 160k| for (int e = head[u]; e; e = edge[e].next) {
^106k
------------------
| Branch (105:25): [True: 66.67%, False: 33.33%]
------------------
106| 106k| int v = edge[e].to;
107| 106k| if (v != p) {
------------------
| Branch (107:9): [True: 50.00%, False: 50.00%]
------------------
108| 53.3k| build(v, u);
109| 53.3k| node[v + 1].p = u + 1;
110| 53.3k| node[u + 1].sum_ += node[v + 1].sum;
111| 53.3k| }
112| 106k| }
113| 53.3k| node[u + 1].sum = node[u + 1].val + node[u + 1].sum_;
114| 53.3k|}
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| 53.3k| for (int i = 1; i <= n; ++i) node[i] = {.val = rd.uw(), .k = -1};
^53.3k^53.3k
------------------
| Branch (126:19): [True: 100.00%, False: 0.00%]
------------------
127| 53.3k| for (int i = 1; i != n; ++i) {
^53.3k
------------------
| Branch (127:19): [True: 100.00%, False: 0.00%]
------------------
128| 53.3k| int u = rd.uh();
129| 53.3k| int v = rd.uh();
130| 53.3k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
131| 53.3k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
132| 53.3k| }
133| 1| build(0, 0);
134| 120k| while (q--) {
------------------
| Branch (134:10): [True: 100.00%, False: 0.00%]
------------------
135| 120k| let t = rd.u1();
136| 120k| if (t == 0) {
------------------
| Branch (136:9): [True: 33.48%, False: 66.52%]
------------------
137| 40.2k| int u = rd.uh() + 1;
138| 40.2k| int v = rd.uh() + 1;
139| 40.2k| access(u);
140| 40.2k| reverse(u);
141| 40.2k| access(v);
142| 40.2k| node[node[v].ch[0]].p = 0;
143| 40.2k| node[node[v].ch[0]].k = -1;
144| 40.2k| node[v].ch[0] = 0;
145| 40.2k| int w = rd.uh() + 1;
146| 40.2k| int x = rd.uh() + 1;
147| 40.2k| access(w);
148| 40.2k| reverse(w);
149| 40.2k| access(x);
150| 40.2k| node[w].p = x;
151| 40.2k| node[x].sum_ += node[w].sum;
152| 40.2k| }
153| 120k| if (t == 1) {
------------------
| Branch (153:9): [True: 33.23%, False: 66.77%]
------------------
154| 39.9k| int u = rd.uh() + 1;
155| 39.9k| u32 x = rd.uw();
156| 39.9k| access(u);
157| 39.9k| node[u].val += x;
158| 39.9k| }
159| 120k| if (t == 2) {
------------------
| Branch (159:9): [True: 33.29%, False: 66.71%]
------------------
160| 40.0k| int u = rd.uh() + 1;
161| 40.0k| int p = rd.uh() + 1;
162| 40.0k| access(u);
163| 40.0k| reverse(u);
164| 40.0k| access(p);
165| 40.0k| wt.ud(node[u].sum);
166| 40.0k| }
167| 120k| }
168| 1| return 0;
169| 1|}