/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| 88|void dfs(int u, int w) {
23| 88| p[u] = w;
24| 88| l[u] = ++id;
25| 88| c[id] += b[id] = a[u];
26| 262| for (int e = head[u]; e; e = edge[e].next) {
^174
------------------
| Branch (26:25): [True: 66.41%, False: 33.59%]
------------------
27| 174| int v = edge[e].to;
28| 174| if (v != w) {
------------------
| Branch (28:9): [True: 50.00%, False: 50.00%]
------------------
29| 87| dfs(v, u);
30| 87| }
31| 174| }
32| 88| c[r[u] = id + 1] -= a[u];
33| 88|}
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| 89| for (int i = 0; i < n; ++i) a[i] = rd.uw();
^88 ^88
------------------
| Branch (47:19): [True: 98.88%, False: 1.12%]
------------------
48| 88| for (int i = 1; i < n; ++i) {
^87
------------------
| Branch (48:19): [True: 98.86%, False: 1.14%]
------------------
49| 87| int u = rd.uh();
50| 87| int v = rd.uh();
51| 87| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
52| 87| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
53| 87| }
54| 1| dfs(0, -1);
55| 1| u64 sum = 0;
56| 89| for (int i = 1; i <= n; ++i) c[i] = sum += c[i];
^88 ^88
------------------
| Branch (56:19): [True: 98.88%, False: 1.12%]
------------------
57| 89| for (int i = n; i >= 1; --i) c[i] -= c[i - (i & -i)];
^88 ^88
------------------
| Branch (57:19): [True: 98.88%, False: 1.12%]
------------------
58| 1| int val[n + 1];
59| 88| for (int i = 1; i < n; ++i) val[l[i]] = l[p[i]];
^87 ^87
------------------
| Branch (59:19): [True: 98.86%, False: 1.14%]
------------------
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| 90| for (int min, i = 0; i < n; ++i) {
^89
------------------
| Branch (66:24): [True: 98.89%, False: 1.11%]
------------------
67| 89| int id = i & 63;
68| 89| int value = pre[i] = val[i];
69| 89| suf[i] = min = id ? std::min(min, value) : value;
^87 ^2
------------------
| Branch (69:20): [True: 97.75%, False: 2.25%]
------------------
70| 89| if (id == 63) table[0][i / 64] = suf[i];
^1
------------------
| Branch (70:9): [True: 1.12%, False: 98.88%]
------------------
71| 89| }
72| 89| for (int min, i = n - 2; i >= 0; --i) {
^88
------------------
| Branch (72:28): [True: 98.88%, False: 1.12%]
------------------
73| 88| int id = ~i & 63;
74| 88| int value = pre[i];
75| 88| pre[i] = min = id ? std::min(min, value) : value;
^87 ^1
------------------
| Branch (75:20): [True: 98.86%, False: 1.14%]
------------------
76| 88| }
77| 2| for (int i = 1; i < len; ++i) {
^1
------------------
| Branch (77:19): [True: 50.00%, False: 50.00%]
------------------
78| 2| for (int j = 0, k = 1 << (i - 1); k < m; ++j, ++k) {
^1
------------------
| Branch (78:39): [True: 50.00%, False: 50.00%]
------------------
79| 1| table[i][j] = std::min(table[i - 1][j], table[i - 1][k]);
80| 1| }
81| 1| }
82| 396| while (q--) {
------------------
| Branch (82:10): [True: 99.75%, False: 0.25%]
------------------
83| 395| let t = rd.u1();
84| 395| if (t == 0) {
------------------
| Branch (84:9): [True: 51.90%, False: 48.10%]
------------------
85| 205| int k = rd.uh();
86| 205| int x = rd.uw();
87| 205| int u = l[k];
88| 205| int v = r[k];
89| 205| b[u] += x;
90| 952| for (; u <= n; u += u & -u) c[u] += x;
^747 ^747
------------------
| Branch (90:14): [True: 78.47%, False: 21.53%]
------------------
91| 908| for (; v <= n; v += v & -v) c[v] -= x;
^703 ^703
------------------
| Branch (91:14): [True: 77.42%, False: 22.58%]
------------------
92| 205| }
93| 395| if (t == 1) {
------------------
| Branch (93:9): [True: 48.10%, False: 51.90%]
------------------
94| 190| int u = l[rd.uh()];
95| 190| int v = l[rd.uh()];
96| 190| if (u == v) {
------------------
| Branch (96:11): [True: 0.53%, False: 99.47%]
------------------
97| 1| wt.ud(b[u]);
98| 1| continue;
99| 1| }
100| 189| int l = std::min(u, v) + 1;
101| 189| int r = std::max(u, v);
102| 189| int L = l / 64;
103| 189| int R = r / 64;
104| 189| int w;
105| 189| if (L < R - 1) {
------------------
| Branch (105:11): [True: 0.00%, False: 100.00%]
------------------
106| 0| int p = pre[l];
107| 0| int s = suf[r];
108| 0| w = std::min(p, s);
109| 0| int k = log(R - L - 1);
110| 0| int a = table[k][L + 1];
111| 0| int b = table[k][R - (1 << k)];
112| 0| int tmp = std::min(a, b);
113| 0| w = std::min(w, tmp);
114| 189| } else if (L == R - 1) {
------------------
| Branch (114:18): [True: 47.09%, False: 52.91%]
------------------
115| 89| int p = pre[l];
116| 89| int s = suf[r];
117| 89| w = std::min(p, s);
118| 100| } else {
119| 100| w = val[l];
120| 1.84k| for (int i = l + 1; i <= r; ++i) w = std::min(w, val[i]);
^1.74k^1.74k
------------------
| Branch (120:29): [True: 94.57%, False: 5.43%]
------------------
121| 100| }
122| 189| u64 sum = b[w];
123| 189| --l;
124| 710| for (; l; l -= l & -l) sum += c[l];
^521 ^521
------------------
| Branch (124:14): [True: 73.38%, False: 26.62%]
------------------
125| 809| for (; r; r -= r & -r) sum += c[r];
^620 ^620
------------------
| Branch (125:14): [True: 76.64%, False: 23.36%]
------------------
126| 709| for (; w; w -= w & -w) sum -= c[w] * 2;
^520 ^520
------------------
| Branch (126:14): [True: 73.34%, False: 26.66%]
------------------
127| 189| wt.ud(sum);
128| 189| }
129| 395| }
130| 1| return 0;
131| 1|}