/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| 5.74k|def reverse(int x) -> void { node[x].rev ^= 1; }
21| |
22| 6.92k|def pushdown(int x) -> void {
23| 6.92k| if (node[x].rev) {
------------------
| Branch (23:7): [True: 37.71%, False: 62.29%]
------------------
24| 2.61k| std::swap(node[x].ch[0], node[x].ch[1]);
25| 2.61k| node[node[x].ch[0]].k = 0;
26| 2.61k| node[node[x].ch[1]].k = 1;
27| 2.61k| reverse(node[x].ch[0]);
28| 2.61k| reverse(node[x].ch[1]);
29| 2.61k| node[x].rev = 0;
30| 2.61k| }
31| 6.92k|}
32| |
33| 6.86k|def maintain(int x) -> void {
34| 6.86k| node[x].size =
35| 6.86k| node[node[x].ch[0]].size + 1 + node[node[x].ch[1]].size + node[x].size_;
36| 6.86k| node[x].sum = node[node[x].ch[0]].sum + node[node[x].ch[1]].sum +
37| 6.86k| node[x].sum_ + node[x].size * node[x].add;
38| 6.86k|}
39| |
40| 4.21k|def rotateup(int x) -> void {
41| 4.21k| int p = node[x].p;
42| 4.21k| int g = node[p].p;
43| 4.21k| int k = node[x].k;
44| 4.21k| int t = node[p].k;
45| 4.21k| i64 b = node[x].add;
46| 4.21k| i64 a = node[p].add;
47| 4.21k| node[x].add = a + b;
48| 4.21k| node[p].add = -b;
49| 4.21k| node[node[x].ch[k ^ 1]].add += b;
50| 4.21k| node[node[x].ch[k ^ 1]].sum += b * node[node[x].ch[k ^ 1]].size;
51| 4.21k| node[node[x].ch[k ^ 1]].p = p;
52| 4.21k| node[node[x].ch[k ^ 1]].k = k;
53| 4.21k| node[p].ch[k] = node[x].ch[k ^ 1];
54| 4.21k| node[p].p = x;
55| 4.21k| node[p].k = k ^ 1;
56| 4.21k| node[x].ch[k ^ 1] = p;
57| 4.21k| node[x].p = g;
58| 4.21k| node[x].k = t;
59| 4.21k| if (t != -1) {
------------------
| Branch (59:7): [True: 31.30%, False: 68.70%]
------------------
60| 1.31k| node[g].ch[t] = x;
61| 1.31k| }
62| 4.21k| maintain(p);
63| 4.21k|}
64| |
65| 6.00k|def is_root(int x) -> bool { return node[x].k == -1; }
66| |
67| 2.64k|def splay(int x) -> void {
68| 2.64k| pushdown(x);
69| 4.32k| while (!is_root(x)) {
------------------
| Branch (69:10): [True: 38.84%, False: 61.16%]
------------------
70| 1.68k| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (70:28): [True: 45.27%, False: 54.73%]
------------------
71| 761| pushdown(p);
72| 761| pushdown(x);
73| 761| rotateup(x);
74| 920| } else {
75| 920| int g = node[p].p;
76| 920| pushdown(g);
77| 920| pushdown(p);
78| 920| pushdown(x);
79| 920| if (node[x].k == node[p].k) {
------------------
| Branch (79:11): [True: 49.67%, False: 50.33%]
------------------
80| 457| rotateup(p);
81| 457| rotateup(x);
82| 463| } else {
83| 463| rotateup(x);
84| 463| rotateup(x);
85| 463| }
86| 920| }
87| 1.68k| }
88| 2.64k|}
89| |
90| 1.03k|def access(int x) -> void {
91| 1.03k| splay(x);
92| 1.03k| node[node[x].ch[1]].k = -1;
93| 1.03k| node[x].sum_ += node[node[x].ch[1]].sum;
94| 1.03k| node[x].size_ += node[node[x].ch[1]].size;
95| 1.03k| node[x].ch[1] = 0;
96| 1.03k| maintain(x);
97| 2.64k| while (int p = node[x].p) {
------------------
| Branch (97:14): [True: 60.94%, False: 39.06%]
------------------
98| 1.61k| splay(p);
99| 1.61k| node[node[p].ch[1]].k = -1;
100| 1.61k| node[p].sum_ += node[node[p].ch[1]].sum;
101| 1.61k| node[p].size_ += node[node[p].ch[1]].size;
102| 1.61k| node[p].sum_ -= node[x].sum;
103| 1.61k| node[p].size_ -= node[x].size;
104| 1.61k| node[p].ch[1] = x;
105| 1.61k| node[x].k = 1;
106| 1.61k| rotateup(x);
107| 1.61k| maintain(x);
108| 1.61k| }
109| 1.03k|}
110| |
111| |int head[N];
112| |struct {
113| | int to;
114| | int next;
115| |} edge[N * 2];
116| |
117| 89|def build(int u, int p) -> void {
118| 265| for (int e = head[u]; e; e = edge[e].next) {
^176
------------------
| Branch (118:25): [True: 66.42%, False: 33.58%]
------------------
119| 176| int v = edge[e].to;
120| 176| if (v != p) {
------------------
| Branch (120:9): [True: 50.00%, False: 50.00%]
------------------
121| 88| build(v, u);
122| 88| node[v + 1].p = u + 1;
123| 88| node[u + 1].sum_ += node[v + 1].sum;
124| 88| node[u + 1].size_ += node[v + 1].size;
125| 88| }
126| 176| }
127| 89| node[u + 1].sum = node[u + 1].sum_;
128| 89| node[u + 1].size = node[u + 1].size_ + 1;
129| 89|}
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| 90| for (int i = 1; i <= n; ++i) node[i] = {.sum_ = rd.uw(), .k = -1};
^89 ^89
------------------
| Branch (141:19): [True: 98.89%, False: 1.11%]
------------------
142| 89| for (int i = 1; i != n; ++i) {
^88
------------------
| Branch (142:19): [True: 98.88%, False: 1.12%]
------------------
143| 88| int u = rd.uh();
144| 88| int v = rd.uh();
145| 88| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
146| 88| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
147| 88| }
148| 1| build(0, 0);
149| 396| while (q--) {
------------------
| Branch (149:10): [True: 99.75%, False: 0.25%]
------------------
150| 395| let t = rd.u1();
151| 395| if (t == 0) {
------------------
| Branch (151:9): [True: 30.89%, False: 69.11%]
------------------
152| 122| int u = rd.uh() + 1;
153| 122| int v = rd.uh() + 1;
154| 122| access(u);
155| 122| reverse(u);
156| 122| access(v);
157| 122| node[node[v].ch[0]].p = 0;
158| 122| node[node[v].ch[0]].k = -1;
159| 122| node[node[v].ch[0]].add += node[v].add;
160| 122| node[v].ch[0] = 0;
161| 122| int w = rd.uh() + 1;
162| 122| int x = rd.uh() + 1;
163| 122| access(w);
164| 122| reverse(w);
165| 122| access(x);
166| 122| node[w].p = x;
167| 122| node[w].add -= node[x].add;
168| 122| node[w].sum -= node[x].add * node[w].size;
169| 122| node[x].sum_ += node[w].sum;
170| 122| node[x].size_ += node[w].size;
171| 122| }
172| 395| if (t == 1) {
------------------
| Branch (172:9): [True: 33.67%, False: 66.33%]
------------------
173| 133| int u = rd.uh() + 1;
174| 133| int p = rd.uh() + 1;
175| 133| i64 x = rd.uw();
176| 133| access(u);
177| 133| reverse(u);
178| 133| access(p);
179| 133| node[u].add += x;
180| 133| node[u].sum += x * node[u].size;
181| 133| }
182| 395| if (t == 2) {
------------------
| Branch (182:9): [True: 35.44%, False: 64.56%]
------------------
183| 140| int u = rd.uh() + 1;
184| 140| int p = rd.uh() + 1;
185| 140| access(u);
186| 140| reverse(u);
187| 140| access(p);
188| 140| i64 ans = node[u].sum + node[u].size * node[p].add;
189| 140| wt.ud(ans);
190| 140| }
191| 395| }
192| 1| return 0;
193| 1|}