/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| 2.13k|def reverse(int x) -> void { node[x].rev ^= 1; }
18| |
19| 2.98k|def pushdown(int x) -> void {
20| 2.98k| if (node[x].rev) {
------------------
| Branch (20:7): [True: 32.21%, False: 67.79%]
------------------
21| 961| std::swap(node[x].ch[0], node[x].ch[1]);
22| 961| node[node[x].ch[0]].k = 0;
23| 961| node[node[x].ch[1]].k = 1;
24| 961| reverse(node[x].ch[0]);
25| 961| reverse(node[x].ch[1]);
26| 961| node[x].rev = 0;
27| 961| }
28| 2.98k|}
29| |
30| 1.81k|def maintain(int x) -> void {
31| 1.81k| node[x].sum = node[node[x].ch[0]].sum + node[x].val + node[node[x].ch[1]].sum;
32| 1.81k|}
33| |
34| 1.81k|def rotateup(int x) -> void {
35| 1.81k| int p = node[x].p;
36| 1.81k| int g = node[p].p;
37| 1.81k| int k = node[x].k;
38| 1.81k| int t = node[p].k;
39| 1.81k| node[node[x].ch[k ^ 1]].p = p;
40| 1.81k| node[node[x].ch[k ^ 1]].k = k;
41| 1.81k| node[p].ch[k] = node[x].ch[k ^ 1];
42| 1.81k| node[p].p = x;
43| 1.81k| node[p].k = k ^ 1;
44| 1.81k| node[x].ch[k ^ 1] = p;
45| 1.81k| node[x].p = g;
46| 1.81k| node[x].k = t;
47| 1.81k| if (t != -1) {
------------------
| Branch (47:7): [True: 23.69%, False: 76.31%]
------------------
48| 430| node[g].ch[t] = x;
49| 430| }
50| 1.81k| maintain(p);
51| 1.81k|}
52| |
53| 2.65k|def is_root(int x) -> bool { return node[x].k == -1; }
54| |
55| 1.21k|def splay(int x) -> void {
56| 1.21k| pushdown(x);
57| 1.93k| while (!is_root(x)) {
------------------
| Branch (57:10): [True: 37.17%, False: 62.83%]
------------------
58| 720| if (int p = node[x].p; is_root(p)) {
------------------
| Branch (58:28): [True: 54.58%, False: 45.42%]
------------------
59| 393| pushdown(p);
60| 393| pushdown(x);
61| 393| rotateup(x);
62| 393| } else {
63| 327| int g = node[p].p;
64| 327| pushdown(g);
65| 327| pushdown(p);
66| 327| pushdown(x);
67| 327| if (node[x].k == node[p].k) {
------------------
| Branch (67:11): [True: 31.80%, False: 68.20%]
------------------
68| 104| rotateup(p);
69| 104| rotateup(x);
70| 223| } else {
71| 223| rotateup(x);
72| 223| rotateup(x);
73| 223| }
74| 327| }
75| 720| }
76| 1.21k|}
77| |
78| 361|def access(int x) -> void {
79| 361| splay(x);
80| 361| node[node[x].ch[1]].k = -1;
81| 361| node[x].ch[1] = 0;
82| 1.12k| while (int p = node[x].p) {
------------------
| Branch (82:14): [True: 68.02%, False: 31.98%]
------------------
83| 768| splay(p);
84| 768| node[node[p].ch[1]].k = -1;
85| 768| node[p].ch[1] = x;
86| 768| node[x].k = 1;
87| 768| rotateup(x);
88| 768| }
89| 361|}
90| |
91| |int head[N];
92| |struct {
93| | int to;
94| | int next;
95| |} edge[N * 2];
96| |
97| 197|def build(int u, int p) -> void {
98| 589| for (int e = head[u]; e; e = edge[e].next) {
^392
------------------
| Branch (98:25): [True: 66.55%, False: 33.45%]
------------------
99| 392| int v = edge[e].to;
100| 392| if (v != p) {
------------------
| Branch (100:9): [True: 50.00%, False: 50.00%]
------------------
101| 196| build(v, u);
102| 196| node[v + 1].p = u + 1;
103| 196| }
104| 392| }
105| 197|}
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| 198| for (int i = 1; i <= n; ++i)
^197
------------------
| Branch (117:19): [True: 99.49%, False: 0.51%]
------------------
118| 197| node[i] = {.k = -1}, node[i].sum = node[i].val = rd.uw();
119| 197| for (int i = 1; i != n; ++i) {
^196
------------------
| Branch (119:19): [True: 99.49%, False: 0.51%]
------------------
120| 196| int u = rd.uh();
121| 196| int v = rd.uh();
122| 196| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
123| 196| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
124| 196| }
125| 1| build(0, 0);
126| 235| while (q--) {
------------------
| Branch (126:10): [True: 99.57%, False: 0.43%]
------------------
127| 234| let t = rd.u1();
128| 234| if (t == 0) {
------------------
| Branch (128:9): [True: 29.49%, False: 70.51%]
------------------
129| 69| int u = rd.uh() + 1;
130| 69| int v = rd.uh() + 1;
131| 69| access(u);
132| 69| reverse(u);
133| 69| access(v);
134| 69| node[node[v].ch[0]].p = 0;
135| 69| node[node[v].ch[0]].k = -1;
136| 69| node[v].ch[0] = 0;
137| 69| int w = rd.uh() + 1;
138| 69| int x = rd.uh() + 1;
139| 69| access(w);
140| 69| reverse(w);
141| 69| node[w].p = x;
142| 69| }
143| 234| if (t == 1) {
------------------
| Branch (143:9): [True: 37.61%, False: 62.39%]
------------------
144| 88| int u = rd.uh() + 1;
145| 88| u32 x = rd.uw();
146| 88| splay(u);
147| 88| node[u].val += x;
148| 88| node[u].sum += x;
149| 88| }
150| 234| if (t == 2) {
------------------
| Branch (150:9): [True: 32.91%, False: 67.09%]
------------------
151| 77| int u = rd.uh() + 1;
152| 77| int v = rd.uh() + 1;
153| 77| access(u);
154| 77| reverse(u);
155| 77| access(v);
156| 77| wt.ud(node[node[v].ch[0]].sum + node[v].val);
157| 77| }
158| 234| }
159| 1| return 0;
160| 1|}