/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| 193|void dfs(int u, int w) {
23| 193| p[u] = w;
24| 193| l[u] = ++id;
25| 193| c[id] += b[id] = a[u];
26| 577| for (int e = head[u]; e; e = edge[e].next) {
^384
------------------
| Branch (26:25): [True: 66.55%, False: 33.45%]
------------------
27| 384| int v = edge[e].to;
28| 384| if (v != w) {
------------------
| Branch (28:9): [True: 50.00%, False: 50.00%]
------------------
29| 192| dfs(v, u);
30| 192| }
31| 384| }
32| 193| c[r[u] = id + 1] -= a[u];
33| 193|}
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| 194| for (int i = 0; i < n; ++i) a[i] = rd.uw();
^193 ^193
------------------
| Branch (47:19): [True: 99.48%, False: 0.52%]
------------------
48| 193| for (int i = 1; i < n; ++i) {
^192
------------------
| Branch (48:19): [True: 99.48%, False: 0.52%]
------------------
49| 192| int u = rd.uh();
50| 192| int v = rd.uh();
51| 192| edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
52| 192| edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
53| 192| }
54| 1| dfs(0, -1);
55| 1| u64 sum = 0;
56| 194| for (int i = 1; i <= n; ++i) c[i] = sum += c[i];
^193 ^193
------------------
| Branch (56:19): [True: 99.48%, False: 0.52%]
------------------
57| 194| for (int i = n; i >= 1; --i) c[i] -= c[i - (i & -i)];
^193 ^193
------------------
| Branch (57:19): [True: 99.48%, False: 0.52%]
------------------
58| 1| int val[n + 1];
59| 193| for (int i = 1; i < n; ++i) val[l[i]] = l[p[i]];
^192 ^192
------------------
| Branch (59:19): [True: 99.48%, False: 0.52%]
------------------
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| 195| for (int min, i = 0; i < n; ++i) {
^194
------------------
| Branch (66:24): [True: 99.49%, False: 0.51%]
------------------
67| 194| int id = i & 63;
68| 194| int value = pre[i] = val[i];
69| 194| suf[i] = min = id ? std::min(min, value) : value;
^190 ^4
------------------
| Branch (69:20): [True: 97.94%, False: 2.06%]
------------------
70| 194| if (id == 63) table[0][i / 64] = suf[i];
^3
------------------
| Branch (70:9): [True: 1.55%, False: 98.45%]
------------------
71| 194| }
72| 194| for (int min, i = n - 2; i >= 0; --i) {
^193
------------------
| Branch (72:28): [True: 99.48%, False: 0.52%]
------------------
73| 193| int id = ~i & 63;
74| 193| int value = pre[i];
75| 193| pre[i] = min = id ? std::min(min, value) : value;
^190 ^3
------------------
| Branch (75:20): [True: 98.45%, False: 1.55%]
------------------
76| 193| }
77| 3| for (int i = 1; i < len; ++i) {
^2
------------------
| Branch (77:19): [True: 66.67%, False: 33.33%]
------------------
78| 7| for (int j = 0, k = 1 << (i - 1); k < m; ++j, ++k) {
^5
------------------
| Branch (78:39): [True: 71.43%, False: 28.57%]
------------------
79| 5| table[i][j] = std::min(table[i - 1][j], table[i - 1][k]);
80| 5| }
81| 2| }
82| 240| while (q--) {
------------------
| Branch (82:10): [True: 99.58%, False: 0.42%]
------------------
83| 239| let t = rd.u1();
84| 239| if (t == 0) {
------------------
| Branch (84:9): [True: 54.81%, False: 45.19%]
------------------
85| 131| int k = rd.uh();
86| 131| int x = rd.uw();
87| 131| int u = l[k];
88| 131| int v = r[k];
89| 131| b[u] += x;
90| 698| for (; u <= n; u += u & -u) c[u] += x;
^567 ^567
------------------
| Branch (90:14): [True: 81.23%, False: 18.77%]
------------------
91| 616| for (; v <= n; v += v & -v) c[v] -= x;
^485 ^485
------------------
| Branch (91:14): [True: 78.73%, False: 21.27%]
------------------
92| 131| }
93| 239| if (t == 1) {
------------------
| Branch (93:9): [True: 45.19%, False: 54.81%]
------------------
94| 108| int u = l[rd.uh()];
95| 108| int v = l[rd.uh()];
96| 108| if (u == v) {
------------------
| Branch (96:11): [True: 1.85%, False: 98.15%]
------------------
97| 2| wt.ud(b[u]);
98| 2| continue;
99| 2| }
100| 106| int l = std::min(u, v) + 1;
101| 106| int r = std::max(u, v);
102| 106| int L = l / 64;
103| 106| int R = r / 64;
104| 106| int w;
105| 106| if (L < R - 1) {
------------------
| Branch (105:11): [True: 15.09%, False: 84.91%]
------------------
106| 16| int p = pre[l];
107| 16| int s = suf[r];
108| 16| w = std::min(p, s);
109| 16| int k = log(R - L - 1);
110| 16| int a = table[k][L + 1];
111| 16| int b = table[k][R - (1 << k)];
112| 16| int tmp = std::min(a, b);
113| 16| w = std::min(w, tmp);
114| 90| } else if (L == R - 1) {
------------------
| Branch (114:18): [True: 53.33%, False: 46.67%]
------------------
115| 48| int p = pre[l];
116| 48| int s = suf[r];
117| 48| w = std::min(p, s);
118| 48| } else {
119| 42| w = val[l];
120| 832| for (int i = l + 1; i <= r; ++i) w = std::min(w, val[i]);
^790 ^790
------------------
| Branch (120:29): [True: 94.95%, False: 5.05%]
------------------
121| 42| }
122| 106| u64 sum = b[w];
123| 106| --l;
124| 450| for (; l; l -= l & -l) sum += c[l];
^344 ^344
------------------
| Branch (124:14): [True: 76.44%, False: 23.56%]
------------------
125| 493| for (; r; r -= r & -r) sum += c[r];
^387 ^387
------------------
| Branch (125:14): [True: 78.50%, False: 21.50%]
------------------
126| 433| for (; w; w -= w & -w) sum -= c[w] * 2;
^327 ^327
------------------
| Branch (126:14): [True: 75.52%, False: 24.48%]
------------------
127| 106| wt.ud(sum);
128| 106| }
129| 239| }
130| 1| return 0;
131| 1|}