//writer:Oier_szc
#include <bits/stdc++.h>
#define ED cerr<<endl;
#define TS cerr<<"I AK IOI"<<endl;
#define cr(x) cerr<<x<<endl;
#define cr2(x,y) cerr<<x<<" "<<y<<endl;
#define cr3(x,y,z) cerr<<x<<" "<<y<<" "<<z<<endl;
#define cr4(x,y,z,w) cerr<<x<<" "<<y<<" "<<z<<" "<<w<<endl;
#define pii pair<int,int>
#define mk make_pair
#define fi first
#define se second
#define ll long long
using namespace std;
const int N=3e5+5,INF=2e9,mod=1e9+7;
int n,m;
int fa[N],c[N],deg[N];
int rt[N],idx=0;
ll ans=0;
struct tree {
int l,r,dis;
ll v;
}tr[N<<1];
int merge(int x,int y) {
if(!x||!y) return x|y;
if(tr[x].v<tr[y].v) swap(x,y);
tr[x].r=merge(tr[x].r,y);
if(tr[tr[x].l].dis<tr[tr[x].r].dis) {
swap(tr[x].l,tr[x].r);
}
tr[x].dis=tr[tr[x].r].dis+1;
return x;
}
inline int pop(int x) {
return merge(tr[x].l,tr[x].r);
}
int main()
{
tr[0].dis=-1;
scanf("%d%d",&n,&m);
for(int i=2;i<=n+m;++i) {
scanf("%d%d",&fa[i],&c[i]);
++deg[fa[i]],ans+=c[i];
}
for(int i=n+m;i>1;--i) {
ll l=0,r=0;
if(i<=n) {
while(--deg[i]) rt[i]=pop(rt[i]);
r=tr[rt[i]].v,rt[i]=pop(rt[i]);
l=tr[rt[i]].v,rt[i]=pop(rt[i]);
}
tr[++idx].v=l+c[i],tr[++idx].v=r+c[i];
rt[i]=merge(rt[i],merge(idx-1,idx));
rt[fa[i]]=merge(rt[fa[i]],rt[i]);
}
while(deg[1]--) rt[1]=pop(rt[1]);
while(rt[1]) ans-=tr[rt[1]].v,rt[1]=pop(rt[1]);
printf("%lld",ans);
return 0;
}
/*
人类智慧。
首先套路的DP是设dp[i][j]表示子树i然后距离为j。
可以发现转移是和一个绝对函数做加法卷积然后再把
所有的儿子合并起来。
其中加法卷积这一操作又可以分解成整体上移,整体右移,
添加斜率为-1的直线,将右边斜率整体推平为-1四个操作。
考虑用slope trick进行操作。
由于j这一维规模极大所以考虑二次差分记录转折点。
这样问题就变成了堆的插入,删除,合并。上左偏树即可。
最后统计答案,显然f(0)等于边权之和,然后容易借转折点
推出函数值。
*/
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:45:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
fireworks.cpp:47:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d%d",&fa[i],&c[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6596 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6588 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6596 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6592 KB |
Output is correct |
30 |
Correct |
1 ms |
6488 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
6748 KB |
Output is correct |
35 |
Correct |
2 ms |
6748 KB |
Output is correct |
36 |
Correct |
3 ms |
8708 KB |
Output is correct |
37 |
Correct |
2 ms |
8796 KB |
Output is correct |
38 |
Correct |
3 ms |
8720 KB |
Output is correct |
39 |
Correct |
2 ms |
8792 KB |
Output is correct |
40 |
Correct |
2 ms |
8796 KB |
Output is correct |
41 |
Correct |
2 ms |
8912 KB |
Output is correct |
42 |
Correct |
2 ms |
8796 KB |
Output is correct |
43 |
Correct |
2 ms |
8796 KB |
Output is correct |
44 |
Correct |
2 ms |
8796 KB |
Output is correct |
45 |
Correct |
3 ms |
9052 KB |
Output is correct |
46 |
Correct |
3 ms |
8896 KB |
Output is correct |
47 |
Correct |
3 ms |
8796 KB |
Output is correct |
48 |
Correct |
3 ms |
8796 KB |
Output is correct |
49 |
Correct |
3 ms |
8796 KB |
Output is correct |
50 |
Correct |
3 ms |
8700 KB |
Output is correct |
51 |
Correct |
3 ms |
8652 KB |
Output is correct |
52 |
Correct |
3 ms |
8796 KB |
Output is correct |
53 |
Correct |
3 ms |
8884 KB |
Output is correct |
54 |
Correct |
3 ms |
8792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6596 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6592 KB |
Output is correct |
30 |
Correct |
1 ms |
6488 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
6748 KB |
Output is correct |
35 |
Correct |
2 ms |
6748 KB |
Output is correct |
36 |
Correct |
3 ms |
8708 KB |
Output is correct |
37 |
Correct |
2 ms |
8796 KB |
Output is correct |
38 |
Correct |
3 ms |
8720 KB |
Output is correct |
39 |
Correct |
2 ms |
8792 KB |
Output is correct |
40 |
Correct |
2 ms |
8796 KB |
Output is correct |
41 |
Correct |
2 ms |
8912 KB |
Output is correct |
42 |
Correct |
2 ms |
8796 KB |
Output is correct |
43 |
Correct |
2 ms |
8796 KB |
Output is correct |
44 |
Correct |
2 ms |
8796 KB |
Output is correct |
45 |
Correct |
3 ms |
9052 KB |
Output is correct |
46 |
Correct |
3 ms |
8896 KB |
Output is correct |
47 |
Correct |
3 ms |
8796 KB |
Output is correct |
48 |
Correct |
3 ms |
8796 KB |
Output is correct |
49 |
Correct |
3 ms |
8796 KB |
Output is correct |
50 |
Correct |
3 ms |
8700 KB |
Output is correct |
51 |
Correct |
3 ms |
8652 KB |
Output is correct |
52 |
Correct |
3 ms |
8796 KB |
Output is correct |
53 |
Correct |
3 ms |
8884 KB |
Output is correct |
54 |
Correct |
3 ms |
8792 KB |
Output is correct |
55 |
Correct |
4 ms |
8796 KB |
Output is correct |
56 |
Correct |
13 ms |
11576 KB |
Output is correct |
57 |
Correct |
21 ms |
12120 KB |
Output is correct |
58 |
Correct |
33 ms |
14424 KB |
Output is correct |
59 |
Correct |
35 ms |
16988 KB |
Output is correct |
60 |
Correct |
45 ms |
17708 KB |
Output is correct |
61 |
Correct |
53 ms |
20048 KB |
Output is correct |
62 |
Correct |
59 ms |
22444 KB |
Output is correct |
63 |
Correct |
75 ms |
23124 KB |
Output is correct |
64 |
Correct |
79 ms |
23380 KB |
Output is correct |
65 |
Correct |
51 ms |
23836 KB |
Output is correct |
66 |
Correct |
44 ms |
23984 KB |
Output is correct |
67 |
Correct |
46 ms |
23984 KB |
Output is correct |
68 |
Correct |
111 ms |
23892 KB |
Output is correct |
69 |
Correct |
94 ms |
23880 KB |
Output is correct |
70 |
Correct |
90 ms |
23892 KB |
Output is correct |
71 |
Correct |
139 ms |
23244 KB |
Output is correct |
72 |
Correct |
141 ms |
23380 KB |
Output is correct |
73 |
Correct |
158 ms |
23120 KB |
Output is correct |
74 |
Correct |
151 ms |
23088 KB |
Output is correct |
75 |
Correct |
162 ms |
22868 KB |
Output is correct |
76 |
Correct |
157 ms |
23044 KB |
Output is correct |
77 |
Correct |
162 ms |
23124 KB |
Output is correct |
78 |
Correct |
172 ms |
23112 KB |
Output is correct |
79 |
Correct |
194 ms |
22612 KB |
Output is correct |