/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.77M|def reverse(int x) -> void { node[x].rev ^= 1; }
21| |
22| 6.19M|def pushdown(int x) -> void {
23| 6.19M| if (node[x].rev) {
------------------
| Branch (23:7): [True: 20.79%, False: 79.21%]
------------------
24| 1.28M| std::swap(node[x].ch[0], node[x].ch[1]);
25| 1.28M| node[node[x].ch[0]].k = 0;
26| 1.28M| node[node[x].ch[1]].k = 1;
27| 1.28M| reverse(node[x].ch[0]);
28| 1.28M| reverse(node[x].ch[1]);
29| 1.28M| node[x].rev = 0;
30| 1.28M| }
31| 6.19M|}
32| |
33| 5.90M|def maintain(int x) -> void {
34| 5.90M| node[x].size =
35| 5.90M| node[node[x].ch[0]].size + 1 + node[node[x].ch[1]].size + node[x].size_;
36| 5.90M| node[x].sum = node[node[x].ch[0]].sum + node[node[x].ch[1]].sum +
37| 5.90M| node[x].sum_ + node[x].size * node[x].add;
38| 5.90M|}
39| |
40| 4.11M|def rotateup(int x) -> void {
41| 4.11M| int p = node[x].p;
42| 4.11M| int g = node[p].p;
43| 4.11M| int k = node[x].k;
44| 4.11M| int t = node[p].k;
45| 4.11M| i64 b = node[x].add;
46| 4.11M| i64 a = node[p].add;
47| 4.11M| node[x].add = a + b;
48| 4.11M| node[p].add = -b;
49| 4.11M| node[node[x].ch[k ^ 1]].add += b;
50| 4.11M| node[node[x].ch[k ^ 1]].sum += b * node[node[x].ch[k ^ 1]].size;
51| 4.11M| node[node[x].ch[k ^ 1]].p = p;
52| 4.11M| node[node[x].ch[k ^ 1]].k = k;
53| 4.11M| node[p].ch[k] = node[x].ch[k ^ 1];
54| 4.11M| node[p].p = x;
55| 4.11M| node[p].k = k ^ 1;
56| 4.11M| node[x].ch[k ^ 1] = p;
57| 4.11M| node[x].p = g;
58| 4.11M| node[x].k = t;
59| 4.11M| if (t != -1) {
------------------
| Branch (59:7): [True: 35.38%, False: 64.62%]
------------------
60| 1.45M| node[g].ch[t] = x;
61| 1.45M| }
62| 4.11M| maintain(p);
63| 4.11M|}
64| |
65| 5.16M|def is_root(int x) -> bool { return node[x].k == -1; }
66| |
67| 1.79M|def splay(int x) -> void {
68| 1.79M| pushdown(x);
69| 3.48M| while (!is_root(x)) {
------------------
| Branch (69:10): [True: 48.45%, False: 51.55%]
------------------
70| 1.68M| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (70:28): [True: 39.09%, False: 60.91%]
------------------
71| 659k| pushdown(p);
72| 659k| pushdown(x);
73| 659k| rotateup(x);
74| 1.02M| } else {
75| 1.02M| int g = node[p].p;
76| 1.02M| pushdown(g);
77| 1.02M| pushdown(p);
78| 1.02M| pushdown(x);
79| 1.02M| if (node[x].k == node[p].k) {
------------------
| Branch (79:11): [True: 57.68%, False: 42.32%]
------------------
80| 592k| rotateup(p);
81| 592k| rotateup(x);
82| 592k| } else {
83| 434k| rotateup(x);
84| 434k| rotateup(x);
85| 434k| }
86| 1.02M| }
87| 1.68M| }
88| 1.79M|}
89| |
90| 396k|def access(int x) -> void {
91| 396k| splay(x);
92| 396k| node[node[x].ch[1]].k = -1;
93| 396k| node[x].sum_ += node[node[x].ch[1]].sum;
94| 396k| node[x].size_ += node[node[x].ch[1]].size;
95| 396k| node[x].ch[1] = 0;
96| 396k| maintain(x);
97| 1.79M| while (int p = node[x].p) {
------------------
| Branch (97:14): [True: 77.88%, False: 22.12%]
------------------
98| 1.39M| splay(p);
99| 1.39M| node[node[p].ch[1]].k = -1;
100| 1.39M| node[p].sum_ += node[node[p].ch[1]].sum;
101| 1.39M| node[p].size_ += node[node[p].ch[1]].size;
102| 1.39M| node[p].sum_ -= node[x].sum;
103| 1.39M| node[p].size_ -= node[x].size;
104| 1.39M| node[p].ch[1] = x;
105| 1.39M| node[x].k = 1;
106| 1.39M| rotateup(x);
107| 1.39M| maintain(x);
108| 1.39M| }
109| 396k|}
110| |
111| |int head[N];
112| |struct {
113| | int to;
114| | int next;
115| |} edge[N * 2];
116| |
117| 150k|def build(int u, int p) -> void {
118| 452k| for (int e = head[u]; e; e = edge[e].next) {
^301k
------------------
| Branch (118:25): [True: 66.67%, False: 33.33%]
------------------
119| 301k| int v = edge[e].to;
120| 301k| if (v != p) {
------------------
| Branch (120:9): [True: 50.00%, False: 50.00%]
------------------
121| 150k| build(v, u);
122| 150k| node[v + 1].p = u + 1;
123| 150k| node[u + 1].sum_ += node[v + 1].sum;
124| 150k| node[u + 1].size_ += node[v + 1].size;
125| 150k| }
126| 301k| }
127| 150k| node[u + 1].sum = node[u + 1].sum_;
128| 150k| node[u + 1].size = node[u + 1].size_ + 1;
129| 150k|}
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| 150k| for (int i = 1; i <= n; ++i) node[i] = {.sum_ = rd.uw(), .k = -1};
^150k^150k
------------------
| Branch (141:19): [True: 100.00%, False: 0.00%]
------------------
142| 150k| for (int i = 1; i != n; ++i) {
^150k
------------------
| Branch (142:19): [True: 100.00%, False: 0.00%]
------------------
143| 150k| int u = rd.uh();
144| 150k| int v = rd.uh();
145| 150k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
146| 150k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
147| 150k| }
148| 1| build(0, 0);
149| 148k| while (q--) {
------------------
| Branch (149:10): [True: 100.00%, False: 0.00%]
------------------
150| 148k| let t = rd.u1();
151| 148k| if (t == 0) {
------------------
| Branch (151:9): [True: 33.41%, False: 66.59%]
------------------
152| 49.7k| int u = rd.uh() + 1;
153| 49.7k| int v = rd.uh() + 1;
154| 49.7k| access(u);
155| 49.7k| reverse(u);
156| 49.7k| access(v);
157| 49.7k| node[node[v].ch[0]].p = 0;
158| 49.7k| node[node[v].ch[0]].k = -1;
159| 49.7k| node[node[v].ch[0]].add += node[v].add;
160| 49.7k| node[v].ch[0] = 0;
161| 49.7k| int w = rd.uh() + 1;
162| 49.7k| int x = rd.uh() + 1;
163| 49.7k| access(w);
164| 49.7k| reverse(w);
165| 49.7k| access(x);
166| 49.7k| node[w].p = x;
167| 49.7k| node[w].add -= node[x].add;
168| 49.7k| node[w].sum -= node[x].add * node[w].size;
169| 49.7k| node[x].sum_ += node[w].sum;
170| 49.7k| node[x].size_ += node[w].size;
171| 49.7k| }
172| 148k| if (t == 1) {
------------------
| Branch (172:9): [True: 33.15%, False: 66.85%]
------------------
173| 49.3k| int u = rd.uh() + 1;
174| 49.3k| int p = rd.uh() + 1;
175| 49.3k| i64 x = rd.uw();
176| 49.3k| access(u);
177| 49.3k| reverse(u);
178| 49.3k| access(p);
179| 49.3k| node[u].add += x;
180| 49.3k| node[u].sum += x * node[u].size;
181| 49.3k| }
182| 148k| if (t == 2) {
------------------
| Branch (182:9): [True: 33.43%, False: 66.57%]
------------------
183| 49.7k| int u = rd.uh() + 1;
184| 49.7k| int p = rd.uh() + 1;
185| 49.7k| access(u);
186| 49.7k| reverse(u);
187| 49.7k| access(p);
188| 49.7k| i64 ans = node[u].sum + node[u].size * node[p].add;
189| 49.7k| wt.ud(ans);
190| 49.7k| }
191| 148k| }
192| 1| return 0;
193| 1|}