/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| 4.44k|def reverse(int x) -> void { node[x].rev ^= 1; }
21| |
22| 6.64k|def pushdown(int x) -> void {
23| 6.64k| if (node[x].rev) {
------------------
| Branch (23:7): [True: 30.46%, False: 69.54%]
------------------
24| 2.02k| std::swap(node[x].ch[0], node[x].ch[1]);
25| 2.02k| node[node[x].ch[0]].k = 0;
26| 2.02k| node[node[x].ch[1]].k = 1;
27| 2.02k| reverse(node[x].ch[0]);
28| 2.02k| reverse(node[x].ch[1]);
29| 2.02k| node[x].rev = 0;
30| 2.02k| }
31| 6.64k|}
32| |
33| 6.73k|def maintain(int x) -> void {
34| 6.73k| node[x].size =
35| 6.73k| node[node[x].ch[0]].size + 1 + node[node[x].ch[1]].size + node[x].size_;
36| 6.73k| node[x].sum = node[node[x].ch[0]].sum + node[node[x].ch[1]].sum +
37| 6.73k| node[x].sum_ + node[x].size * node[x].add;
38| 6.73k|}
39| |
40| 4.24k|def rotateup(int x) -> void {
41| 4.24k| int p = node[x].p;
42| 4.24k| int g = node[p].p;
43| 4.24k| int k = node[x].k;
44| 4.24k| int t = node[p].k;
45| 4.24k| i64 b = node[x].add;
46| 4.24k| i64 a = node[p].add;
47| 4.24k| node[x].add = a + b;
48| 4.24k| node[p].add = -b;
49| 4.24k| node[node[x].ch[k ^ 1]].add += b;
50| 4.24k| node[node[x].ch[k ^ 1]].sum += b * node[node[x].ch[k ^ 1]].size;
51| 4.24k| node[node[x].ch[k ^ 1]].p = p;
52| 4.24k| node[node[x].ch[k ^ 1]].k = k;
53| 4.24k| node[p].ch[k] = node[x].ch[k ^ 1];
54| 4.24k| node[p].p = x;
55| 4.24k| node[p].k = k ^ 1;
56| 4.24k| node[x].ch[k ^ 1] = p;
57| 4.24k| node[x].p = g;
58| 4.24k| node[x].k = t;
59| 4.24k| if (t != -1) {
------------------
| Branch (59:7): [True: 30.59%, False: 69.41%]
------------------
60| 1.29k| node[g].ch[t] = x;
61| 1.29k| }
62| 4.24k| maintain(p);
63| 4.24k|}
64| |
65| 5.72k|def is_root(int x) -> bool { return node[x].k == -1; }
66| |
67| 2.49k|def splay(int x) -> void {
68| 2.49k| pushdown(x);
69| 4.11k| while (!is_root(x)) {
------------------
| Branch (69:10): [True: 39.34%, False: 60.66%]
------------------
70| 1.61k| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (70:28): [True: 43.23%, False: 56.77%]
------------------
71| 699| pushdown(p);
72| 699| pushdown(x);
73| 699| rotateup(x);
74| 918| } else {
75| 918| int g = node[p].p;
76| 918| pushdown(g);
77| 918| pushdown(p);
78| 918| pushdown(x);
79| 918| if (node[x].k == node[p].k) {
------------------
| Branch (79:11): [True: 54.03%, False: 45.97%]
------------------
80| 496| rotateup(p);
81| 496| rotateup(x);
82| 496| } else {
83| 422| rotateup(x);
84| 422| rotateup(x);
85| 422| }
86| 918| }
87| 1.61k| }
88| 2.49k|}
89| |
90| 788|def access(int x) -> void {
91| 788| splay(x);
92| 788| node[node[x].ch[1]].k = -1;
93| 788| node[x].sum_ += node[node[x].ch[1]].sum;
94| 788| node[x].size_ += node[node[x].ch[1]].size;
95| 788| node[x].ch[1] = 0;
96| 788| maintain(x);
97| 2.49k| while (int p = node[x].p) {
------------------
| Branch (97:14): [True: 68.39%, False: 31.61%]
------------------
98| 1.70k| splay(p);
99| 1.70k| node[node[p].ch[1]].k = -1;
100| 1.70k| node[p].sum_ += node[node[p].ch[1]].sum;
101| 1.70k| node[p].size_ += node[node[p].ch[1]].size;
102| 1.70k| node[p].sum_ -= node[x].sum;
103| 1.70k| node[p].size_ -= node[x].size;
104| 1.70k| node[p].ch[1] = x;
105| 1.70k| node[x].k = 1;
106| 1.70k| rotateup(x);
107| 1.70k| maintain(x);
108| 1.70k| }
109| 788|}
110| |
111| |int head[N];
112| |struct {
113| | int to;
114| | int next;
115| |} edge[N * 2];
116| |
117| 694|def build(int u, int p) -> void {
118| 2.08k| for (int e = head[u]; e; e = edge[e].next) {
^1.38k
------------------
| Branch (118:25): [True: 66.63%, False: 33.37%]
------------------
119| 1.38k| int v = edge[e].to;
120| 1.38k| if (v != p) {
------------------
| Branch (120:9): [True: 50.00%, False: 50.00%]
------------------
121| 693| build(v, u);
122| 693| node[v + 1].p = u + 1;
123| 693| node[u + 1].sum_ += node[v + 1].sum;
124| 693| node[u + 1].size_ += node[v + 1].size;
125| 693| }
126| 1.38k| }
127| 694| node[u + 1].sum = node[u + 1].sum_;
128| 694| node[u + 1].size = node[u + 1].size_ + 1;
129| 694|}
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| 695| for (int i = 1; i <= n; ++i) node[i] = {.sum_ = rd.uw(), .k = -1};
^694 ^694
------------------
| Branch (141:19): [True: 99.86%, False: 0.14%]
------------------
142| 694| for (int i = 1; i != n; ++i) {
^693
------------------
| Branch (142:19): [True: 99.86%, False: 0.14%]
------------------
143| 693| int u = rd.uh();
144| 693| int v = rd.uh();
145| 693| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
146| 693| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
147| 693| }
148| 1| build(0, 0);
149| 300| while (q--) {
------------------
| Branch (149:10): [True: 99.67%, False: 0.33%]
------------------
150| 299| let t = rd.u1();
151| 299| if (t == 0) {
------------------
| Branch (151:9): [True: 31.77%, False: 68.23%]
------------------
152| 95| int u = rd.uh() + 1;
153| 95| int v = rd.uh() + 1;
154| 95| access(u);
155| 95| reverse(u);
156| 95| access(v);
157| 95| node[node[v].ch[0]].p = 0;
158| 95| node[node[v].ch[0]].k = -1;
159| 95| node[node[v].ch[0]].add += node[v].add;
160| 95| node[v].ch[0] = 0;
161| 95| int w = rd.uh() + 1;
162| 95| int x = rd.uh() + 1;
163| 95| access(w);
164| 95| reverse(w);
165| 95| access(x);
166| 95| node[w].p = x;
167| 95| node[w].add -= node[x].add;
168| 95| node[w].sum -= node[x].add * node[w].size;
169| 95| node[x].sum_ += node[w].sum;
170| 95| node[x].size_ += node[w].size;
171| 95| }
172| 299| if (t == 1) {
------------------
| Branch (172:9): [True: 34.11%, False: 65.89%]
------------------
173| 102| int u = rd.uh() + 1;
174| 102| int p = rd.uh() + 1;
175| 102| i64 x = rd.uw();
176| 102| access(u);
177| 102| reverse(u);
178| 102| access(p);
179| 102| node[u].add += x;
180| 102| node[u].sum += x * node[u].size;
181| 102| }
182| 299| if (t == 2) {
------------------
| Branch (182:9): [True: 34.11%, False: 65.89%]
------------------
183| 102| int u = rd.uh() + 1;
184| 102| int p = rd.uh() + 1;
185| 102| access(u);
186| 102| reverse(u);
187| 102| access(p);
188| 102| i64 ans = node[u].sum + node[u].size * node[p].add;
189| 102| wt.ud(ans);
190| 102| }
191| 299| }
192| 1| return 0;
193| 1|}