/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.3k|def reverse(int x) -> void { node[x].rev ^= 1; }
19| |
20| 20.2k|def pushdown(int x) -> void {
21| 20.2k| if (node[x].rev) {
------------------
| Branch (21:7): [True: 28.28%, False: 71.72%]
------------------
22| 5.73k| std::swap(node[x].ch[0], node[x].ch[1]);
23| 5.73k| node[node[x].ch[0]].k = 0;
24| 5.73k| node[node[x].ch[1]].k = 1;
25| 5.73k| reverse(node[x].ch[0]);
26| 5.73k| reverse(node[x].ch[1]);
27| 5.73k| node[x].rev = 0;
28| 5.73k| }
29| 20.2k|}
30| |
31| 20.1k|def maintain(int x) -> void {
32| 20.1k| node[x].sum = node[node[x].ch[0]].sum + node[x].val +
33| 20.1k| node[node[x].ch[1]].sum + node[x].sum_;
34| 20.1k|}
35| |
36| 13.0k|def rotateup(int x) -> void {
37| 13.0k| int p = node[x].p;
38| 13.0k| int g = node[p].p;
39| 13.0k| int k = node[x].k;
40| 13.0k| int t = node[p].k;
41| 13.0k| node[node[x].ch[k ^ 1]].p = p;
42| 13.0k| node[node[x].ch[k ^ 1]].k = k;
43| 13.0k| node[p].ch[k] = node[x].ch[k ^ 1];
44| 13.0k| node[p].p = x;
45| 13.0k| node[p].k = k ^ 1;
46| 13.0k| node[x].ch[k ^ 1] = p;
47| 13.0k| node[x].p = g;
48| 13.0k| node[x].k = t;
49| 13.0k| if (t != -1) {
------------------
| Branch (49:7): [True: 30.84%, False: 69.16%]
------------------
50| 4.01k| node[g].ch[t] = x;
51| 4.01k| }
52| 13.0k| maintain(p);
53| 13.0k|}
54| |
55| 17.3k|def is_root(int x) -> bool { return node[x].k == -1; }
56| |
57| 7.12k|def splay(int x) -> void {
58| 7.12k| pushdown(x);
59| 12.2k| while (!is_root(x)) {
------------------
| Branch (59:10): [True: 41.73%, False: 58.27%]
------------------
60| 5.10k| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (60:28): [True: 42.70%, False: 57.30%]
------------------
61| 2.18k| pushdown(p);
62| 2.18k| pushdown(x);
63| 2.18k| rotateup(x);
64| 2.92k| } else {
65| 2.92k| int g = node[p].p;
66| 2.92k| pushdown(g);
67| 2.92k| pushdown(p);
68| 2.92k| pushdown(x);
69| 2.92k| if (node[x].k == node[p].k) {
------------------
| Branch (69:11): [True: 47.28%, False: 52.72%]
------------------
70| 1.38k| rotateup(p);
71| 1.38k| rotateup(x);
72| 1.54k| } else {
73| 1.54k| rotateup(x);
74| 1.54k| rotateup(x);
75| 1.54k| }
76| 2.92k| }
77| 5.10k| }
78| 7.12k|}
79| |
80| 2.12k|def access(int x) -> void {
81| 2.12k| splay(x);
82| 2.12k| node[node[x].ch[1]].k = -1;
83| 2.12k| node[x].sum_ += node[node[x].ch[1]].sum;
84| 2.12k| node[x].ch[1] = 0;
85| 2.12k| maintain(x);
86| 7.12k| while (int p = node[x].p) {
------------------
| Branch (86:14): [True: 70.15%, False: 29.85%]
------------------
87| 5.00k| splay(p);
88| 5.00k| node[node[p].ch[1]].k = -1;
89| 5.00k| node[p].sum_ += node[node[p].ch[1]].sum;
90| 5.00k| node[p].sum_ -= node[x].sum;
91| 5.00k| node[p].ch[1] = x;
92| 5.00k| node[x].k = 1;
93| 5.00k| rotateup(x);
94| 5.00k| maintain(x);
95| 5.00k| }
96| 2.12k|}
97| |
98| |int head[N];
99| |struct {
100| | int to;
101| | int next;
102| |} edge[N * 2];
103| |
104| 969|def build(int u, int p) -> void {
105| 2.90k| for (int e = head[u]; e; e = edge[e].next) {
^1.93k
------------------
| Branch (105:25): [True: 66.64%, False: 33.36%]
------------------
106| 1.93k| int v = edge[e].to;
107| 1.93k| if (v != p) {
------------------
| Branch (107:9): [True: 50.00%, False: 50.00%]
------------------
108| 968| build(v, u);
109| 968| node[v + 1].p = u + 1;
110| 968| node[u + 1].sum_ += node[v + 1].sum;
111| 968| }
112| 1.93k| }
113| 969| node[u + 1].sum = node[u + 1].val + node[u + 1].sum_;
114| 969|}
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| 970| for (int i = 1; i <= n; ++i) node[i] = {.val = rd.uw(), .k = -1};
^969 ^969
------------------
| Branch (126:19): [True: 99.90%, False: 0.10%]
------------------
127| 969| for (int i = 1; i != n; ++i) {
^968
------------------
| Branch (127:19): [True: 99.90%, False: 0.10%]
------------------
128| 968| int u = rd.uh();
129| 968| int v = rd.uh();
130| 968| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
131| 968| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
132| 968| }
133| 1| build(0, 0);
134| 921| while (q--) {
------------------
| Branch (134:10): [True: 99.89%, False: 0.11%]
------------------
135| 920| let t = rd.u1();
136| 920| if (t == 0) {
------------------
| Branch (136:9): [True: 32.93%, False: 67.07%]
------------------
137| 303| int u = rd.uh() + 1;
138| 303| int v = rd.uh() + 1;
139| 303| access(u);
140| 303| reverse(u);
141| 303| access(v);
142| 303| node[node[v].ch[0]].p = 0;
143| 303| node[node[v].ch[0]].k = -1;
144| 303| node[v].ch[0] = 0;
145| 303| int w = rd.uh() + 1;
146| 303| int x = rd.uh() + 1;
147| 303| access(w);
148| 303| reverse(w);
149| 303| access(x);
150| 303| node[w].p = x;
151| 303| node[x].sum_ += node[w].sum;
152| 303| }
153| 920| if (t == 1) {
------------------
| Branch (153:9): [True: 34.57%, False: 65.43%]
------------------
154| 318| int u = rd.uh() + 1;
155| 318| u32 x = rd.uw();
156| 318| access(u);
157| 318| node[u].val += x;
158| 318| }
159| 920| if (t == 2) {
------------------
| Branch (159:9): [True: 32.50%, False: 67.50%]
------------------
160| 299| int u = rd.uh() + 1;
161| 299| int p = rd.uh() + 1;
162| 299| access(u);
163| 299| reverse(u);
164| 299| access(p);
165| 299| wt.ud(node[u].sum);
166| 299| }
167| 920| }
168| 1| return 0;
169| 1|}