/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.8k|def reverse(int x) -> void { node[x].rev ^= 1; }
19| |
20| 22.3k|def pushdown(int x) -> void {
21| 22.3k| if (node[x].rev) {
------------------
| Branch (21:7): [True: 28.79%, False: 71.21%]
------------------
22| 6.42k| std::swap(node[x].ch[0], node[x].ch[1]);
23| 6.42k| node[node[x].ch[0]].k = 0;
24| 6.42k| node[node[x].ch[1]].k = 1;
25| 6.42k| reverse(node[x].ch[0]);
26| 6.42k| reverse(node[x].ch[1]);
27| 6.42k| node[x].rev = 0;
28| 6.42k| }
29| 22.3k|}
30| |
31| 21.9k|def maintain(int x) -> void {
32| 21.9k| node[x].sum = node[node[x].ch[0]].sum + node[x].val +
33| 21.9k| node[node[x].ch[1]].sum + node[x].sum_;
34| 21.9k|}
35| |
36| 14.3k|def rotateup(int x) -> void {
37| 14.3k| int p = node[x].p;
38| 14.3k| int g = node[p].p;
39| 14.3k| int k = node[x].k;
40| 14.3k| int t = node[p].k;
41| 14.3k| node[node[x].ch[k ^ 1]].p = p;
42| 14.3k| node[node[x].ch[k ^ 1]].k = k;
43| 14.3k| node[p].ch[k] = node[x].ch[k ^ 1];
44| 14.3k| node[p].p = x;
45| 14.3k| node[p].k = k ^ 1;
46| 14.3k| node[x].ch[k ^ 1] = p;
47| 14.3k| node[x].p = g;
48| 14.3k| node[x].k = t;
49| 14.3k| if (t != -1) {
------------------
| Branch (49:7): [True: 32.85%, False: 67.15%]
------------------
50| 4.71k| node[g].ch[t] = x;
51| 4.71k| }
52| 14.3k| maintain(p);
53| 14.3k|}
54| |
55| 18.9k|def is_root(int x) -> bool { return node[x].k == -1; }
56| |
57| 7.61k|def splay(int x) -> void {
58| 7.61k| pushdown(x);
59| 13.2k| while (!is_root(x)) {
------------------
| Branch (59:10): [True: 42.75%, False: 57.25%]
------------------
60| 5.68k| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (60:28): [True: 41.53%, False: 58.47%]
------------------
61| 2.36k| pushdown(p);
62| 2.36k| pushdown(x);
63| 2.36k| rotateup(x);
64| 3.32k| } else {
65| 3.32k| int g = node[p].p;
66| 3.32k| pushdown(g);
67| 3.32k| pushdown(p);
68| 3.32k| pushdown(x);
69| 3.32k| if (node[x].k == node[p].k) {
------------------
| Branch (69:11): [True: 45.92%, False: 54.08%]
------------------
70| 1.52k| rotateup(p);
71| 1.52k| rotateup(x);
72| 1.79k| } else {
73| 1.79k| rotateup(x);
74| 1.79k| rotateup(x);
75| 1.79k| }
76| 3.32k| }
77| 5.68k| }
78| 7.61k|}
79| |
80| 2.26k|def access(int x) -> void {
81| 2.26k| splay(x);
82| 2.26k| node[node[x].ch[1]].k = -1;
83| 2.26k| node[x].sum_ += node[node[x].ch[1]].sum;
84| 2.26k| node[x].ch[1] = 0;
85| 2.26k| maintain(x);
86| 7.61k| while (int p = node[x].p) {
------------------
| Branch (86:14): [True: 70.23%, False: 29.77%]
------------------
87| 5.34k| splay(p);
88| 5.34k| node[node[p].ch[1]].k = -1;
89| 5.34k| node[p].sum_ += node[node[p].ch[1]].sum;
90| 5.34k| node[p].sum_ -= node[x].sum;
91| 5.34k| node[p].ch[1] = x;
92| 5.34k| node[x].k = 1;
93| 5.34k| rotateup(x);
94| 5.34k| maintain(x);
95| 5.34k| }
96| 2.26k|}
97| |
98| |int head[N];
99| |struct {
100| | int to;
101| | int next;
102| |} edge[N * 2];
103| |
104| 964|def build(int u, int p) -> void {
105| 2.89k| for (int e = head[u]; e; e = edge[e].next) {
^1.92k
------------------
| Branch (105:25): [True: 66.64%, False: 33.36%]
------------------
106| 1.92k| int v = edge[e].to;
107| 1.92k| if (v != p) {
------------------
| Branch (107:9): [True: 50.00%, False: 50.00%]
------------------
108| 963| build(v, u);
109| 963| node[v + 1].p = u + 1;
110| 963| node[u + 1].sum_ += node[v + 1].sum;
111| 963| }
112| 1.92k| }
113| 964| node[u + 1].sum = node[u + 1].val + node[u + 1].sum_;
114| 964|}
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| 965| for (int i = 1; i <= n; ++i) node[i] = {.val = rd.uw(), .k = -1};
^964 ^964
------------------
| Branch (126:19): [True: 99.90%, False: 0.10%]
------------------
127| 964| for (int i = 1; i != n; ++i) {
^963
------------------
| Branch (127:19): [True: 99.90%, False: 0.10%]
------------------
128| 963| int u = rd.uh();
129| 963| int v = rd.uh();
130| 963| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
131| 963| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
132| 963| }
133| 1| build(0, 0);
134| 982| while (q--) {
------------------
| Branch (134:10): [True: 99.90%, False: 0.10%]
------------------
135| 981| let t = rd.u1();
136| 981| if (t == 0) {
------------------
| Branch (136:9): [True: 31.60%, False: 68.40%]
------------------
137| 310| int u = rd.uh() + 1;
138| 310| int v = rd.uh() + 1;
139| 310| access(u);
140| 310| reverse(u);
141| 310| access(v);
142| 310| node[node[v].ch[0]].p = 0;
143| 310| node[node[v].ch[0]].k = -1;
144| 310| node[v].ch[0] = 0;
145| 310| int w = rd.uh() + 1;
146| 310| int x = rd.uh() + 1;
147| 310| access(w);
148| 310| reverse(w);
149| 310| access(x);
150| 310| node[w].p = x;
151| 310| node[x].sum_ += node[w].sum;
152| 310| }
153| 981| if (t == 1) {
------------------
| Branch (153:9): [True: 32.21%, False: 67.79%]
------------------
154| 316| int u = rd.uh() + 1;
155| 316| u32 x = rd.uw();
156| 316| access(u);
157| 316| node[u].val += x;
158| 316| }
159| 981| if (t == 2) {
------------------
| Branch (159:9): [True: 36.19%, False: 63.81%]
------------------
160| 355| int u = rd.uh() + 1;
161| 355| int p = rd.uh() + 1;
162| 355| access(u);
163| 355| reverse(u);
164| 355| access(p);
165| 355| wt.ud(node[u].sum);
166| 355| }
167| 981| }
168| 1| return 0;
169| 1|}