/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| 277k|def reverse(int x) -> void { node[x].rev ^= 1; }
18| |
19| 644k|def pushdown(int x) -> void {
20| 644k| if (node[x].rev) {
------------------
| Branch (20:7): [True: 19.54%, False: 80.46%]
------------------
21| 125k| std::swap(node[x].ch[0], node[x].ch[1]);
22| 125k| node[node[x].ch[0]].k = 0;
23| 125k| node[node[x].ch[1]].k = 1;
24| 125k| reverse(node[x].ch[0]);
25| 125k| reverse(node[x].ch[1]);
26| 125k| node[x].rev = 0;
27| 125k| }
28| 644k|}
29| |
30| 429k|def maintain(int x) -> void {
31| 429k| node[x].sum = node[node[x].ch[0]].sum + node[x].val + node[node[x].ch[1]].sum;
32| 429k|}
33| |
34| 429k|def rotateup(int x) -> void {
35| 429k| int p = node[x].p;
36| 429k| int g = node[p].p;
37| 429k| int k = node[x].k;
38| 429k| int t = node[p].k;
39| 429k| node[node[x].ch[k ^ 1]].p = p;
40| 429k| node[node[x].ch[k ^ 1]].k = k;
41| 429k| node[p].ch[k] = node[x].ch[k ^ 1];
42| 429k| node[p].p = x;
43| 429k| node[p].k = k ^ 1;
44| 429k| node[x].ch[k ^ 1] = p;
45| 429k| node[x].p = g;
46| 429k| node[x].k = t;
47| 429k| if (t != -1) {
------------------
| Branch (47:7): [True: 22.05%, False: 77.95%]
------------------
48| 94.7k| node[g].ch[t] = x;
49| 94.7k| }
50| 429k| maintain(p);
51| 429k|}
52| |
53| 566k|def is_root(int x) -> bool { return node[x].k == -1; }
54| |
55| 239k|def splay(int x) -> void {
56| 239k| pushdown(x);
57| 402k| while (!is_root(x)) {
------------------
| Branch (57:10): [True: 40.50%, False: 59.50%]
------------------
58| 163k| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (58:28): [True: 52.06%, False: 47.94%]
------------------
59| 84.9k| pushdown(p);
60| 84.9k| pushdown(x);
61| 84.9k| rotateup(x);
62| 84.9k| } else {
63| 78.2k| int g = node[p].p;
64| 78.2k| pushdown(g);
65| 78.2k| pushdown(p);
66| 78.2k| pushdown(x);
67| 78.2k| if (node[x].k == node[p].k) {
------------------
| Branch (67:11): [True: 42.66%, False: 57.34%]
------------------
68| 33.3k| rotateup(p);
69| 33.3k| rotateup(x);
70| 44.8k| } else {
71| 44.8k| rotateup(x);
72| 44.8k| rotateup(x);
73| 44.8k| }
74| 78.2k| }
75| 163k| }
76| 239k|}
77| |
78| 42.9k|def access(int x) -> void {
79| 42.9k| splay(x);
80| 42.9k| node[node[x].ch[1]].k = -1;
81| 42.9k| node[x].ch[1] = 0;
82| 231k| while (int p = node[x].p) {
------------------
| Branch (82:14): [True: 81.40%, False: 18.60%]
------------------
83| 188k| splay(p);
84| 188k| node[node[p].ch[1]].k = -1;
85| 188k| node[p].ch[1] = x;
86| 188k| node[x].k = 1;
87| 188k| rotateup(x);
88| 188k| }
89| 42.9k|}
90| |
91| |int head[N];
92| |struct {
93| | int to;
94| | int next;
95| |} edge[N * 2];
96| |
97| 167k|def build(int u, int p) -> void {
98| 501k| for (int e = head[u]; e; e = edge[e].next) {
^334k
------------------
| Branch (98:25): [True: 66.67%, False: 33.33%]
------------------
99| 334k| int v = edge[e].to;
100| 334k| if (v != p) {
------------------
| Branch (100:9): [True: 50.00%, False: 50.00%]
------------------
101| 167k| build(v, u);
102| 167k| node[v + 1].p = u + 1;
103| 167k| }
104| 334k| }
105| 167k|}
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| 167k| for (int i = 1; i <= n; ++i)
^167k
------------------
| Branch (117:19): [True: 100.00%, False: 0.00%]
------------------
118| 167k| node[i] = {.k = -1}, node[i].sum = node[i].val = rd.uw();
119| 167k| for (int i = 1; i != n; ++i) {
^167k
------------------
| Branch (119:19): [True: 100.00%, False: 0.00%]
------------------
120| 167k| int u = rd.uh();
121| 167k| int v = rd.uh();
122| 167k| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
123| 167k| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
124| 167k| }
125| 1| build(0, 0);
126| 25.8k| while (q--) {
------------------
| Branch (126:10): [True: 100.00%, False: 0.00%]
------------------
127| 25.8k| let t = rd.u1();
128| 25.8k| if (t == 0) {
------------------
| Branch (128:9): [True: 33.05%, False: 66.95%]
------------------
129| 8.53k| int u = rd.uh() + 1;
130| 8.53k| int v = rd.uh() + 1;
131| 8.53k| access(u);
132| 8.53k| reverse(u);
133| 8.53k| access(v);
134| 8.53k| node[node[v].ch[0]].p = 0;
135| 8.53k| node[node[v].ch[0]].k = -1;
136| 8.53k| node[v].ch[0] = 0;
137| 8.53k| int w = rd.uh() + 1;
138| 8.53k| int x = rd.uh() + 1;
139| 8.53k| access(w);
140| 8.53k| reverse(w);
141| 8.53k| node[w].p = x;
142| 8.53k| }
143| 25.8k| if (t == 1) {
------------------
| Branch (143:9): [True: 33.35%, False: 66.65%]
------------------
144| 8.61k| int u = rd.uh() + 1;
145| 8.61k| u32 x = rd.uw();
146| 8.61k| splay(u);
147| 8.61k| node[u].val += x;
148| 8.61k| node[u].sum += x;
149| 8.61k| }
150| 25.8k| if (t == 2) {
------------------
| Branch (150:9): [True: 33.60%, False: 66.40%]
------------------
151| 8.68k| int u = rd.uh() + 1;
152| 8.68k| int v = rd.uh() + 1;
153| 8.68k| access(u);
154| 8.68k| reverse(u);
155| 8.68k| access(v);
156| 8.68k| wt.ud(node[node[v].ch[0]].sum + node[v].val);
157| 8.68k| }
158| 25.8k| }
159| 1| return 0;
160| 1|}