/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| 12.6k|def reverse(int x) -> void { node[x].rev ^= 1; }
19| |
20| 20.3k|def pushdown(int x) -> void {
21| 20.3k| if (node[x].rev) {
------------------
| Branch (21:7): [True: 28.87%, False: 71.13%]
------------------
22| 5.88k| std::swap(node[x].ch[0], node[x].ch[1]);
23| 5.88k| node[node[x].ch[0]].k = 0;
24| 5.88k| node[node[x].ch[1]].k = 1;
25| 5.88k| reverse(node[x].ch[0]);
26| 5.88k| reverse(node[x].ch[1]);
27| 5.88k| node[x].rev = 0;
28| 5.88k| }
29| 20.3k|}
30| |
31| 20.3k|def maintain(int x) -> void {
32| 20.3k| node[x].sum = node[node[x].ch[0]].sum + node[x].val +
33| 20.3k| node[node[x].ch[1]].sum + node[x].sum_;
34| 20.3k|}
35| |
36| 13.1k|def rotateup(int x) -> void {
37| 13.1k| int p = node[x].p;
38| 13.1k| int g = node[p].p;
39| 13.1k| int k = node[x].k;
40| 13.1k| int t = node[p].k;
41| 13.1k| node[node[x].ch[k ^ 1]].p = p;
42| 13.1k| node[node[x].ch[k ^ 1]].k = k;
43| 13.1k| node[p].ch[k] = node[x].ch[k ^ 1];
44| 13.1k| node[p].p = x;
45| 13.1k| node[p].k = k ^ 1;
46| 13.1k| node[x].ch[k ^ 1] = p;
47| 13.1k| node[x].p = g;
48| 13.1k| node[x].k = t;
49| 13.1k| if (t != -1) {
------------------
| Branch (49:7): [True: 30.32%, False: 69.68%]
------------------
50| 3.97k| node[g].ch[t] = x;
51| 3.97k| }
52| 13.1k| maintain(p);
53| 13.1k|}
54| |
55| 17.4k|def is_root(int x) -> bool { return node[x].k == -1; }
56| |
57| 7.27k|def splay(int x) -> void {
58| 7.27k| pushdown(x);
59| 12.3k| while (!is_root(x)) {
------------------
| Branch (59:10): [True: 41.28%, False: 58.72%]
------------------
60| 5.11k| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (60:28): [True: 43.68%, False: 56.32%]
------------------
61| 2.23k| pushdown(p);
62| 2.23k| pushdown(x);
63| 2.23k| rotateup(x);
64| 2.87k| } else {
65| 2.87k| int g = node[p].p;
66| 2.87k| pushdown(g);
67| 2.87k| pushdown(p);
68| 2.87k| pushdown(x);
69| 2.87k| if (node[x].k == node[p].k) {
------------------
| Branch (69:11): [True: 45.88%, False: 54.12%]
------------------
70| 1.32k| rotateup(p);
71| 1.32k| rotateup(x);
72| 1.55k| } else {
73| 1.55k| rotateup(x);
74| 1.55k| rotateup(x);
75| 1.55k| }
76| 2.87k| }
77| 5.11k| }
78| 7.27k|}
79| |
80| 2.15k|def access(int x) -> void {
81| 2.15k| splay(x);
82| 2.15k| node[node[x].ch[1]].k = -1;
83| 2.15k| node[x].sum_ += node[node[x].ch[1]].sum;
84| 2.15k| node[x].ch[1] = 0;
85| 2.15k| maintain(x);
86| 7.27k| while (int p = node[x].p) {
------------------
| Branch (86:14): [True: 70.42%, False: 29.58%]
------------------
87| 5.12k| splay(p);
88| 5.12k| node[node[p].ch[1]].k = -1;
89| 5.12k| node[p].sum_ += node[node[p].ch[1]].sum;
90| 5.12k| node[p].sum_ -= node[x].sum;
91| 5.12k| node[p].ch[1] = x;
92| 5.12k| node[x].k = 1;
93| 5.12k| rotateup(x);
94| 5.12k| maintain(x);
95| 5.12k| }
96| 2.15k|}
97| |
98| |int head[N];
99| |struct {
100| | int to;
101| | int next;
102| |} edge[N * 2];
103| |
104| 987|def build(int u, int p) -> void {
105| 2.95k| for (int e = head[u]; e; e = edge[e].next) {
^1.97k
------------------
| Branch (105:25): [True: 66.64%, False: 33.36%]
------------------
106| 1.97k| int v = edge[e].to;
107| 1.97k| if (v != p) {
------------------
| Branch (107:9): [True: 50.00%, False: 50.00%]
------------------
108| 986| build(v, u);
109| 986| node[v + 1].p = u + 1;
110| 986| node[u + 1].sum_ += node[v + 1].sum;
111| 986| }
112| 1.97k| }
113| 987| node[u + 1].sum = node[u + 1].val + node[u + 1].sum_;
114| 987|}
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| 988| for (int i = 1; i <= n; ++i) node[i] = {.val = rd.uw(), .k = -1};
^987 ^987
------------------
| Branch (126:19): [True: 99.90%, False: 0.10%]
------------------
127| 987| for (int i = 1; i != n; ++i) {
^986
------------------
| Branch (127:19): [True: 99.90%, False: 0.10%]
------------------
128| 986| int u = rd.uh();
129| 986| int v = rd.uh();
130| 986| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
131| 986| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
132| 986| }
133| 1| build(0, 0);
134| 911| while (q--) {
------------------
| Branch (134:10): [True: 99.89%, False: 0.11%]
------------------
135| 910| let t = rd.u1();
136| 910| if (t == 0) {
------------------
| Branch (136:9): [True: 34.18%, False: 65.82%]
------------------
137| 311| int u = rd.uh() + 1;
138| 311| int v = rd.uh() + 1;
139| 311| access(u);
140| 311| reverse(u);
141| 311| access(v);
142| 311| node[node[v].ch[0]].p = 0;
143| 311| node[node[v].ch[0]].k = -1;
144| 311| node[v].ch[0] = 0;
145| 311| int w = rd.uh() + 1;
146| 311| int x = rd.uh() + 1;
147| 311| access(w);
148| 311| reverse(w);
149| 311| access(x);
150| 311| node[w].p = x;
151| 311| node[x].sum_ += node[w].sum;
152| 311| }
153| 910| if (t == 1) {
------------------
| Branch (153:9): [True: 31.98%, False: 68.02%]
------------------
154| 291| int u = rd.uh() + 1;
155| 291| u32 x = rd.uw();
156| 291| access(u);
157| 291| node[u].val += x;
158| 291| }
159| 910| if (t == 2) {
------------------
| Branch (159:9): [True: 33.85%, False: 66.15%]
------------------
160| 308| int u = rd.uh() + 1;
161| 308| int p = rd.uh() + 1;
162| 308| access(u);
163| 308| reverse(u);
164| 308| access(p);
165| 308| wt.ud(node[u].sum);
166| 308| }
167| 910| }
168| 1| return 0;
169| 1|}