#include <stdio.h>
#include <stdlib.h>
#define N 300000
#define LN 19 /* LN = ceil(log2(N)) */
#define N_ (1 << LN)
#define N1 (N * ((LN + 1) * 2 + 4))
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }
int n;
int aa[N], cc[N];
int *ej[N], eo[N];
void append(int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
int cc_[N_ * 2];
void build(int k, int l, int r) {
int m;
if (r - l == 1) {
cc_[k] = cc[l];
return;
}
m = (l + r) / 2;
build(k << 1 | 0, l, m), build(k << 1 | 1, m, r);
cc_[k] = min(cc_[k << 1 | 0], cc_[k << 1 | 1]);
}
int ll[N1], rr[N1]; long long xx1[N1], xx2[N1], yy[N1], lz1[N1], lz2[N1]; int cc1[N1];
int node(int k, long long x) {
static int _ = 1;
xx1[_] = x, xx2[_] = -INF, yy[_] = INF, cc1[_] = cc_[k];
return _++;
}
void put1(int u, long long z) {
xx1[u] += z;
lz1[u] += z;
}
void put2(int u, long long z) {
xx1[u] += z;
if (xx2[u] != -INF)
xx2[u] += z;
if (yy[u] != INF)
yy[u] += z;
lz2[u] += z;
}
void pus(int u) {
int l, r;
long long z;
l = ll[u], r = rr[u];
if (z = lz1[u]) {
if (xx1[l] > xx1[r])
put1(l, z);
else if (xx1[l] < xx1[r])
put1(r, z);
else
put1(l, z), put1(r, z);
lz1[u] = 0;
}
if (z = lz2[u]) {
put2(l, z), put2(r, z);
lz2[u] = 0;
}
}
void pul(int u) {
int l = ll[u], r = rr[u];
if (xx1[l] > xx1[r]) {
xx1[u] = xx1[l], xx2[u] = max(xx2[l], xx1[r]);
yy[u] = min(yy[l], min(xx1[r] + cc1[r], yy[r]));
cc1[u] = cc1[l];
} else if (xx1[l] < xx1[r]) {
xx1[u] = xx1[r], xx2[u] = max(xx1[l], xx2[r]);
yy[u] = min(min(xx1[l] + cc1[l], yy[l]), yy[r]);
cc1[u] = cc1[r];
} else {
xx1[u] = xx1[l], xx2[u] = max(xx2[l], xx2[r]);
yy[u] = min(yy[l], yy[r]);
cc1[u] = min(cc1[l], cc1[r]);
}
if (xx2[u] == -INF)
ll[u] = rr[u] = 0;
}
int merge(int u, int v) {
if (xx2[u] == -INF) {
put2(v, xx1[u]);
return v;
} else if (xx2[v] == -INF) {
put2(u, xx1[v]);
return u;
} else {
pus(u), pus(v);
ll[u] = merge(ll[u], ll[v]), rr[u] = merge(rr[u], rr[v]);
pul(u);
return u;
}
}
int cut_(int k, int l, int r, int ql, int qr, long long x) {
int u, m;
if (ql <= l && r <= qr)
return node(k, x);
if (qr <= l || r <= ql)
return node(k, INF);
u = node(k, INF);
m = (l + r) / 2;
ll[u] = cut_(k << 1 | 0, l, m, ql, qr, x), rr[u] = cut_(k << 1 | 1, m, r, ql, qr, x);
pul(u);
return u;
}
int cut(int u, int k, int l, int r, int ql, int qr) {
int m;
if (ql <= l && r <= qr)
return u;
if (qr <= l || r <= ql)
return node(k, INF);
if (xx2[u] == -INF)
return cut_(k, l, r, ql, qr, xx1[u]);
m = (l + r) / 2;
pus(u);
ll[u] = cut(ll[u], k << 1 | 0, l, m, ql, qr), rr[u] = cut(rr[u], k << 1 | 1, m, r, ql, qr);
pul(u);
return u;
}
void update(int u, int l, int r, long long x) {
int m;
if (xx1[u] <= x)
return;
if (xx2[u] < x) {
put1(u, x - xx1[u]);
return;
}
m = (l + r) / 2;
pus(u);
update(ll[u], l, m, x), update(rr[u], m, r, x);
pul(u);
}
int tt[N]; long long dp[N];
void dfs(int p, int i) {
int o;
tt[i] = node(1, 0);
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs(i, j);
tt[i] = merge(tt[i], tt[j]);
}
}
tt[i] = cut(tt[i], 1, 0, n, aa[i], n);
dp[i] = min(xx1[tt[i]] + cc1[tt[i]], yy[tt[i]]);
if (p != -1 && aa[p] != aa[i] - 1) {
if (aa[i] == 0)
tt[i] = cut(tt[i], 1, 0, n, aa[p], aa[p] + 1);
else
tt[i] = node(1, INF);
}
update(tt[i], 0, n, dp[i]);
}
int main() {
int h, i, j;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
for (i = 0; i < n; i++)
scanf("%d", &cc[i]);
build(1, 0, n);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (h = 0; h < n - 1; h++) {
scanf("%d%d", &i, &j), i--, j--;
append(i, j), append(j, i);
}
dfs(-1, 0);
printf("%lld\n", dp[0]);
return 0;
}
Compilation message
code1.c: In function 'append':
code1.c:20:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
20 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
code1.c: In function 'pus':
code1.c:67:6: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
67 | if (z = lz1[u]) {
| ^
code1.c:76:6: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
76 | if (z = lz2[u]) {
| ^
code1.c: In function 'main':
code1.c:190:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
190 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
code1.c:192:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
192 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
code1.c:194:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
194 | scanf("%d", &cc[i]);
| ^~~~~~~~~~~~~~~~~~~
code1.c:199:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
199 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2476 KB |
Output is correct |
2 |
Correct |
4 ms |
2516 KB |
Output is correct |
3 |
Correct |
5 ms |
5460 KB |
Output is correct |
4 |
Correct |
4 ms |
2576 KB |
Output is correct |
5 |
Correct |
4 ms |
2760 KB |
Output is correct |
6 |
Correct |
5 ms |
3412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
271 ms |
252804 KB |
Output is correct |
2 |
Correct |
290 ms |
155748 KB |
Output is correct |
3 |
Correct |
346 ms |
282560 KB |
Output is correct |
4 |
Correct |
442 ms |
474912 KB |
Output is correct |
5 |
Correct |
294 ms |
159572 KB |
Output is correct |
6 |
Correct |
272 ms |
175852 KB |
Output is correct |
7 |
Correct |
269 ms |
167628 KB |
Output is correct |
8 |
Correct |
273 ms |
188216 KB |
Output is correct |
9 |
Correct |
296 ms |
244392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2476 KB |
Output is correct |
2 |
Correct |
4 ms |
2516 KB |
Output is correct |
3 |
Correct |
5 ms |
5460 KB |
Output is correct |
4 |
Correct |
4 ms |
2576 KB |
Output is correct |
5 |
Correct |
4 ms |
2760 KB |
Output is correct |
6 |
Correct |
5 ms |
3412 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
6 ms |
4804 KB |
Output is correct |
15 |
Correct |
5 ms |
4820 KB |
Output is correct |
16 |
Correct |
6 ms |
4948 KB |
Output is correct |
17 |
Correct |
5 ms |
4780 KB |
Output is correct |
18 |
Correct |
5 ms |
4948 KB |
Output is correct |
19 |
Correct |
5 ms |
5160 KB |
Output is correct |
20 |
Correct |
5 ms |
5884 KB |
Output is correct |
21 |
Correct |
5 ms |
5972 KB |
Output is correct |
22 |
Correct |
5 ms |
5844 KB |
Output is correct |
23 |
Correct |
7 ms |
5964 KB |
Output is correct |
24 |
Correct |
6 ms |
5884 KB |
Output is correct |
25 |
Correct |
6 ms |
5716 KB |
Output is correct |
26 |
Correct |
5 ms |
5844 KB |
Output is correct |
27 |
Correct |
6 ms |
5716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2476 KB |
Output is correct |
2 |
Correct |
4 ms |
2516 KB |
Output is correct |
3 |
Correct |
5 ms |
5460 KB |
Output is correct |
4 |
Correct |
4 ms |
2576 KB |
Output is correct |
5 |
Correct |
4 ms |
2760 KB |
Output is correct |
6 |
Correct |
5 ms |
3412 KB |
Output is correct |
7 |
Correct |
271 ms |
252804 KB |
Output is correct |
8 |
Correct |
290 ms |
155748 KB |
Output is correct |
9 |
Correct |
346 ms |
282560 KB |
Output is correct |
10 |
Correct |
442 ms |
474912 KB |
Output is correct |
11 |
Correct |
294 ms |
159572 KB |
Output is correct |
12 |
Correct |
272 ms |
175852 KB |
Output is correct |
13 |
Correct |
269 ms |
167628 KB |
Output is correct |
14 |
Correct |
273 ms |
188216 KB |
Output is correct |
15 |
Correct |
296 ms |
244392 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
6 ms |
4804 KB |
Output is correct |
24 |
Correct |
5 ms |
4820 KB |
Output is correct |
25 |
Correct |
6 ms |
4948 KB |
Output is correct |
26 |
Correct |
5 ms |
4780 KB |
Output is correct |
27 |
Correct |
5 ms |
4948 KB |
Output is correct |
28 |
Correct |
5 ms |
5160 KB |
Output is correct |
29 |
Correct |
5 ms |
5884 KB |
Output is correct |
30 |
Correct |
5 ms |
5972 KB |
Output is correct |
31 |
Correct |
5 ms |
5844 KB |
Output is correct |
32 |
Correct |
7 ms |
5964 KB |
Output is correct |
33 |
Correct |
6 ms |
5884 KB |
Output is correct |
34 |
Correct |
6 ms |
5716 KB |
Output is correct |
35 |
Correct |
5 ms |
5844 KB |
Output is correct |
36 |
Correct |
6 ms |
5716 KB |
Output is correct |
37 |
Correct |
522 ms |
466604 KB |
Output is correct |
38 |
Correct |
524 ms |
358256 KB |
Output is correct |
39 |
Correct |
519 ms |
377752 KB |
Output is correct |
40 |
Correct |
536 ms |
404244 KB |
Output is correct |
41 |
Correct |
512 ms |
428940 KB |
Output is correct |
42 |
Correct |
502 ms |
394012 KB |
Output is correct |
43 |
Correct |
501 ms |
395224 KB |
Output is correct |
44 |
Correct |
508 ms |
395976 KB |
Output is correct |
45 |
Correct |
515 ms |
397644 KB |
Output is correct |
46 |
Correct |
472 ms |
470792 KB |
Output is correct |
47 |
Correct |
566 ms |
476044 KB |
Output is correct |
48 |
Correct |
470 ms |
469464 KB |
Output is correct |
49 |
Correct |
490 ms |
474844 KB |
Output is correct |
50 |
Correct |
534 ms |
477372 KB |
Output is correct |
51 |
Correct |
517 ms |
463784 KB |
Output is correct |
52 |
Correct |
547 ms |
469576 KB |
Output is correct |
53 |
Correct |
607 ms |
466048 KB |
Output is correct |