/tmp/solutions/build/vertex_add_path_sum-slow.cpp:
1| |#include <common.h>
2| |#include <toy/bit.h>
3| |prelude;
4| |
5| |namespace {
6| |
7| |constexpr int N = 5e5;
8| |
9| |int head[N];
10| |struct {
11| | int to;
12| | int next;
13| |} edge[N * 2];
14| |int p[N];
15| |int l[N];
16| |int r[N];
17| |int a[N];
18| |u64 b[N + 1];
19| |u64 c[N + 2];
20| |int id;
21| |
22| 532|void dfs(int u, int w) {
23| 532| p[u] = w;
24| 532| l[u] = ++id;
25| 532| c[id] += b[id] = a[u];
26| 1.59k| for (int e = head[u]; e; e = edge[e].next) {
^1.06k
------------------
| Branch (26:25): [True: 66.62%, False: 33.38%]
------------------
27| 1.06k| int v = edge[e].to;
28| 1.06k| if (v != w) {
------------------
| Branch (28:9): [True: 50.00%, False: 50.00%]
------------------
29| 531| dfs(v, u);
30| 531| }
31| 1.06k| }
32| 532| c[r[u] = id + 1] -= a[u];
33| 532|}
34| |
35| |} // namespace
36| |
37| 1|int main() {
38| 1| rd rd;
39| 1| wt wt;
40| 1| int n = rd.uh();
41| 1| int q = rd.uh();
42| 1|#ifdef LOCAL
43| 1| id = 0;
44| 1| std::memset(head, 0, 4 * n);
45| 1| std::memset(c, 0, 8 * n + 16);
46| 1|#endif
47| 533| for (int i = 0; i < n; ++i) a[i] = rd.uw();
^532 ^532
------------------
| Branch (47:19): [True: 99.81%, False: 0.19%]
------------------
48| 532| for (int i = 1; i < n; ++i) {
^531
------------------
| Branch (48:19): [True: 99.81%, False: 0.19%]
------------------
49| 531| int u = rd.uh();
50| 531| int v = rd.uh();
51| 531| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
52| 531| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
53| 531| }
54| 1| dfs(0, -1);
55| 1| u64 sum = 0;
56| 533| for (int i = 1; i <= n; ++i) c[i] = sum += c[i];
^532 ^532
------------------
| Branch (56:19): [True: 99.81%, False: 0.19%]
------------------
57| 533| for (int i = n; i >= 1; --i) c[i] -= c[i - (i & -i)];
^532 ^532
------------------
| Branch (57:19): [True: 99.81%, False: 0.19%]
------------------
58| 1| int val[n + 1];
59| 532| for (int i = 1; i < n; ++i) val[l[i]] = l[p[i]];
^531 ^531
------------------
| Branch (59:19): [True: 99.81%, False: 0.19%]
------------------
60| 1| ++n;
61| 1| int m = n / 64 + 1;
62| 1| int len = log(m) + 1;
63| 1| int table[len][m];
64| 1| int suf[n];
65| 1| int pre[n];
66| 534| for (int min, i = 0; i < n; ++i) {
^533
------------------
| Branch (66:24): [True: 99.81%, False: 0.19%]
------------------
67| 533| int id = i & 63;
68| 533| int value = pre[i] = val[i];
69| 533| suf[i] = min = id ? std::min(min, value) : value;
^524 ^9
------------------
| Branch (69:20): [True: 98.31%, False: 1.69%]
------------------
70| 533| if (id == 63) table[0][i / 64] = suf[i];
^8
------------------
| Branch (70:9): [True: 1.50%, False: 98.50%]
------------------
71| 533| }
72| 533| for (int min, i = n - 2; i >= 0; --i) {
^532
------------------
| Branch (72:28): [True: 99.81%, False: 0.19%]
------------------
73| 532| int id = ~i & 63;
74| 532| int value = pre[i];
75| 532| pre[i] = min = id ? std::min(min, value) : value;
^524 ^8
------------------
| Branch (75:20): [True: 98.50%, False: 1.50%]
------------------
76| 532| }
77| 4| for (int i = 1; i < len; ++i) {
^3
------------------
| Branch (77:19): [True: 75.00%, False: 25.00%]
------------------
78| 23| for (int j = 0, k = 1 << (i - 1); k < m; ++j, ++k) {
^20
------------------
| Branch (78:39): [True: 86.96%, False: 13.04%]
------------------
79| 20| table[i][j] = std::min(table[i - 1][j], table[i - 1][k]);
80| 20| }
81| 3| }
82| 814| while (q--) {
------------------
| Branch (82:10): [True: 99.88%, False: 0.12%]
------------------
83| 813| let t = rd.u1();
84| 813| if (t == 0) {
------------------
| Branch (84:9): [True: 49.94%, False: 50.06%]
------------------
85| 406| int k = rd.uh();
86| 406| int x = rd.uw();
87| 406| int u = l[k];
88| 406| int v = r[k];
89| 406| b[u] += x;
90| 2.56k| for (; u <= n; u += u & -u) c[u] += x;
^2.16k ^2.16k
------------------
| Branch (90:14): [True: 84.18%, False: 15.82%]
------------------
91| 2.47k| for (; v <= n; v += v & -v) c[v] -= x;
^2.07k ^2.07k
------------------
| Branch (91:14): [True: 83.62%, False: 16.38%]
------------------
92| 406| }
93| 813| if (t == 1) {
------------------
| Branch (93:9): [True: 50.06%, False: 49.94%]
------------------
94| 407| int u = l[rd.uh()];
95| 407| int v = l[rd.uh()];
96| 407| if (u == v) {
------------------
| Branch (96:11): [True: 0.49%, False: 99.51%]
------------------
97| 2| wt.ud(b[u]);
98| 2| continue;
99| 2| }
100| 405| int l = std::min(u, v) + 1;
101| 405| int r = std::max(u, v);
102| 405| int L = l / 64;
103| 405| int R = r / 64;
104| 405| int w;
105| 405| if (L < R - 1) {
------------------
| Branch (105:11): [True: 65.93%, False: 34.07%]
------------------
106| 267| int p = pre[l];
107| 267| int s = suf[r];
108| 267| w = std::min(p, s);
109| 267| int k = log(R - L - 1);
110| 267| int a = table[k][L + 1];
111| 267| int b = table[k][R - (1 << k)];
112| 267| int tmp = std::min(a, b);
113| 267| w = std::min(w, tmp);
114| 267| } else if (L == R - 1) {
^138^138
------------------
| Branch (114:18): [True: 65.22%, False: 34.78%]
------------------
115| 90| int p = pre[l];
116| 90| int s = suf[r];
117| 90| w = std::min(p, s);
118| 90| } else {
119| 48| w = val[l];
120| 1.01k| for (int i = l + 1; i <= r; ++i) w = std::min(w, val[i]);
^970 ^970
------------------
| Branch (120:29): [True: 95.28%, False: 4.72%]
------------------
121| 48| }
122| 405| u64 sum = b[w];
123| 405| --l;
124| 2.08k| for (; l; l -= l & -l) sum += c[l];
^1.68k ^1.68k
------------------
| Branch (124:14): [True: 80.59%, False: 19.41%]
------------------
125| 2.36k| for (; r; r -= r & -r) sum += c[r];
^1.95k ^1.95k
------------------
| Branch (125:14): [True: 82.86%, False: 17.14%]
------------------
126| 2.16k| for (; w; w -= w & -w) sum -= c[w] * 2;
^1.75k ^1.75k
------------------
| Branch (126:14): [True: 81.28%, False: 18.72%]
------------------
127| 405| wt.ud(sum);
128| 405| }
129| 813| }
130| 1| return 0;
131| 1|}