/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| 3.83k|def reverse(int x) -> void { node[x].rev ^= 1; }
18| |
19| 4.79k|def pushdown(int x) -> void {
20| 4.79k| if (node[x].rev) {
------------------
| Branch (20:7): [True: 35.71%, False: 64.29%]
------------------
21| 1.71k| std::swap(node[x].ch[0], node[x].ch[1]);
22| 1.71k| node[node[x].ch[0]].k = 0;
23| 1.71k| node[node[x].ch[1]].k = 1;
24| 1.71k| reverse(node[x].ch[0]);
25| 1.71k| reverse(node[x].ch[1]);
26| 1.71k| node[x].rev = 0;
27| 1.71k| }
28| 4.79k|}
29| |
30| 2.83k|def maintain(int x) -> void {
31| 2.83k| node[x].sum = node[node[x].ch[0]].sum + node[x].val + node[node[x].ch[1]].sum;
32| 2.83k|}
33| |
34| 2.83k|def rotateup(int x) -> void {
35| 2.83k| int p = node[x].p;
36| 2.83k| int g = node[p].p;
37| 2.83k| int k = node[x].k;
38| 2.83k| int t = node[p].k;
39| 2.83k| node[node[x].ch[k ^ 1]].p = p;
40| 2.83k| node[node[x].ch[k ^ 1]].k = k;
41| 2.83k| node[p].ch[k] = node[x].ch[k ^ 1];
42| 2.83k| node[p].p = x;
43| 2.83k| node[p].k = k ^ 1;
44| 2.83k| node[x].ch[k ^ 1] = p;
45| 2.83k| node[x].p = g;
46| 2.83k| node[x].k = t;
47| 2.83k| if (t != -1) {
------------------
| Branch (47:7): [True: 23.10%, False: 76.90%]
------------------
48| 655| node[g].ch[t] = x;
49| 655| }
50| 2.83k| maintain(p);
51| 2.83k|}
52| |
53| 4.29k|def is_root(int x) -> bool { return node[x].k == -1; }
54| |
55| 1.98k|def splay(int x) -> void {
56| 1.98k| pushdown(x);
57| 3.14k| while (!is_root(x)) {
------------------
| Branch (57:10): [True: 36.75%, False: 63.25%]
------------------
58| 1.15k| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (58:28): [True: 56.85%, False: 43.15%]
------------------
59| 656| pushdown(p);
60| 656| pushdown(x);
61| 656| rotateup(x);
62| 656| } else {
63| 498| int g = node[p].p;
64| 498| pushdown(g);
65| 498| pushdown(p);
66| 498| pushdown(x);
67| 498| if (node[x].k == node[p].k) {
------------------
| Branch (67:11): [True: 33.53%, False: 66.47%]
------------------
68| 167| rotateup(p);
69| 167| rotateup(x);
70| 331| } else {
71| 331| rotateup(x);
72| 331| rotateup(x);
73| 331| }
74| 498| }
75| 1.15k| }
76| 1.98k|}
77| |
78| 682|def access(int x) -> void {
79| 682| splay(x);
80| 682| node[node[x].ch[1]].k = -1;
81| 682| node[x].ch[1] = 0;
82| 1.86k| while (int p = node[x].p) {
------------------
| Branch (82:14): [True: 63.45%, False: 36.55%]
------------------
83| 1.18k| splay(p);
84| 1.18k| node[node[p].ch[1]].k = -1;
85| 1.18k| node[p].ch[1] = x;
86| 1.18k| node[x].k = 1;
87| 1.18k| rotateup(x);
88| 1.18k| }
89| 682|}
90| |
91| |int head[N];
92| |struct {
93| | int to;
94| | int next;
95| |} edge[N * 2];
96| |
97| 87|def build(int u, int p) -> void {
98| 259| for (int e = head[u]; e; e = edge[e].next) {
^172
------------------
| Branch (98:25): [True: 66.41%, False: 33.59%]
------------------
99| 172| int v = edge[e].to;
100| 172| if (v != p) {
------------------
| Branch (100:9): [True: 50.00%, False: 50.00%]
------------------
101| 86| build(v, u);
102| 86| node[v + 1].p = u + 1;
103| 86| }
104| 172| }
105| 87|}
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| 88| for (int i = 1; i <= n; ++i)
^87
------------------
| Branch (117:19): [True: 98.86%, False: 1.14%]
------------------
118| 87| node[i] = {.k = -1}, node[i].sum = node[i].val = rd.uw();
119| 87| for (int i = 1; i != n; ++i) {
^86
------------------
| Branch (119:19): [True: 98.85%, False: 1.15%]
------------------
120| 86| int u = rd.uh();
121| 86| int v = rd.uh();
122| 86| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
123| 86| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
124| 86| }
125| 1| build(0, 0);
126| 395| while (q--) {
------------------
| Branch (126:10): [True: 99.75%, False: 0.25%]
------------------
127| 394| let t = rd.u1();
128| 394| if (t == 0) {
------------------
| Branch (128:9): [True: 34.01%, False: 65.99%]
------------------
129| 134| int u = rd.uh() + 1;
130| 134| int v = rd.uh() + 1;
131| 134| access(u);
132| 134| reverse(u);
133| 134| access(v);
134| 134| node[node[v].ch[0]].p = 0;
135| 134| node[node[v].ch[0]].k = -1;
136| 134| node[v].ch[0] = 0;
137| 134| int w = rd.uh() + 1;
138| 134| int x = rd.uh() + 1;
139| 134| access(w);
140| 134| reverse(w);
141| 134| node[w].p = x;
142| 134| }
143| 394| if (t == 1) {
------------------
| Branch (143:9): [True: 30.46%, False: 69.54%]
------------------
144| 120| int u = rd.uh() + 1;
145| 120| u32 x = rd.uw();
146| 120| splay(u);
147| 120| node[u].val += x;
148| 120| node[u].sum += x;
149| 120| }
150| 394| if (t == 2) {
------------------
| Branch (150:9): [True: 35.53%, False: 64.47%]
------------------
151| 140| int u = rd.uh() + 1;
152| 140| int v = rd.uh() + 1;
153| 140| access(u);
154| 140| reverse(u);
155| 140| access(v);
156| 140| wt.ud(node[node[v].ch[0]].sum + node[v].val);
157| 140| }
158| 394| }
159| 1| return 0;
160| 1|}