#include<stdio.h>
#define N 300001
#define DEFINE_LEFTIST(type, M, is_persistent) \
int hl_##type[M]; \
int hr_##type[M]; \
int hh_##type[M]; \
type ha_##type[M]; \
int halc_##type; \
int leftist_##type(type x) { int p = ++halc_##type; ha_##type[p]=x; hh_##type[p] = 1; hl_##type[p]=hr_##type[p]=0; return p; } \
int leftist_##type##_copy(int q) {\
if(!is_persistent)return q; \
if(!q)return 0;\
int p=++halc_##type; \
hl_##type[p]=hl_##type[q]; \
hr_##type[p]=hr_##type[q]; \
hh_##type[p]=hh_##type[q]; \
ha_##type[p]=ha_##type[q]; \
return p; \
} \
int leftist_##type##_merge(int l,int r) \
{ \
if(!l||!r) { return leftist_##type##_copy(l^r); } \
if(lt_##type(ha_##type[r],ha_##type[l])) return leftist_##type##_merge(r,l); \
l=leftist_##type##_copy(l); \
hr_##type[l]=leftist_##type##_merge(hr_##type[l],r); \
if(hh_##type[hr_##type[l]] > hh_##type[hl_##type[l]]) { int temp = hr_##type[l]; hr_##type[l] = hl_##type[l], hl_##type[l] = temp; } \
hh_##type[l] = hh_##type[hr_##type[l]] + 1; \
return l; \
} \
int leftist_##type##_push(int v,type x) { return leftist_##type##_merge(v, leftist_##type(x)); } \
type leftist_##type##_top(int v) { return ha_##type[v]; } \
int leftist_##type##_pop(int v) { return leftist_##type##_merge(hl_##type[v], hr_##type[v]); }
typedef long long i64;
int lt_i64(i64 a, i64 b){return a>b;}
DEFINE_LEFTIST(i64, 2000000, 0);
int n,M;
int e[N][3],eo,h[N],pq[N];
long long m[N],c[N];
void link(int u,int v,int w) { e[++eo][0]=v,e[eo][1]=w,e[eo][2]=h[u],h[u]=eo; }
void dfs(int u,int p,int ci)
{
m[u]=c[u]=0;
if(u > n)
{
pq[u]=leftist_i64_push(pq[u],ci);
pq[u]=leftist_i64_push(pq[u],ci);
m[u]=1;
c[u]=-ci;
return;
}
for(int v,j=h[u];j;j=e[j][2])if((v=e[j][0])-p)
{
dfs(v,u,e[j][1]);
pq[u]=leftist_i64_merge(pq[u],pq[v]);
m[u]+=m[v];
c[u]+=c[v];
}
if(u!=1)
{
while(m[u]>1)
{
--m[u];
c[u]+=leftist_i64_top(pq[u]);
pq[u]=leftist_i64_pop(pq[u]);
}
long long x=leftist_i64_top(pq[u]);
pq[u]=leftist_i64_pop(pq[u]);
long long x2=leftist_i64_top(pq[u]);
pq[u]=leftist_i64_pop(pq[u]);
pq[u]=leftist_i64_push(pq[u],x+ci);
pq[u]=leftist_i64_push(pq[u],x2+ci);
c[u]-=ci;
}
else
{
while(m[1]>0)
{
--m[1];
c[1]+=leftist_i64_top(pq[1]);
pq[1]=leftist_i64_pop(pq[1]);
}
printf("%lld",c[1]);
}
}
int main()
{
scanf("%d%d",&n,&M);
for(int p,c,i=2;i<=n+M;++i) scanf("%d%d",&p,&c),link(p,i,c);
dfs(1,1,-1);
}
Compilation message
fireworks.c: In function 'main':
fireworks.c:103:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | scanf("%d%d",&n,&M);
| ^~~~~~~~~~~~~~~~~~~
fireworks.c:104:33: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | for(int p,c,i=2;i<=n+M;++i) scanf("%d%d",&p,&c),link(p,i,c);
| ^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10584 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10584 KB |
Output is correct |
9 |
Correct |
1 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10584 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10700 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10584 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10584 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10584 KB |
Output is correct |
9 |
Correct |
1 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10700 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
2 ms |
10584 KB |
Output is correct |
26 |
Correct |
1 ms |
10588 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10672 KB |
Output is correct |
31 |
Correct |
2 ms |
10676 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
3 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10588 KB |
Output is correct |
35 |
Correct |
2 ms |
10844 KB |
Output is correct |
36 |
Correct |
2 ms |
10844 KB |
Output is correct |
37 |
Correct |
3 ms |
10844 KB |
Output is correct |
38 |
Correct |
3 ms |
10844 KB |
Output is correct |
39 |
Correct |
3 ms |
10840 KB |
Output is correct |
40 |
Correct |
4 ms |
11096 KB |
Output is correct |
41 |
Correct |
3 ms |
11100 KB |
Output is correct |
42 |
Correct |
3 ms |
10768 KB |
Output is correct |
43 |
Correct |
3 ms |
10844 KB |
Output is correct |
44 |
Correct |
3 ms |
10844 KB |
Output is correct |
45 |
Correct |
3 ms |
10844 KB |
Output is correct |
46 |
Correct |
4 ms |
10844 KB |
Output is correct |
47 |
Correct |
3 ms |
10844 KB |
Output is correct |
48 |
Correct |
3 ms |
10844 KB |
Output is correct |
49 |
Correct |
3 ms |
10844 KB |
Output is correct |
50 |
Correct |
3 ms |
10844 KB |
Output is correct |
51 |
Correct |
3 ms |
10840 KB |
Output is correct |
52 |
Correct |
3 ms |
10844 KB |
Output is correct |
53 |
Correct |
3 ms |
10840 KB |
Output is correct |
54 |
Correct |
3 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10584 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10584 KB |
Output is correct |
9 |
Correct |
1 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10700 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
2 ms |
10584 KB |
Output is correct |
26 |
Correct |
1 ms |
10588 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10672 KB |
Output is correct |
31 |
Correct |
2 ms |
10676 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
3 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10588 KB |
Output is correct |
35 |
Correct |
2 ms |
10844 KB |
Output is correct |
36 |
Correct |
2 ms |
10844 KB |
Output is correct |
37 |
Correct |
3 ms |
10844 KB |
Output is correct |
38 |
Correct |
3 ms |
10844 KB |
Output is correct |
39 |
Correct |
3 ms |
10840 KB |
Output is correct |
40 |
Correct |
4 ms |
11096 KB |
Output is correct |
41 |
Correct |
3 ms |
11100 KB |
Output is correct |
42 |
Correct |
3 ms |
10768 KB |
Output is correct |
43 |
Correct |
3 ms |
10844 KB |
Output is correct |
44 |
Correct |
3 ms |
10844 KB |
Output is correct |
45 |
Correct |
3 ms |
10844 KB |
Output is correct |
46 |
Correct |
4 ms |
10844 KB |
Output is correct |
47 |
Correct |
3 ms |
10844 KB |
Output is correct |
48 |
Correct |
3 ms |
10844 KB |
Output is correct |
49 |
Correct |
3 ms |
10844 KB |
Output is correct |
50 |
Correct |
3 ms |
10844 KB |
Output is correct |
51 |
Correct |
3 ms |
10840 KB |
Output is correct |
52 |
Correct |
3 ms |
10844 KB |
Output is correct |
53 |
Correct |
3 ms |
10840 KB |
Output is correct |
54 |
Correct |
3 ms |
10844 KB |
Output is correct |
55 |
Correct |
5 ms |
11100 KB |
Output is correct |
56 |
Correct |
13 ms |
16508 KB |
Output is correct |
57 |
Correct |
22 ms |
19592 KB |
Output is correct |
58 |
Correct |
27 ms |
20252 KB |
Output is correct |
59 |
Correct |
35 ms |
21472 KB |
Output is correct |
60 |
Correct |
49 ms |
23820 KB |
Output is correct |
61 |
Correct |
52 ms |
26192 KB |
Output is correct |
62 |
Correct |
58 ms |
28780 KB |
Output is correct |
63 |
Correct |
87 ms |
31828 KB |
Output is correct |
64 |
Correct |
85 ms |
32188 KB |
Output is correct |
65 |
Correct |
73 ms |
56496 KB |
Output is correct |
66 |
Correct |
67 ms |
56488 KB |
Output is correct |
67 |
Correct |
72 ms |
33128 KB |
Output is correct |
68 |
Correct |
72 ms |
44112 KB |
Output is correct |
69 |
Correct |
75 ms |
41944 KB |
Output is correct |
70 |
Correct |
79 ms |
42016 KB |
Output is correct |
71 |
Correct |
79 ms |
31384 KB |
Output is correct |
72 |
Correct |
75 ms |
31136 KB |
Output is correct |
73 |
Correct |
87 ms |
31000 KB |
Output is correct |
74 |
Correct |
91 ms |
31060 KB |
Output is correct |
75 |
Correct |
97 ms |
30848 KB |
Output is correct |
76 |
Correct |
95 ms |
30852 KB |
Output is correct |
77 |
Correct |
119 ms |
30984 KB |
Output is correct |
78 |
Correct |
122 ms |
30804 KB |
Output is correct |
79 |
Correct |
187 ms |
30540 KB |
Output is correct |