/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| 13.0k|def reverse(int x) -> void { node[x].rev ^= 1; }
21| |
22| 18.5k|def pushdown(int x) -> void {
23| 18.5k| if (node[x].rev) {
------------------
| Branch (23:7): [True: 32.30%, False: 67.70%]
------------------
24| 5.98k| std::swap(node[x].ch[0], node[x].ch[1]);
25| 5.98k| node[node[x].ch[0]].k = 0;
26| 5.98k| node[node[x].ch[1]].k = 1;
27| 5.98k| reverse(node[x].ch[0]);
28| 5.98k| reverse(node[x].ch[1]);
29| 5.98k| node[x].rev = 0;
30| 5.98k| }
31| 18.5k|}
32| |
33| 18.2k|def maintain(int x) -> void {
34| 18.2k| node[x].size =
35| 18.2k| node[node[x].ch[0]].size + 1 + node[node[x].ch[1]].size + node[x].size_;
36| 18.2k| node[x].sum = node[node[x].ch[0]].sum + node[node[x].ch[1]].sum +
37| 18.2k| node[x].sum_ + node[x].size * node[x].add;
38| 18.2k|}
39| |
40| 11.7k|def rotateup(int x) -> void {
41| 11.7k| int p = node[x].p;
42| 11.7k| int g = node[p].p;
43| 11.7k| int k = node[x].k;
44| 11.7k| int t = node[p].k;
45| 11.7k| i64 b = node[x].add;
46| 11.7k| i64 a = node[p].add;
47| 11.7k| node[x].add = a + b;
48| 11.7k| node[p].add = -b;
49| 11.7k| node[node[x].ch[k ^ 1]].add += b;
50| 11.7k| node[node[x].ch[k ^ 1]].sum += b * node[node[x].ch[k ^ 1]].size;
51| 11.7k| node[node[x].ch[k ^ 1]].p = p;
52| 11.7k| node[node[x].ch[k ^ 1]].k = k;
53| 11.7k| node[p].ch[k] = node[x].ch[k ^ 1];
54| 11.7k| node[p].p = x;
55| 11.7k| node[p].k = k ^ 1;
56| 11.7k| node[x].ch[k ^ 1] = p;
57| 11.7k| node[x].p = g;
58| 11.7k| node[x].k = t;
59| 11.7k| if (t != -1) {
------------------
| Branch (59:7): [True: 32.06%, False: 67.94%]
------------------
60| 3.75k| node[g].ch[t] = x;
61| 3.75k| }
62| 11.7k| maintain(p);
63| 11.7k|}
64| |
65| 15.8k|def is_root(int x) -> bool { return node[x].k == -1; }
66| |
67| 6.57k|def splay(int x) -> void {
68| 6.57k| pushdown(x);
69| 11.2k| while (!is_root(x)) {
------------------
| Branch (69:10): [True: 41.46%, False: 58.54%]
------------------
70| 4.65k| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (70:28): [True: 43.15%, False: 56.85%]
------------------
71| 2.00k| pushdown(p);
72| 2.00k| pushdown(x);
73| 2.00k| rotateup(x);
74| 2.64k| } else {
75| 2.64k| int g = node[p].p;
76| 2.64k| pushdown(g);
77| 2.64k| pushdown(p);
78| 2.64k| pushdown(x);
79| 2.64k| if (node[x].k == node[p].k) {
------------------
| Branch (79:11): [True: 52.27%, False: 47.73%]
------------------
80| 1.38k| rotateup(p);
81| 1.38k| rotateup(x);
82| 1.38k| } else {
83| 1.26k| rotateup(x);
84| 1.26k| rotateup(x);
85| 1.26k| }
86| 2.64k| }
87| 4.65k| }
88| 6.57k|}
89| |
90| 2.15k|def access(int x) -> void {
91| 2.15k| splay(x);
92| 2.15k| node[node[x].ch[1]].k = -1;
93| 2.15k| node[x].sum_ += node[node[x].ch[1]].sum;
94| 2.15k| node[x].size_ += node[node[x].ch[1]].size;
95| 2.15k| node[x].ch[1] = 0;
96| 2.15k| maintain(x);
97| 6.57k| while (int p = node[x].p) {
------------------
| Branch (97:14): [True: 67.19%, False: 32.81%]
------------------
98| 4.41k| splay(p);
99| 4.41k| node[node[p].ch[1]].k = -1;
100| 4.41k| node[p].sum_ += node[node[p].ch[1]].sum;
101| 4.41k| node[p].size_ += node[node[p].ch[1]].size;
102| 4.41k| node[p].sum_ -= node[x].sum;
103| 4.41k| node[p].size_ -= node[x].size;
104| 4.41k| node[p].ch[1] = x;
105| 4.41k| node[x].k = 1;
106| 4.41k| rotateup(x);
107| 4.41k| maintain(x);
108| 4.41k| }
109| 2.15k|}
110| |
111| |int head[N];
112| |struct {
113| | int to;
114| | int next;
115| |} edge[N * 2];
116| |
117| 533|def build(int u, int p) -> void {
118| 1.59k| for (int e = head[u]; e; e = edge[e].next) {
^1.06k
------------------
| Branch (118:25): [True: 66.62%, False: 33.38%]
------------------
119| 1.06k| int v = edge[e].to;
120| 1.06k| if (v != p) {
------------------
| Branch (120:9): [True: 50.00%, False: 50.00%]
------------------
121| 532| build(v, u);
122| 532| node[v + 1].p = u + 1;
123| 532| node[u + 1].sum_ += node[v + 1].sum;
124| 532| node[u + 1].size_ += node[v + 1].size;
125| 532| }
126| 1.06k| }
127| 533| node[u + 1].sum = node[u + 1].sum_;
128| 533| node[u + 1].size = node[u + 1].size_ + 1;
129| 533|}
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| 534| for (int i = 1; i <= n; ++i) node[i] = {.sum_ = rd.uw(), .k = -1};
^533 ^533
------------------
| Branch (141:19): [True: 99.81%, False: 0.19%]
------------------
142| 533| for (int i = 1; i != n; ++i) {
^532
------------------
| Branch (142:19): [True: 99.81%, False: 0.19%]
------------------
143| 532| int u = rd.uh();
144| 532| int v = rd.uh();
145| 532| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
146| 532| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
147| 532| }
148| 1| build(0, 0);
149| 814| while (q--) {
------------------
| Branch (149:10): [True: 99.88%, False: 0.12%]
------------------
150| 813| let t = rd.u1();
151| 813| if (t == 0) {
------------------
| Branch (151:9): [True: 32.60%, False: 67.40%]
------------------
152| 265| int u = rd.uh() + 1;
153| 265| int v = rd.uh() + 1;
154| 265| access(u);
155| 265| reverse(u);
156| 265| access(v);
157| 265| node[node[v].ch[0]].p = 0;
158| 265| node[node[v].ch[0]].k = -1;
159| 265| node[node[v].ch[0]].add += node[v].add;
160| 265| node[v].ch[0] = 0;
161| 265| int w = rd.uh() + 1;
162| 265| int x = rd.uh() + 1;
163| 265| access(w);
164| 265| reverse(w);
165| 265| access(x);
166| 265| node[w].p = x;
167| 265| node[w].add -= node[x].add;
168| 265| node[w].sum -= node[x].add * node[w].size;
169| 265| node[x].sum_ += node[w].sum;
170| 265| node[x].size_ += node[w].size;
171| 265| }
172| 813| if (t == 1) {
------------------
| Branch (172:9): [True: 33.83%, False: 66.17%]
------------------
173| 275| int u = rd.uh() + 1;
174| 275| int p = rd.uh() + 1;
175| 275| i64 x = rd.uw();
176| 275| access(u);
177| 275| reverse(u);
178| 275| access(p);
179| 275| node[u].add += x;
180| 275| node[u].sum += x * node[u].size;
181| 275| }
182| 813| if (t == 2) {
------------------
| Branch (182:9): [True: 33.58%, False: 66.42%]
------------------
183| 273| int u = rd.uh() + 1;
184| 273| int p = rd.uh() + 1;
185| 273| access(u);
186| 273| reverse(u);
187| 273| access(p);
188| 273| i64 ans = node[u].sum + node[u].size * node[p].add;
189| 273| wt.ud(ans);
190| 273| }
191| 813| }
192| 1| return 0;
193| 1|}