/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| 13.3k|def reverse(int x) -> void { node[x].rev ^= 1; }
19| |
20| 21.4k|def pushdown(int x) -> void {
21| 21.4k| if (node[x].rev) {
------------------
| Branch (21:7): [True: 28.84%, False: 71.16%]
------------------
22| 6.17k| std::swap(node[x].ch[0], node[x].ch[1]);
23| 6.17k| node[node[x].ch[0]].k = 0;
24| 6.17k| node[node[x].ch[1]].k = 1;
25| 6.17k| reverse(node[x].ch[0]);
26| 6.17k| reverse(node[x].ch[1]);
27| 6.17k| node[x].rev = 0;
28| 6.17k| }
29| 21.4k|}
30| |
31| 21.2k|def maintain(int x) -> void {
32| 21.2k| node[x].sum = node[node[x].ch[0]].sum + node[x].val +
33| 21.2k| node[node[x].ch[1]].sum + node[x].sum_;
34| 21.2k|}
35| |
36| 13.7k|def rotateup(int x) -> void {
37| 13.7k| int p = node[x].p;
38| 13.7k| int g = node[p].p;
39| 13.7k| int k = node[x].k;
40| 13.7k| int t = node[p].k;
41| 13.7k| node[node[x].ch[k ^ 1]].p = p;
42| 13.7k| node[node[x].ch[k ^ 1]].k = k;
43| 13.7k| node[p].ch[k] = node[x].ch[k ^ 1];
44| 13.7k| node[p].p = x;
45| 13.7k| node[p].k = k ^ 1;
46| 13.7k| node[x].ch[k ^ 1] = p;
47| 13.7k| node[x].p = g;
48| 13.7k| node[x].k = t;
49| 13.7k| if (t != -1) {
------------------
| Branch (49:7): [True: 31.34%, False: 68.66%]
------------------
50| 4.31k| node[g].ch[t] = x;
51| 4.31k| }
52| 13.7k| maintain(p);
53| 13.7k|}
54| |
55| 18.3k|def is_root(int x) -> bool { return node[x].k == -1; }
56| |
57| 7.46k|def splay(int x) -> void {
58| 7.46k| pushdown(x);
59| 12.8k| while (!is_root(x)) {
------------------
| Branch (59:10): [True: 42.07%, False: 57.93%]
------------------
60| 5.41k| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (60:28): [True: 42.35%, False: 57.65%]
------------------
61| 2.29k| pushdown(p);
62| 2.29k| pushdown(x);
63| 2.29k| rotateup(x);
64| 3.12k| } else {
65| 3.12k| int g = node[p].p;
66| 3.12k| pushdown(g);
67| 3.12k| pushdown(p);
68| 3.12k| pushdown(x);
69| 3.12k| if (node[x].k == node[p].k) {
------------------
| Branch (69:11): [True: 45.55%, False: 54.45%]
------------------
70| 1.42k| rotateup(p);
71| 1.42k| rotateup(x);
72| 1.70k| } else {
73| 1.70k| rotateup(x);
74| 1.70k| rotateup(x);
75| 1.70k| }
76| 3.12k| }
77| 5.41k| }
78| 7.46k|}
79| |
80| 2.22k|def access(int x) -> void {
81| 2.22k| splay(x);
82| 2.22k| node[node[x].ch[1]].k = -1;
83| 2.22k| node[x].sum_ += node[node[x].ch[1]].sum;
84| 2.22k| node[x].ch[1] = 0;
85| 2.22k| maintain(x);
86| 7.46k| while (int p = node[x].p) {
------------------
| Branch (86:14): [True: 70.13%, False: 29.87%]
------------------
87| 5.23k| splay(p);
88| 5.23k| node[node[p].ch[1]].k = -1;
89| 5.23k| node[p].sum_ += node[node[p].ch[1]].sum;
90| 5.23k| node[p].sum_ -= node[x].sum;
91| 5.23k| node[p].ch[1] = x;
92| 5.23k| node[x].k = 1;
93| 5.23k| rotateup(x);
94| 5.23k| maintain(x);
95| 5.23k| }
96| 2.22k|}
97| |
98| |int head[N];
99| |struct {
100| | int to;
101| | int next;
102| |} edge[N * 2];
103| |
104| 919|def build(int u, int p) -> void {
105| 2.75k| for (int e = head[u]; e; e = edge[e].next) {
^1.83k
------------------
| Branch (105:25): [True: 66.64%, False: 33.36%]
------------------
106| 1.83k| int v = edge[e].to;
107| 1.83k| if (v != p) {
------------------
| Branch (107:9): [True: 50.00%, False: 50.00%]
------------------
108| 918| build(v, u);
109| 918| node[v + 1].p = u + 1;
110| 918| node[u + 1].sum_ += node[v + 1].sum;
111| 918| }
112| 1.83k| }
113| 919| node[u + 1].sum = node[u + 1].val + node[u + 1].sum_;
114| 919|}
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| 920| for (int i = 1; i <= n; ++i) node[i] = {.val = rd.uw(), .k = -1};
^919 ^919
------------------
| Branch (126:19): [True: 99.89%, False: 0.11%]
------------------
127| 919| for (int i = 1; i != n; ++i) {
^918
------------------
| Branch (127:19): [True: 99.89%, False: 0.11%]
------------------
128| 918| int u = rd.uh();
129| 918| int v = rd.uh();
130| 918| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
131| 918| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
132| 918| }
133| 1| build(0, 0);
134| 945| while (q--) {
------------------
| Branch (134:10): [True: 99.89%, False: 0.11%]
------------------
135| 944| let t = rd.u1();
136| 944| if (t == 0) {
------------------
| Branch (136:9): [True: 34.11%, False: 65.89%]
------------------
137| 322| int u = rd.uh() + 1;
138| 322| int v = rd.uh() + 1;
139| 322| access(u);
140| 322| reverse(u);
141| 322| access(v);
142| 322| node[node[v].ch[0]].p = 0;
143| 322| node[node[v].ch[0]].k = -1;
144| 322| node[v].ch[0] = 0;
145| 322| int w = rd.uh() + 1;
146| 322| int x = rd.uh() + 1;
147| 322| access(w);
148| 322| reverse(w);
149| 322| access(x);
150| 322| node[w].p = x;
151| 322| node[x].sum_ += node[w].sum;
152| 322| }
153| 944| if (t == 1) {
------------------
| Branch (153:9): [True: 32.10%, False: 67.90%]
------------------
154| 303| int u = rd.uh() + 1;
155| 303| u32 x = rd.uw();
156| 303| access(u);
157| 303| node[u].val += x;
158| 303| }
159| 944| if (t == 2) {
------------------
| Branch (159:9): [True: 33.79%, False: 66.21%]
------------------
160| 319| int u = rd.uh() + 1;
161| 319| int p = rd.uh() + 1;
162| 319| access(u);
163| 319| reverse(u);
164| 319| access(p);
165| 319| wt.ud(node[u].sum);
166| 319| }
167| 944| }
168| 1| return 0;
169| 1|}