#include <stdio.h>
#include <stdlib.h>
#define N 200000
#define LN 18 /* LN = ceil(log2(N)) */
#define N_ (N * (LN + 1) + 1)
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int aa[N], cc[N], n;
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (aa[ii[j]] == aa[i_])
j++;
else if (aa[ii[j]] < aa[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
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 ll[N_], rr[N_], _; long long xx[N_];
int add(int t, int l, int r, int i, int x) {
int t_ = t ? t : ++_, m;
xx[t_] += x;
if (r - l > 1) {
m = (l + r) / 2;
if (i < m)
ll[t_] = add(ll[t_], l, m, i, x);
else
rr[t_] = add(rr[t_], m, r, i, x);
}
return t_;
}
int x;
int remove_(int t, int l, int r, int i) {
int m;
if (l >= i || x == 0 || !t)
return t;
if (r <= i && x >= xx[t]) {
x -= xx[t];
return 0;
}
if (r - l == 1) {
xx[t] -= x, x = 0;
return t;
}
m = (l + r) / 2;
rr[t] = remove_(rr[t], m, r, i), ll[t] = remove_(ll[t], l, m, i);
xx[t] = xx[ll[t]] + xx[rr[t]];
return ll[t] || rr[t] ? t : 0;
}
int merge(int t1, int t2, int l, int r) {
int m;
if (!t1)
return t2;
else if (!t2)
return t1;
xx[t1] += xx[t2];
if (r - l > 1) {
m = (l + r) / 2;
ll[t1] = merge(ll[t1], ll[t2], l, m), rr[t1] = merge(rr[t1], rr[t2], m, r);
}
return t1;
}
int tt[N];
void dfs(int p, int i) {
int o;
tt[i] = 0;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs(i, j);
tt[i] = merge(tt[i], tt[j], 0, n);
}
}
x = cc[i];
tt[i] = add(remove_(tt[i], 0, n, aa[i]), 0, n, aa[i], cc[i]);
}
int main() {
static int pp[N], ii[N];
int i, a;
long long sum;
scanf("%d", &n);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
sum = 0;
for (i = 0; i < n; i++) {
scanf("%d%d%d", &pp[i], &aa[i], &cc[i]), pp[i]--;
sum += cc[i];
if (i > 0)
append(pp[i], i);
}
for (i = 0; i < n; i++)
ii[i] = i;
sort(ii, 0, n);
for (i = 0, a = 0; i < n; i++)
aa[ii[i]] = i + 1 == n || aa[ii[i + 1]] != aa[ii[i]] ? a++ : a;
dfs(-1, 0);
printf("%lld\n", sum - xx[tt[0]]);
return 0;
}
Compilation message
worst_reporter2.c: In function 'append':
worst_reporter2.c:40:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
40 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
worst_reporter2.c: In function 'main':
worst_reporter2.c:120:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
worst_reporter2.c:125:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
125 | scanf("%d%d%d", &pp[i], &aa[i], &cc[i]), pp[i]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
1712 KB |
Output is correct |
6 |
Correct |
4 ms |
1492 KB |
Output is correct |
7 |
Correct |
4 ms |
1364 KB |
Output is correct |
8 |
Correct |
5 ms |
1620 KB |
Output is correct |
9 |
Correct |
4 ms |
1492 KB |
Output is correct |
10 |
Correct |
5 ms |
1364 KB |
Output is correct |
11 |
Correct |
4 ms |
1220 KB |
Output is correct |
12 |
Correct |
5 ms |
1580 KB |
Output is correct |
13 |
Correct |
5 ms |
1108 KB |
Output is correct |
14 |
Correct |
6 ms |
1456 KB |
Output is correct |
15 |
Correct |
4 ms |
1072 KB |
Output is correct |
16 |
Correct |
5 ms |
1620 KB |
Output is correct |
17 |
Correct |
6 ms |
1364 KB |
Output is correct |
18 |
Correct |
4 ms |
1208 KB |
Output is correct |
19 |
Correct |
5 ms |
1544 KB |
Output is correct |
20 |
Correct |
4 ms |
1464 KB |
Output is correct |
21 |
Correct |
4 ms |
1364 KB |
Output is correct |
22 |
Correct |
5 ms |
1776 KB |
Output is correct |
23 |
Correct |
5 ms |
1784 KB |
Output is correct |
24 |
Correct |
7 ms |
1536 KB |
Output is correct |
25 |
Correct |
4 ms |
1492 KB |
Output is correct |
26 |
Correct |
5 ms |
2132 KB |
Output is correct |
27 |
Correct |
5 ms |
1620 KB |
Output is correct |
28 |
Correct |
5 ms |
1460 KB |
Output is correct |
29 |
Correct |
5 ms |
1328 KB |
Output is correct |
30 |
Correct |
4 ms |
1072 KB |
Output is correct |
31 |
Correct |
4 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
1712 KB |
Output is correct |
6 |
Correct |
4 ms |
1492 KB |
Output is correct |
7 |
Correct |
4 ms |
1364 KB |
Output is correct |
8 |
Correct |
5 ms |
1620 KB |
Output is correct |
9 |
Correct |
4 ms |
1492 KB |
Output is correct |
10 |
Correct |
5 ms |
1364 KB |
Output is correct |
11 |
Correct |
4 ms |
1220 KB |
Output is correct |
12 |
Correct |
5 ms |
1580 KB |
Output is correct |
13 |
Correct |
5 ms |
1108 KB |
Output is correct |
14 |
Correct |
6 ms |
1456 KB |
Output is correct |
15 |
Correct |
4 ms |
1072 KB |
Output is correct |
16 |
Correct |
5 ms |
1620 KB |
Output is correct |
17 |
Correct |
6 ms |
1364 KB |
Output is correct |
18 |
Correct |
4 ms |
1208 KB |
Output is correct |
19 |
Correct |
5 ms |
1544 KB |
Output is correct |
20 |
Correct |
4 ms |
1464 KB |
Output is correct |
21 |
Correct |
4 ms |
1364 KB |
Output is correct |
22 |
Correct |
5 ms |
1776 KB |
Output is correct |
23 |
Correct |
5 ms |
1784 KB |
Output is correct |
24 |
Correct |
7 ms |
1536 KB |
Output is correct |
25 |
Correct |
4 ms |
1492 KB |
Output is correct |
26 |
Correct |
5 ms |
2132 KB |
Output is correct |
27 |
Correct |
5 ms |
1620 KB |
Output is correct |
28 |
Correct |
5 ms |
1460 KB |
Output is correct |
29 |
Correct |
5 ms |
1328 KB |
Output is correct |
30 |
Correct |
4 ms |
1072 KB |
Output is correct |
31 |
Correct |
4 ms |
1108 KB |
Output is correct |
32 |
Correct |
5 ms |
1620 KB |
Output is correct |
33 |
Correct |
341 ms |
72156 KB |
Output is correct |
34 |
Correct |
267 ms |
61584 KB |
Output is correct |
35 |
Correct |
313 ms |
72180 KB |
Output is correct |
36 |
Correct |
267 ms |
61652 KB |
Output is correct |
37 |
Correct |
184 ms |
50772 KB |
Output is correct |
38 |
Correct |
176 ms |
46036 KB |
Output is correct |
39 |
Correct |
231 ms |
60672 KB |
Output is correct |
40 |
Correct |
177 ms |
38120 KB |
Output is correct |
41 |
Correct |
150 ms |
33620 KB |
Output is correct |
42 |
Correct |
230 ms |
54412 KB |
Output is correct |
43 |
Correct |
188 ms |
31344 KB |
Output is correct |
44 |
Correct |
323 ms |
70632 KB |
Output is correct |
45 |
Correct |
226 ms |
59248 KB |
Output is correct |
46 |
Correct |
131 ms |
45692 KB |
Output is correct |
47 |
Correct |
331 ms |
59228 KB |
Output is correct |
48 |
Correct |
183 ms |
54548 KB |
Output is correct |
49 |
Correct |
145 ms |
54404 KB |
Output is correct |
50 |
Correct |
359 ms |
75580 KB |
Output is correct |
51 |
Correct |
208 ms |
75560 KB |
Output is correct |
52 |
Correct |
349 ms |
58088 KB |
Output is correct |
53 |
Correct |
186 ms |
55112 KB |
Output is correct |
54 |
Correct |
188 ms |
92364 KB |
Output is correct |
55 |
Correct |
225 ms |
57708 KB |
Output is correct |
56 |
Correct |
187 ms |
48460 KB |
Output is correct |
57 |
Correct |
184 ms |
44584 KB |
Output is correct |
58 |
Correct |
203 ms |
31344 KB |
Output is correct |
59 |
Correct |
175 ms |
31312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
1712 KB |
Output is correct |
6 |
Correct |
4 ms |
1492 KB |
Output is correct |
7 |
Correct |
4 ms |
1364 KB |
Output is correct |
8 |
Correct |
5 ms |
1620 KB |
Output is correct |
9 |
Correct |
4 ms |
1492 KB |
Output is correct |
10 |
Correct |
5 ms |
1364 KB |
Output is correct |
11 |
Correct |
4 ms |
1220 KB |
Output is correct |
12 |
Correct |
5 ms |
1580 KB |
Output is correct |
13 |
Correct |
5 ms |
1108 KB |
Output is correct |
14 |
Correct |
6 ms |
1456 KB |
Output is correct |
15 |
Correct |
4 ms |
1072 KB |
Output is correct |
16 |
Correct |
5 ms |
1620 KB |
Output is correct |
17 |
Correct |
6 ms |
1364 KB |
Output is correct |
18 |
Correct |
4 ms |
1208 KB |
Output is correct |
19 |
Correct |
5 ms |
1544 KB |
Output is correct |
20 |
Correct |
4 ms |
1464 KB |
Output is correct |
21 |
Correct |
4 ms |
1364 KB |
Output is correct |
22 |
Correct |
5 ms |
1776 KB |
Output is correct |
23 |
Correct |
5 ms |
1784 KB |
Output is correct |
24 |
Correct |
7 ms |
1536 KB |
Output is correct |
25 |
Correct |
4 ms |
1492 KB |
Output is correct |
26 |
Correct |
5 ms |
2132 KB |
Output is correct |
27 |
Correct |
5 ms |
1620 KB |
Output is correct |
28 |
Correct |
5 ms |
1460 KB |
Output is correct |
29 |
Correct |
5 ms |
1328 KB |
Output is correct |
30 |
Correct |
4 ms |
1072 KB |
Output is correct |
31 |
Correct |
4 ms |
1108 KB |
Output is correct |
32 |
Correct |
5 ms |
1620 KB |
Output is correct |
33 |
Correct |
341 ms |
72156 KB |
Output is correct |
34 |
Correct |
267 ms |
61584 KB |
Output is correct |
35 |
Correct |
313 ms |
72180 KB |
Output is correct |
36 |
Correct |
267 ms |
61652 KB |
Output is correct |
37 |
Correct |
184 ms |
50772 KB |
Output is correct |
38 |
Correct |
176 ms |
46036 KB |
Output is correct |
39 |
Correct |
231 ms |
60672 KB |
Output is correct |
40 |
Correct |
177 ms |
38120 KB |
Output is correct |
41 |
Correct |
150 ms |
33620 KB |
Output is correct |
42 |
Correct |
230 ms |
54412 KB |
Output is correct |
43 |
Correct |
188 ms |
31344 KB |
Output is correct |
44 |
Correct |
323 ms |
70632 KB |
Output is correct |
45 |
Correct |
226 ms |
59248 KB |
Output is correct |
46 |
Correct |
131 ms |
45692 KB |
Output is correct |
47 |
Correct |
331 ms |
59228 KB |
Output is correct |
48 |
Correct |
183 ms |
54548 KB |
Output is correct |
49 |
Correct |
145 ms |
54404 KB |
Output is correct |
50 |
Correct |
359 ms |
75580 KB |
Output is correct |
51 |
Correct |
208 ms |
75560 KB |
Output is correct |
52 |
Correct |
349 ms |
58088 KB |
Output is correct |
53 |
Correct |
186 ms |
55112 KB |
Output is correct |
54 |
Correct |
188 ms |
92364 KB |
Output is correct |
55 |
Correct |
225 ms |
57708 KB |
Output is correct |
56 |
Correct |
187 ms |
48460 KB |
Output is correct |
57 |
Correct |
184 ms |
44584 KB |
Output is correct |
58 |
Correct |
203 ms |
31344 KB |
Output is correct |
59 |
Correct |
175 ms |
31312 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Incorrect |
0 ms |
296 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |