/tmp/solutions/build/dynamic_tree_subtree_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| | i64 sum;
10| | i64 sum_;
11| | i64 add;
12| | int size;
13| | int size_;
14| | int p;
15| | int ch[2];
16| | i8 rev;
17| | i8 k;
18| |} node[N];
19| |
20| 2.44M|def reverse(int x) -> void { node[x].rev ^= 1; }
21| |
22| 4.92M|def pushdown(int x) -> void {
23| 4.92M| if (node[x].rev) {
------------------
| Branch (23:7): [True: 23.13%, False: 76.87%]
------------------
24| 1.14M| std::swap(node[x].ch[0], node[x].ch[1]);
25| 1.14M| node[node[x].ch[0]].k = 0;
26| 1.14M| node[node[x].ch[1]].k = 1;
27| 1.14M| reverse(node[x].ch[0]);
28| 1.14M| reverse(node[x].ch[1]);
29| 1.14M| node[x].rev = 0;
30| 1.14M| }
31| 4.92M|}
32| |
33| 4.60M|def maintain(int x) -> void {
34| 4.60M| node[x].size =
35| 4.60M| node[node[x].ch[0]].size + 1 + node[node[x].ch[1]].size + node[x].size_;
36| 4.60M| node[x].sum = node[node[x].ch[0]].sum + node[node[x].ch[1]].sum +
37| 4.60M| node[x].sum_ + node[x].size * node[x].add;
38| 4.60M|}
39| |
40| 3.25M|def rotateup(int x) -> void {
41| 3.25M| int p = node[x].p;
42| 3.25M| int g = node[p].p;
43| 3.25M| int k = node[x].k;
44| 3.25M| int t = node[p].k;
45| 3.25M| i64 b = node[x].add;
46| 3.25M| i64 a = node[p].add;
47| 3.25M| node[x].add = a + b;
48| 3.25M| node[p].add = -b;
49| 3.25M| node[node[x].ch[k ^ 1]].add += b;
50| 3.25M| node[node[x].ch[k ^ 1]].sum += b * node[node[x].ch[k ^ 1]].size;
51| 3.25M| node[node[x].ch[k ^ 1]].p = p;
52| 3.25M| node[node[x].ch[k ^ 1]].k = k;
53| 3.25M| node[p].ch[k] = node[x].ch[k ^ 1];
54| 3.25M| node[p].p = x;
55| 3.25M| node[p].k = k ^ 1;
56| 3.25M| node[x].ch[k ^ 1] = p;
57| 3.25M| node[x].p = g;
58| 3.25M| node[x].k = t;
59| 3.25M| if (t != -1) {
------------------
| Branch (59:7): [True: 39.14%, False: 60.86%]
------------------
60| 1.27M| node[g].ch[t] = x;
61| 1.27M| }
62| 3.25M| maintain(p);
63| 3.25M|}
64| |
65| 4.06M|def is_root(int x) -> bool { return node[x].k == -1; }
66| |
67| 1.35M|def splay(int x) -> void {
68| 1.35M| pushdown(x);
69| 2.70M| while (!is_root(x)) {
------------------
| Branch (69:10): [True: 50.08%, False: 49.92%]
------------------
70| 1.35M| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (70:28): [True: 36.41%, False: 63.59%]
------------------
71| 494k| pushdown(p);
72| 494k| pushdown(x);
73| 494k| rotateup(x);
74| 862k| } else {
75| 862k| int g = node[p].p;
76| 862k| pushdown(g);
77| 862k| pushdown(p);
78| 862k| pushdown(x);
79| 862k| if (node[x].k == node[p].k) {
------------------
| Branch (79:11): [True: 55.60%, False: 44.40%]
------------------
80| 479k| rotateup(p);
81| 479k| rotateup(x);
82| 479k| } else {
83| 383k| rotateup(x);
84| 383k| rotateup(x);
85| 383k| }
86| 862k| }
87| 1.35M| }
88| 1.35M|}
89| |
90| 320k|def access(int x) -> void {
91| 320k| splay(x);
92| 320k| node[node[x].ch[1]].k = -1;
93| 320k| node[x].sum_ += node[node[x].ch[1]].sum;
94| 320k| node[x].size_ += node[node[x].ch[1]].size;
95| 320k| node[x].ch[1] = 0;
96| 320k| maintain(x);
97| 1.35M| while (int p = node[x].p) {
------------------
| Branch (97:14): [True: 76.30%, False: 23.70%]
------------------
98| 1.03M| splay(p);
99| 1.03M| node[node[p].ch[1]].k = -1;
100| 1.03M| node[p].sum_ += node[node[p].ch[1]].sum;
101| 1.03M| node[p].size_ += node[node[p].ch[1]].size;
102| 1.03M| node[p].sum_ -= node[x].sum;
103| 1.03M| node[p].size_ -= node[x].size;
104| 1.03M| node[p].ch[1] = x;
105| 1.03M| node[x].k = 1;
106| 1.03M| rotateup(x);
107| 1.03M| maintain(x);
108| 1.03M| }
109| 320k|}
110| |
111| |int head[N];
112| |struct {
113| | int to;
114| | int next;
115| |} edge[N * 2];
116| |
117| 53.3k|def build(int u, int p) -> void {
118| 160k| for (int e = head[u]; e; e = edge[e].next) {
^106k
------------------
| Branch (118:25): [True: 66.67%, False: 33.33%]
------------------
119| 106k| int v = edge[e].to;
120| 106k| if (v != p) {
------------------
| Branch (120:9): [True: 50.00%, False: 50.00%]
------------------
121| 53.3k| build(v, u);
122| 53.3k| node[v + 1].p = u + 1;
123| 53.3k| node[u + 1].sum_ += node[v + 1].sum;
124| 53.3k| node[u + 1].size_ += node[v + 1].size;
125| 53.3k| }
126| 106k| }
127| 53.3k| node[u + 1].sum = node[u + 1].sum_;
128| 53.3k| node[u + 1].size = node[u + 1].size_ + 1;
129| 53.3k|}
130| |
131| |} // namespace
132| |
133| 1|int main() {
134| 1| rd rd;
135| 1| wt wt;
136| 1| int n = rd.uh();
137| 1| int q = rd.uh();
138| 1|#ifdef LOCAL
139| 1| std::memset(head, 0, 4 * n);
140| 1|#endif
141| 53.3k| for (int i = 1; i <= n; ++i) node[i] = {.sum_ = rd.uw(), .k = -1};
^53.3k^53.3k
------------------
| Branch (141:19): [True: 100.00%, False: 0.00%]
------------------
142| 53.3k| for (int i = 1; i != n; ++i) {
^53.3k
------------------
| Branch (142:19): [True: 100.00%, False: 0.00%]
------------------
143| 53.3k| int u = rd.uh();
144| 53.3k| int v = rd.uh();
145| 53.3k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
146| 53.3k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
147| 53.3k| }
148| 1| build(0, 0);
149| 120k| while (q--) {
------------------
| Branch (149:10): [True: 100.00%, False: 0.00%]
------------------
150| 120k| let t = rd.u1();
151| 120k| if (t == 0) {
------------------
| Branch (151:9): [True: 33.37%, False: 66.63%]
------------------
152| 40.1k| int u = rd.uh() + 1;
153| 40.1k| int v = rd.uh() + 1;
154| 40.1k| access(u);
155| 40.1k| reverse(u);
156| 40.1k| access(v);
157| 40.1k| node[node[v].ch[0]].p = 0;
158| 40.1k| node[node[v].ch[0]].k = -1;
159| 40.1k| node[node[v].ch[0]].add += node[v].add;
160| 40.1k| node[v].ch[0] = 0;
161| 40.1k| int w = rd.uh() + 1;
162| 40.1k| int x = rd.uh() + 1;
163| 40.1k| access(w);
164| 40.1k| reverse(w);
165| 40.1k| access(x);
166| 40.1k| node[w].p = x;
167| 40.1k| node[w].add -= node[x].add;
168| 40.1k| node[w].sum -= node[x].add * node[w].size;
169| 40.1k| node[x].sum_ += node[w].sum;
170| 40.1k| node[x].size_ += node[w].size;
171| 40.1k| }
172| 120k| if (t == 1) {
------------------
| Branch (172:9): [True: 33.21%, False: 66.79%]
------------------
173| 39.9k| int u = rd.uh() + 1;
174| 39.9k| int p = rd.uh() + 1;
175| 39.9k| i64 x = rd.uw();
176| 39.9k| access(u);
177| 39.9k| reverse(u);
178| 39.9k| access(p);
179| 39.9k| node[u].add += x;
180| 39.9k| node[u].sum += x * node[u].size;
181| 39.9k| }
182| 120k| if (t == 2) {
------------------
| Branch (182:9): [True: 33.43%, False: 66.57%]
------------------
183| 40.1k| int u = rd.uh() + 1;
184| 40.1k| int p = rd.uh() + 1;
185| 40.1k| access(u);
186| 40.1k| reverse(u);
187| 40.1k| access(p);
188| 40.1k| i64 ans = node[u].sum + node[u].size * node[p].add;
189| 40.1k| wt.ud(ans);
190| 40.1k| }
191| 120k| }
192| 1| return 0;
193| 1|}