#include <stdio.h>
#define N 300000
int ll[1 + N * 2], rr[1 + N * 2], hh[1 + N * 2]; long long kk[1 + N * 2];
int node(long long k) {
static int _ = 1;
kk[_] = k, ll[_] = rr[_] = 0;
return _++;
}
void pul(int u) {
int tmp;
if (hh[ll[u]] < hh[rr[u]])
tmp = ll[u], ll[u] = rr[u], rr[u] = tmp;
hh[u] = hh[rr[u]] + 1;
}
int merge(int u, int v) {
int tmp;
if (u == 0)
return v;
if (v == 0)
return u;
if (kk[u] < kk[v])
tmp = u, u = v, v = tmp;
rr[u] = merge(rr[u], v), pul(u);
return u;
}
int remove_first(int t) {
int t_ = merge(ll[t], rr[t]);
ll[t] = rr[t] = 0, pul(t);
return t_;
}
long long ans;
void finish(int t) {
if (t == 0)
return;
ans -= kk[t];
finish(ll[t]), finish(rr[t]);
}
int main() {
static int pp[N], cc[N], tt[N], cnt[N];
static long long dp[N];
int n, m, n_, i, t;
scanf("%d%d", &n, &m), n_ = n + m;
for (i = 1; i < n_; i++)
scanf("%d%d", &pp[i], &cc[i]), pp[i]--;
for (i = n_ - 1; i > 0; i--) {
dp[i] += cc[i];
if (i >= n)
tt[i] = merge(node(cc[i]), node(cc[i]));
else {
int u;
t = tt[i];
while (--cnt[i])
t = remove_first(t);
u = !rr[t] || ll[t] && kk[ll[t]] > kk[rr[t]] ? ll[t] : rr[t];
kk[t] += cc[i], kk[u] += cc[i];
tt[i] = t;
}
dp[pp[i]] += dp[i], tt[pp[i]] = merge(tt[pp[i]], tt[i]), cnt[pp[i]]++;
}
t = tt[0];
while (cnt[0]--)
t = remove_first(t);
ans = dp[0];
finish(t);
printf("%lld\n", ans);
return 0;
}
Compilation message
fireworks.c: In function 'main':
fireworks.c:69:24: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
69 | u = !rr[t] || ll[t] && kk[ll[t]] > kk[rr[t]] ? ll[t] : rr[t];
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fireworks.c:56:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
56 | scanf("%d%d", &n, &m), n_ = n + m;
| ^~~~~~~~~~~~~~~~~~~~~
fireworks.c:58:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
58 | scanf("%d%d", &pp[i], &cc[i]), pp[i]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
376 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
376 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
1 ms |
492 KB |
Output is correct |
34 |
Correct |
1 ms |
492 KB |
Output is correct |
35 |
Correct |
2 ms |
492 KB |
Output is correct |
36 |
Correct |
2 ms |
620 KB |
Output is correct |
37 |
Correct |
2 ms |
620 KB |
Output is correct |
38 |
Correct |
2 ms |
620 KB |
Output is correct |
39 |
Correct |
2 ms |
620 KB |
Output is correct |
40 |
Correct |
2 ms |
492 KB |
Output is correct |
41 |
Correct |
2 ms |
492 KB |
Output is correct |
42 |
Correct |
2 ms |
492 KB |
Output is correct |
43 |
Correct |
2 ms |
620 KB |
Output is correct |
44 |
Correct |
2 ms |
620 KB |
Output is correct |
45 |
Correct |
3 ms |
620 KB |
Output is correct |
46 |
Correct |
3 ms |
748 KB |
Output is correct |
47 |
Correct |
3 ms |
748 KB |
Output is correct |
48 |
Correct |
3 ms |
620 KB |
Output is correct |
49 |
Correct |
3 ms |
768 KB |
Output is correct |
50 |
Correct |
3 ms |
620 KB |
Output is correct |
51 |
Correct |
4 ms |
620 KB |
Output is correct |
52 |
Correct |
3 ms |
620 KB |
Output is correct |
53 |
Correct |
3 ms |
748 KB |
Output is correct |
54 |
Correct |
3 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
376 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
1 ms |
492 KB |
Output is correct |
34 |
Correct |
1 ms |
492 KB |
Output is correct |
35 |
Correct |
2 ms |
492 KB |
Output is correct |
36 |
Correct |
2 ms |
620 KB |
Output is correct |
37 |
Correct |
2 ms |
620 KB |
Output is correct |
38 |
Correct |
2 ms |
620 KB |
Output is correct |
39 |
Correct |
2 ms |
620 KB |
Output is correct |
40 |
Correct |
2 ms |
492 KB |
Output is correct |
41 |
Correct |
2 ms |
492 KB |
Output is correct |
42 |
Correct |
2 ms |
492 KB |
Output is correct |
43 |
Correct |
2 ms |
620 KB |
Output is correct |
44 |
Correct |
2 ms |
620 KB |
Output is correct |
45 |
Correct |
3 ms |
620 KB |
Output is correct |
46 |
Correct |
3 ms |
748 KB |
Output is correct |
47 |
Correct |
3 ms |
748 KB |
Output is correct |
48 |
Correct |
3 ms |
620 KB |
Output is correct |
49 |
Correct |
3 ms |
768 KB |
Output is correct |
50 |
Correct |
3 ms |
620 KB |
Output is correct |
51 |
Correct |
4 ms |
620 KB |
Output is correct |
52 |
Correct |
3 ms |
620 KB |
Output is correct |
53 |
Correct |
3 ms |
748 KB |
Output is correct |
54 |
Correct |
3 ms |
748 KB |
Output is correct |
55 |
Correct |
5 ms |
1004 KB |
Output is correct |
56 |
Correct |
17 ms |
2924 KB |
Output is correct |
57 |
Correct |
29 ms |
4716 KB |
Output is correct |
58 |
Correct |
37 ms |
5996 KB |
Output is correct |
59 |
Correct |
48 ms |
7788 KB |
Output is correct |
60 |
Correct |
61 ms |
9580 KB |
Output is correct |
61 |
Correct |
73 ms |
10988 KB |
Output is correct |
62 |
Correct |
77 ms |
12016 KB |
Output is correct |
63 |
Correct |
100 ms |
14316 KB |
Output is correct |
64 |
Correct |
116 ms |
15084 KB |
Output is correct |
65 |
Correct |
82 ms |
12224 KB |
Output is correct |
66 |
Correct |
83 ms |
12268 KB |
Output is correct |
67 |
Correct |
82 ms |
12416 KB |
Output is correct |
68 |
Correct |
97 ms |
17388 KB |
Output is correct |
69 |
Correct |
110 ms |
17516 KB |
Output is correct |
70 |
Correct |
142 ms |
17516 KB |
Output is correct |
71 |
Correct |
124 ms |
22124 KB |
Output is correct |
72 |
Correct |
156 ms |
22124 KB |
Output is correct |
73 |
Correct |
151 ms |
21868 KB |
Output is correct |
74 |
Correct |
145 ms |
21868 KB |
Output is correct |
75 |
Correct |
155 ms |
21612 KB |
Output is correct |
76 |
Correct |
155 ms |
21740 KB |
Output is correct |
77 |
Correct |
201 ms |
21612 KB |
Output is correct |
78 |
Correct |
199 ms |
21756 KB |
Output is correct |
79 |
Correct |
357 ms |
21484 KB |
Output is correct |