/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| 54.6M|def reverse(int x) -> void { node[x].rev ^= 1; }
21| |
22| 93.5M|def pushdown(int x) -> void {
23| 93.5M| if (node[x].rev) {
------------------
| Branch (23:7): [True: 27.85%, False: 72.15%]
------------------
24| 26.0M| std::swap(node[x].ch[0], node[x].ch[1]);
25| 26.0M| node[node[x].ch[0]].k = 0;
26| 26.0M| node[node[x].ch[1]].k = 1;
27| 26.0M| reverse(node[x].ch[0]);
28| 26.0M| reverse(node[x].ch[1]);
29| 26.0M| node[x].rev = 0;
30| 26.0M| }
31| 93.5M|}
32| |
33| 82.7M|def maintain(int x) -> void {
34| 82.7M| node[x].size =
35| 82.7M| node[node[x].ch[0]].size + 1 + node[node[x].ch[1]].size + node[x].size_;
36| 82.7M| node[x].sum = node[node[x].ch[0]].sum + node[node[x].ch[1]].sum +
37| 82.7M| node[x].sum_ + node[x].size * node[x].add;
38| 82.7M|}
39| |
40| 61.6M|def rotateup(int x) -> void {
41| 61.6M| int p = node[x].p;
42| 61.6M| int g = node[p].p;
43| 61.6M| int k = node[x].k;
44| 61.6M| int t = node[p].k;
45| 61.6M| i64 b = node[x].add;
46| 61.6M| i64 a = node[p].add;
47| 61.6M| node[x].add = a + b;
48| 61.6M| node[p].add = -b;
49| 61.6M| node[node[x].ch[k ^ 1]].add += b;
50| 61.6M| node[node[x].ch[k ^ 1]].sum += b * node[node[x].ch[k ^ 1]].size;
51| 61.6M| node[node[x].ch[k ^ 1]].p = p;
52| 61.6M| node[node[x].ch[k ^ 1]].k = k;
53| 61.6M| node[p].ch[k] = node[x].ch[k ^ 1];
54| 61.6M| node[p].p = x;
55| 61.6M| node[p].k = k ^ 1;
56| 61.6M| node[x].ch[k ^ 1] = p;
57| 61.6M| node[x].p = g;
58| 61.6M| node[x].k = t;
59| 61.6M| if (t != -1) {
------------------
| Branch (59:7): [True: 51.00%, False: 49.00%]
------------------
60| 31.4M| node[g].ch[t] = x;
61| 31.4M| }
62| 61.6M| maintain(p);
63| 61.6M|}
64| |
65| 74.3M|def is_root(int x) -> bool { return node[x].k == -1; }
66| |
67| 21.0M|def splay(int x) -> void {
68| 21.0M| pushdown(x);
69| 47.7M| while (!is_root(x)) {
------------------
| Branch (69:10): [True: 55.83%, False: 44.17%]
------------------
70| 26.6M| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (70:28): [True: 28.13%, False: 71.87%]
------------------
71| 7.49M| pushdown(p);
72| 7.49M| pushdown(x);
73| 7.49M| rotateup(x);
74| 19.1M| } else {
75| 19.1M| int g = node[p].p;
76| 19.1M| pushdown(g);
77| 19.1M| pushdown(p);
78| 19.1M| pushdown(x);
79| 19.1M| if (node[x].k == node[p].k) {
------------------
| Branch (79:11): [True: 60.54%, False: 39.46%]
------------------
80| 11.5M| rotateup(p);
81| 11.5M| rotateup(x);
82| 11.5M| } else {
83| 7.55M| rotateup(x);
84| 7.55M| rotateup(x);
85| 7.55M| }
86| 19.1M| }
87| 26.6M| }
88| 21.0M|}
89| |
90| 5.18M|def access(int x) -> void {
91| 5.18M| splay(x);
92| 5.18M| node[node[x].ch[1]].k = -1;
93| 5.18M| node[x].sum_ += node[node[x].ch[1]].sum;
94| 5.18M| node[x].size_ += node[node[x].ch[1]].size;
95| 5.18M| node[x].ch[1] = 0;
96| 5.18M| maintain(x);
97| 21.0M| while (int p = node[x].p) {
------------------
| Branch (97:14): [True: 75.40%, False: 24.60%]
------------------
98| 15.8M| splay(p);
99| 15.8M| node[node[p].ch[1]].k = -1;
100| 15.8M| node[p].sum_ += node[node[p].ch[1]].sum;
101| 15.8M| node[p].size_ += node[node[p].ch[1]].size;
102| 15.8M| node[p].sum_ -= node[x].sum;
103| 15.8M| node[p].size_ -= node[x].size;
104| 15.8M| node[p].ch[1] = x;
105| 15.8M| node[x].k = 1;
106| 15.8M| rotateup(x);
107| 15.8M| maintain(x);
108| 15.8M| }
109| 5.18M|}
110| |
111| |int head[N];
112| |struct {
113| | int to;
114| | int next;
115| |} edge[N * 2];
116| |
117| 1.91M|def build(int u, int p) -> void {
118| 5.74M| for (int e = head[u]; e; e = edge[e].next) {
^3.83M
------------------
| Branch (118:25): [True: 66.67%, False: 33.33%]
------------------
119| 3.83M| int v = edge[e].to;
120| 3.83M| if (v != p) {
------------------
| Branch (120:9): [True: 50.00%, False: 50.00%]
------------------
121| 1.91M| build(v, u);
122| 1.91M| node[v + 1].p = u + 1;
123| 1.91M| node[u + 1].sum_ += node[v + 1].sum;
124| 1.91M| node[u + 1].size_ += node[v + 1].size;
125| 1.91M| }
126| 3.83M| }
127| 1.91M| node[u + 1].sum = node[u + 1].sum_;
128| 1.91M| node[u + 1].size = node[u + 1].size_ + 1;
129| 1.91M|}
130| |
131| |} // namespace
132| |
133| 18|int main() {
134| 18| rd rd;
135| 18| wt wt;
136| 18| int n = rd.uh();
137| 18| int q = rd.uh();
138| 18|#ifdef LOCAL
139| 18| std::memset(head, 0, 4 * n);
140| 18|#endif
141| 1.91M| for (int i = 1; i <= n; ++i) node[i] = {.sum_ = rd.uw(), .k = -1};
^1.91M^1.91M
------------------
| Branch (141:19): [True: 100.00%, False: 0.00%]
------------------
142| 1.91M| for (int i = 1; i != n; ++i) {
^1.91M
------------------
| Branch (142:19): [True: 100.00%, False: 0.00%]
------------------
143| 1.91M| int u = rd.uh();
144| 1.91M| int v = rd.uh();
145| 1.91M| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
146| 1.91M| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
147| 1.91M| }
148| 18| build(0, 0);
149| 1.94M| while (q--) {
------------------
| Branch (149:10): [True: 100.00%, False: 0.00%]
------------------
150| 1.94M| let t = rd.u1();
151| 1.94M| if (t == 0) {
------------------
| Branch (151:9): [True: 33.31%, False: 66.69%]
------------------
152| 647k| int u = rd.uh() + 1;
153| 647k| int v = rd.uh() + 1;
154| 647k| access(u);
155| 647k| reverse(u);
156| 647k| access(v);
157| 647k| node[node[v].ch[0]].p = 0;
158| 647k| node[node[v].ch[0]].k = -1;
159| 647k| node[node[v].ch[0]].add += node[v].add;
160| 647k| node[v].ch[0] = 0;
161| 647k| int w = rd.uh() + 1;
162| 647k| int x = rd.uh() + 1;
163| 647k| access(w);
164| 647k| reverse(w);
165| 647k| access(x);
166| 647k| node[w].p = x;
167| 647k| node[w].add -= node[x].add;
168| 647k| node[w].sum -= node[x].add * node[w].size;
169| 647k| node[x].sum_ += node[w].sum;
170| 647k| node[x].size_ += node[w].size;
171| 647k| }
172| 1.94M| if (t == 1) {
------------------
| Branch (172:9): [True: 33.34%, False: 66.66%]
------------------
173| 648k| int u = rd.uh() + 1;
174| 648k| int p = rd.uh() + 1;
175| 648k| i64 x = rd.uw();
176| 648k| access(u);
177| 648k| reverse(u);
178| 648k| access(p);
179| 648k| node[u].add += x;
180| 648k| node[u].sum += x * node[u].size;
181| 648k| }
182| 1.94M| if (t == 2) {
------------------
| Branch (182:9): [True: 33.35%, False: 66.65%]
------------------
183| 648k| int u = rd.uh() + 1;
184| 648k| int p = rd.uh() + 1;
185| 648k| access(u);
186| 648k| reverse(u);
187| 648k| access(p);
188| 648k| i64 ans = node[u].sum + node[u].size * node[p].add;
189| 648k| wt.ud(ans);
190| 648k| }
191| 1.94M| }
192| 18| return 0;
193| 18|}