/tmp/solutions/build/dynamic_tree_vertex_add_path_sum-main.cpp:
1| |#include <common.h>
2| |prelude;
3| |
4| |namespace {
5| |
6| |constexpr int N = 200001;
7| |
8| |struct Node {
9| | u64 val;
10| | u64 sum;
11| | int p;
12| | int ch[2];
13| | i8 rev;
14| | i8 k;
15| |} node[N];
16| |
17| 1.59M|def reverse(int x) -> void { node[x].rev ^= 1; }
18| |
19| 3.75M|def pushdown(int x) -> void {
20| 3.75M| if (node[x].rev) {
------------------
| Branch (20:7): [True: 19.32%, False: 80.68%]
------------------
21| 725k| std::swap(node[x].ch[0], node[x].ch[1]);
22| 725k| node[node[x].ch[0]].k = 0;
23| 725k| node[node[x].ch[1]].k = 1;
24| 725k| reverse(node[x].ch[0]);
25| 725k| reverse(node[x].ch[1]);
26| 725k| node[x].rev = 0;
27| 725k| }
28| 3.75M|}
29| |
30| 2.46M|def maintain(int x) -> void {
31| 2.46M| node[x].sum = node[node[x].ch[0]].sum + node[x].val + node[node[x].ch[1]].sum;
32| 2.46M|}
33| |
34| 2.46M|def rotateup(int x) -> void {
35| 2.46M| int p = node[x].p;
36| 2.46M| int g = node[p].p;
37| 2.46M| int k = node[x].k;
38| 2.46M| int t = node[p].k;
39| 2.46M| node[node[x].ch[k ^ 1]].p = p;
40| 2.46M| node[node[x].ch[k ^ 1]].k = k;
41| 2.46M| node[p].ch[k] = node[x].ch[k ^ 1];
42| 2.46M| node[p].p = x;
43| 2.46M| node[p].k = k ^ 1;
44| 2.46M| node[x].ch[k ^ 1] = p;
45| 2.46M| node[x].p = g;
46| 2.46M| node[x].k = t;
47| 2.46M| if (t != -1) {
------------------
| Branch (47:7): [True: 22.32%, False: 77.68%]
------------------
48| 549k| node[g].ch[t] = x;
49| 549k| }
50| 2.46M| maintain(p);
51| 2.46M|}
52| |
53| 3.29M|def is_root(int x) -> bool { return node[x].k == -1; }
54| |
55| 1.30M|def splay(int x) -> void {
56| 1.30M| pushdown(x);
57| 2.29M| while (!is_root(x)) {
------------------
| Branch (57:10): [True: 43.30%, False: 56.70%]
------------------
58| 994k| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (58:28): [True: 53.49%, False: 46.51%]
------------------
59| 531k| pushdown(p);
60| 531k| pushdown(x);
61| 531k| rotateup(x);
62| 531k| } else {
63| 462k| int g = node[p].p;
64| 462k| pushdown(g);
65| 462k| pushdown(p);
66| 462k| pushdown(x);
67| 462k| if (node[x].k == node[p].k) {
------------------
| Branch (67:11): [True: 42.25%, False: 57.75%]
------------------
68| 195k| rotateup(p);
69| 195k| rotateup(x);
70| 267k| } else {
71| 267k| rotateup(x);
72| 267k| rotateup(x);
73| 267k| }
74| 462k| }
75| 994k| }
76| 1.30M|}
77| |
78| 247k|def access(int x) -> void {
79| 247k| splay(x);
80| 247k| node[node[x].ch[1]].k = -1;
81| 247k| node[x].ch[1] = 0;
82| 1.25M| while (int p = node[x].p) {
------------------
| Branch (82:14): [True: 80.21%, False: 19.79%]
------------------
83| 1.00M| splay(p);
84| 1.00M| node[node[p].ch[1]].k = -1;
85| 1.00M| node[p].ch[1] = x;
86| 1.00M| node[x].k = 1;
87| 1.00M| rotateup(x);
88| 1.00M| }
89| 247k|}
90| |
91| |int head[N];
92| |struct {
93| | int to;
94| | int next;
95| |} edge[N * 2];
96| |
97| 150k|def build(int u, int p) -> void {
98| 452k| for (int e = head[u]; e; e = edge[e].next) {
^301k
------------------
| Branch (98:25): [True: 66.67%, False: 33.33%]
------------------
99| 301k| int v = edge[e].to;
100| 301k| if (v != p) {
------------------
| Branch (100:9): [True: 50.00%, False: 50.00%]
------------------
101| 150k| build(v, u);
102| 150k| node[v + 1].p = u + 1;
103| 150k| }
104| 301k| }
105| 150k|}
106| |
107| |} // namespace
108| |
109| 1|int main() {
110| 1| rd rd;
111| 1| wt wt;
112| 1| int n = rd.uh();
113| 1| int q = rd.uh();
114| 1|#ifdef LOCAL
115| 1| std::memset(head, 0, 4 * n);
116| 1|#endif
117| 150k| for (int i = 1; i <= n; ++i)
^150k
------------------
| Branch (117:19): [True: 100.00%, False: 0.00%]
------------------
118| 150k| node[i] = {.k = -1}, node[i].sum = node[i].val = rd.uw();
119| 150k| for (int i = 1; i != n; ++i) {
^150k
------------------
| Branch (119:19): [True: 100.00%, False: 0.00%]
------------------
120| 150k| int u = rd.uh();
121| 150k| int v = rd.uh();
122| 150k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
123| 150k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
124| 150k| }
125| 1| build(0, 0);
126| 148k| while (q--) {
------------------
| Branch (126:10): [True: 100.00%, False: 0.00%]
------------------
127| 148k| let t = rd.u1();
128| 148k| if (t == 0) {
------------------
| Branch (128:9): [True: 33.25%, False: 66.75%]
------------------
129| 49.4k| int u = rd.uh() + 1;
130| 49.4k| int v = rd.uh() + 1;
131| 49.4k| access(u);
132| 49.4k| reverse(u);
133| 49.4k| access(v);
134| 49.4k| node[node[v].ch[0]].p = 0;
135| 49.4k| node[node[v].ch[0]].k = -1;
136| 49.4k| node[v].ch[0] = 0;
137| 49.4k| int w = rd.uh() + 1;
138| 49.4k| int x = rd.uh() + 1;
139| 49.4k| access(w);
140| 49.4k| reverse(w);
141| 49.4k| node[w].p = x;
142| 49.4k| }
143| 148k| if (t == 1) {
------------------
| Branch (143:9): [True: 33.29%, False: 66.71%]
------------------
144| 49.5k| int u = rd.uh() + 1;
145| 49.5k| u32 x = rd.uw();
146| 49.5k| splay(u);
147| 49.5k| node[u].val += x;
148| 49.5k| node[u].sum += x;
149| 49.5k| }
150| 148k| if (t == 2) {
------------------
| Branch (150:9): [True: 33.46%, False: 66.54%]
------------------
151| 49.7k| int u = rd.uh() + 1;
152| 49.7k| int v = rd.uh() + 1;
153| 49.7k| access(u);
154| 49.7k| reverse(u);
155| 49.7k| access(v);
156| 49.7k| wt.ud(node[node[v].ch[0]].sum + node[v].val);
157| 49.7k| }
158| 148k| }
159| 1| return 0;
160| 1|}