/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.35M|def reverse(int x) -> void { node[x].rev ^= 1; }
18| |
19| 2.84M|def pushdown(int x) -> void {
20| 2.84M| if (node[x].rev) {
------------------
| Branch (20:7): [True: 21.65%, False: 78.35%]
------------------
21| 615k| std::swap(node[x].ch[0], node[x].ch[1]);
22| 615k| node[node[x].ch[0]].k = 0;
23| 615k| node[node[x].ch[1]].k = 1;
24| 615k| reverse(node[x].ch[0]);
25| 615k| reverse(node[x].ch[1]);
26| 615k| node[x].rev = 0;
27| 615k| }
28| 2.84M|}
29| |
30| 1.84M|def maintain(int x) -> void {
31| 1.84M| node[x].sum = node[node[x].ch[0]].sum + node[x].val + node[node[x].ch[1]].sum;
32| 1.84M|}
33| |
34| 1.84M|def rotateup(int x) -> void {
35| 1.84M| int p = node[x].p;
36| 1.84M| int g = node[p].p;
37| 1.84M| int k = node[x].k;
38| 1.84M| int t = node[p].k;
39| 1.84M| node[node[x].ch[k ^ 1]].p = p;
40| 1.84M| node[node[x].ch[k ^ 1]].k = k;
41| 1.84M| node[p].ch[k] = node[x].ch[k ^ 1];
42| 1.84M| node[p].p = x;
43| 1.84M| node[p].k = k ^ 1;
44| 1.84M| node[x].ch[k ^ 1] = p;
45| 1.84M| node[x].p = g;
46| 1.84M| node[x].k = t;
47| 1.84M| if (t != -1) {
------------------
| Branch (47:7): [True: 23.62%, False: 76.38%]
------------------
48| 436k| node[g].ch[t] = x;
49| 436k| }
50| 1.84M| maintain(p);
51| 1.84M|}
52| |
53| 2.48M|def is_root(int x) -> bool { return node[x].k == -1; }
54| |
55| 973k|def splay(int x) -> void {
56| 973k| pushdown(x);
57| 1.72M| while (!is_root(x)) {
------------------
| Branch (57:10): [True: 43.72%, False: 56.28%]
------------------
58| 756k| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (58:28): [True: 52.81%, False: 47.19%]
------------------
59| 399k| pushdown(p);
60| 399k| pushdown(x);
61| 399k| rotateup(x);
62| 399k| } else {
63| 356k| int g = node[p].p;
64| 356k| pushdown(g);
65| 356k| pushdown(p);
66| 356k| pushdown(x);
67| 356k| if (node[x].k == node[p].k) {
------------------
| Branch (67:11): [True: 41.65%, False: 58.35%]
------------------
68| 148k| rotateup(p);
69| 148k| rotateup(x);
70| 208k| } else {
71| 208k| rotateup(x);
72| 208k| rotateup(x);
73| 208k| }
74| 356k| }
75| 756k| }
76| 973k|}
77| |
78| 199k|def access(int x) -> void {
79| 199k| splay(x);
80| 199k| node[node[x].ch[1]].k = -1;
81| 199k| node[x].ch[1] = 0;
82| 932k| while (int p = node[x].p) {
------------------
| Branch (82:14): [True: 78.59%, False: 21.41%]
------------------
83| 733k| splay(p);
84| 733k| node[node[p].ch[1]].k = -1;
85| 733k| node[p].ch[1] = x;
86| 733k| node[x].k = 1;
87| 733k| rotateup(x);
88| 733k| }
89| 199k|}
90| |
91| |int head[N];
92| |struct {
93| | int to;
94| | int next;
95| |} edge[N * 2];
96| |
97| 53.3k|def build(int u, int p) -> void {
98| 160k| for (int e = head[u]; e; e = edge[e].next) {
^106k
------------------
| Branch (98:25): [True: 66.67%, False: 33.33%]
------------------
99| 106k| int v = edge[e].to;
100| 106k| if (v != p) {
------------------
| Branch (100:9): [True: 50.00%, False: 50.00%]
------------------
101| 53.3k| build(v, u);
102| 53.3k| node[v + 1].p = u + 1;
103| 53.3k| }
104| 106k| }
105| 53.3k|}
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| 53.3k| for (int i = 1; i <= n; ++i)
^53.3k
------------------
| Branch (117:19): [True: 100.00%, False: 0.00%]
------------------
118| 53.3k| node[i] = {.k = -1}, node[i].sum = node[i].val = rd.uw();
119| 53.3k| for (int i = 1; i != n; ++i) {
^53.3k
------------------
| Branch (119:19): [True: 100.00%, False: 0.00%]
------------------
120| 53.3k| int u = rd.uh();
121| 53.3k| int v = rd.uh();
122| 53.3k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
123| 53.3k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
124| 53.3k| }
125| 1| build(0, 0);
126| 120k| while (q--) {
------------------
| Branch (126:10): [True: 100.00%, False: 0.00%]
------------------
127| 120k| let t = rd.u1();
128| 120k| if (t == 0) {
------------------
| Branch (128:9): [True: 33.23%, False: 66.77%]
------------------
129| 39.9k| int u = rd.uh() + 1;
130| 39.9k| int v = rd.uh() + 1;
131| 39.9k| access(u);
132| 39.9k| reverse(u);
133| 39.9k| access(v);
134| 39.9k| node[node[v].ch[0]].p = 0;
135| 39.9k| node[node[v].ch[0]].k = -1;
136| 39.9k| node[v].ch[0] = 0;
137| 39.9k| int w = rd.uh() + 1;
138| 39.9k| int x = rd.uh() + 1;
139| 39.9k| access(w);
140| 39.9k| reverse(w);
141| 39.9k| node[w].p = x;
142| 39.9k| }
143| 120k| if (t == 1) {
------------------
| Branch (143:9): [True: 33.54%, False: 66.46%]
------------------
144| 40.3k| int u = rd.uh() + 1;
145| 40.3k| u32 x = rd.uw();
146| 40.3k| splay(u);
147| 40.3k| node[u].val += x;
148| 40.3k| node[u].sum += x;
149| 40.3k| }
150| 120k| if (t == 2) {
------------------
| Branch (150:9): [True: 33.23%, False: 66.77%]
------------------
151| 39.9k| int u = rd.uh() + 1;
152| 39.9k| int v = rd.uh() + 1;
153| 39.9k| access(u);
154| 39.9k| reverse(u);
155| 39.9k| access(v);
156| 39.9k| wt.ud(node[node[v].ch[0]].sum + node[v].val);
157| 39.9k| }
158| 120k| }
159| 1| return 0;
160| 1|}