//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn=1e6+10;
struct node{
int ls,rs,dis;
ll val;
node(){}
node(ll val,int ls,int rs,int dis):val(val),ls(ls),rs(rs),dis(dis){}
}t[maxn];
int n,m;
vector <int> edge[maxn];
int c[maxn];
int cnt=0;
int merge(int x,int y) {
if (!x||!y) return x|y;
if (t[x].val<t[y].val) swap(x,y);
t[x].rs=merge(t[x].rs,y);
if (t[t[x].ls].dis<t[t[x].rs].dis) swap(t[x].ls,t[x].rs);
if (!t[x].rs) t[x].dis=0;
else t[x].dis=t[t[x].rs].dis+1;
return x;
}
int pop(int x) {
return merge(t[x].ls,t[x].rs);
}
int solve(int u) {
int rt=0;
for (int v:edge[u]) {
rt=merge(rt,solve(v));
}
ll l=0,r=0;
if (u==1) {
rep(i,0,SZ(edge[u])) rt=pop(rt);
return rt;
}
if (u<=n) {
rep(i,1,SZ(edge[u])) rt=pop(rt);
r=t[rt].val,rt=pop(rt);
l=t[rt].val,rt=pop(rt);
}
t[++cnt]=node(l+(ll)c[u],0,0,0),t[++cnt]=node(r+(ll)c[u],0,0,0);
rt=merge(rt,merge(cnt,cnt-1));
// debug(cnt);
// debug(u);
// cout<<u<<" "<<rt<<" "<<t[rt].val<<" "<<t[t[rt].rs].val<<endl;
return rt;
}
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie();
t[0].dis=0;
cin>>n>>m;
ll sum=0;
rep(i,2,n+m+1) {
int fa;
cin>>fa>>c[i];
edge[fa].pb(i);
sum+=c[i];
}
int rt=solve(1);
while (rt) sum-=t[rt].val,rt=pop(rt);
cout<<sum;
return 0;
}
Compilation message
fireworks.cpp: In constructor 'node::node(long long int, int, int, int)':
fireworks.cpp:28:5: warning: 'node::val' will be initialized after [-Wreorder]
28 | ll val;
| ^~~
fireworks.cpp:27:6: warning: 'int node::ls' [-Wreorder]
27 | int ls,rs,dis;
| ^~
fireworks.cpp:30:2: warning: when initialized here [-Wreorder]
30 | node(ll val,int ls,int rs,int dis):val(val),ls(ls),rs(rs),dis(dis){}
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23748 KB |
Output is correct |
3 |
Correct |
15 ms |
23760 KB |
Output is correct |
4 |
Correct |
15 ms |
23860 KB |
Output is correct |
5 |
Correct |
15 ms |
23756 KB |
Output is correct |
6 |
Correct |
15 ms |
23752 KB |
Output is correct |
7 |
Correct |
14 ms |
23756 KB |
Output is correct |
8 |
Correct |
14 ms |
23820 KB |
Output is correct |
9 |
Correct |
14 ms |
23780 KB |
Output is correct |
10 |
Correct |
14 ms |
23796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23708 KB |
Output is correct |
3 |
Correct |
14 ms |
23756 KB |
Output is correct |
4 |
Correct |
15 ms |
23820 KB |
Output is correct |
5 |
Correct |
15 ms |
23812 KB |
Output is correct |
6 |
Correct |
14 ms |
23756 KB |
Output is correct |
7 |
Correct |
15 ms |
23732 KB |
Output is correct |
8 |
Correct |
15 ms |
23756 KB |
Output is correct |
9 |
Correct |
15 ms |
23908 KB |
Output is correct |
10 |
Correct |
15 ms |
23740 KB |
Output is correct |
11 |
Correct |
14 ms |
23752 KB |
Output is correct |
12 |
Correct |
14 ms |
23776 KB |
Output is correct |
13 |
Correct |
16 ms |
23780 KB |
Output is correct |
14 |
Correct |
14 ms |
23756 KB |
Output is correct |
15 |
Correct |
14 ms |
23820 KB |
Output is correct |
16 |
Correct |
15 ms |
23816 KB |
Output is correct |
17 |
Correct |
16 ms |
23820 KB |
Output is correct |
18 |
Correct |
15 ms |
23884 KB |
Output is correct |
19 |
Correct |
15 ms |
23784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23748 KB |
Output is correct |
3 |
Correct |
15 ms |
23760 KB |
Output is correct |
4 |
Correct |
15 ms |
23860 KB |
Output is correct |
5 |
Correct |
15 ms |
23756 KB |
Output is correct |
6 |
Correct |
15 ms |
23752 KB |
Output is correct |
7 |
Correct |
14 ms |
23756 KB |
Output is correct |
8 |
Correct |
14 ms |
23820 KB |
Output is correct |
9 |
Correct |
14 ms |
23780 KB |
Output is correct |
10 |
Correct |
14 ms |
23796 KB |
Output is correct |
11 |
Correct |
14 ms |
23756 KB |
Output is correct |
12 |
Correct |
14 ms |
23708 KB |
Output is correct |
13 |
Correct |
14 ms |
23756 KB |
Output is correct |
14 |
Correct |
15 ms |
23820 KB |
Output is correct |
15 |
Correct |
15 ms |
23812 KB |
Output is correct |
16 |
Correct |
14 ms |
23756 KB |
Output is correct |
17 |
Correct |
15 ms |
23732 KB |
Output is correct |
18 |
Correct |
15 ms |
23756 KB |
Output is correct |
19 |
Correct |
15 ms |
23908 KB |
Output is correct |
20 |
Correct |
15 ms |
23740 KB |
Output is correct |
21 |
Correct |
14 ms |
23752 KB |
Output is correct |
22 |
Correct |
14 ms |
23776 KB |
Output is correct |
23 |
Correct |
16 ms |
23780 KB |
Output is correct |
24 |
Correct |
14 ms |
23756 KB |
Output is correct |
25 |
Correct |
14 ms |
23820 KB |
Output is correct |
26 |
Correct |
15 ms |
23816 KB |
Output is correct |
27 |
Correct |
16 ms |
23820 KB |
Output is correct |
28 |
Correct |
15 ms |
23884 KB |
Output is correct |
29 |
Correct |
15 ms |
23784 KB |
Output is correct |
30 |
Correct |
13 ms |
23916 KB |
Output is correct |
31 |
Correct |
14 ms |
23836 KB |
Output is correct |
32 |
Correct |
15 ms |
23924 KB |
Output is correct |
33 |
Correct |
16 ms |
23956 KB |
Output is correct |
34 |
Correct |
16 ms |
24012 KB |
Output is correct |
35 |
Correct |
16 ms |
24088 KB |
Output is correct |
36 |
Correct |
16 ms |
24152 KB |
Output is correct |
37 |
Correct |
16 ms |
24128 KB |
Output is correct |
38 |
Correct |
16 ms |
24184 KB |
Output is correct |
39 |
Correct |
17 ms |
24176 KB |
Output is correct |
40 |
Correct |
17 ms |
24604 KB |
Output is correct |
41 |
Correct |
17 ms |
24596 KB |
Output is correct |
42 |
Correct |
18 ms |
24212 KB |
Output is correct |
43 |
Correct |
16 ms |
24344 KB |
Output is correct |
44 |
Correct |
17 ms |
24236 KB |
Output is correct |
45 |
Correct |
20 ms |
24268 KB |
Output is correct |
46 |
Correct |
19 ms |
24088 KB |
Output is correct |
47 |
Correct |
18 ms |
24080 KB |
Output is correct |
48 |
Correct |
18 ms |
24004 KB |
Output is correct |
49 |
Correct |
18 ms |
24088 KB |
Output is correct |
50 |
Correct |
18 ms |
24116 KB |
Output is correct |
51 |
Correct |
17 ms |
24092 KB |
Output is correct |
52 |
Correct |
17 ms |
24084 KB |
Output is correct |
53 |
Correct |
18 ms |
24140 KB |
Output is correct |
54 |
Correct |
20 ms |
24260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23748 KB |
Output is correct |
3 |
Correct |
15 ms |
23760 KB |
Output is correct |
4 |
Correct |
15 ms |
23860 KB |
Output is correct |
5 |
Correct |
15 ms |
23756 KB |
Output is correct |
6 |
Correct |
15 ms |
23752 KB |
Output is correct |
7 |
Correct |
14 ms |
23756 KB |
Output is correct |
8 |
Correct |
14 ms |
23820 KB |
Output is correct |
9 |
Correct |
14 ms |
23780 KB |
Output is correct |
10 |
Correct |
14 ms |
23796 KB |
Output is correct |
11 |
Correct |
14 ms |
23756 KB |
Output is correct |
12 |
Correct |
14 ms |
23708 KB |
Output is correct |
13 |
Correct |
14 ms |
23756 KB |
Output is correct |
14 |
Correct |
15 ms |
23820 KB |
Output is correct |
15 |
Correct |
15 ms |
23812 KB |
Output is correct |
16 |
Correct |
14 ms |
23756 KB |
Output is correct |
17 |
Correct |
15 ms |
23732 KB |
Output is correct |
18 |
Correct |
15 ms |
23756 KB |
Output is correct |
19 |
Correct |
15 ms |
23908 KB |
Output is correct |
20 |
Correct |
15 ms |
23740 KB |
Output is correct |
21 |
Correct |
14 ms |
23752 KB |
Output is correct |
22 |
Correct |
14 ms |
23776 KB |
Output is correct |
23 |
Correct |
16 ms |
23780 KB |
Output is correct |
24 |
Correct |
14 ms |
23756 KB |
Output is correct |
25 |
Correct |
14 ms |
23820 KB |
Output is correct |
26 |
Correct |
15 ms |
23816 KB |
Output is correct |
27 |
Correct |
16 ms |
23820 KB |
Output is correct |
28 |
Correct |
15 ms |
23884 KB |
Output is correct |
29 |
Correct |
15 ms |
23784 KB |
Output is correct |
30 |
Correct |
13 ms |
23916 KB |
Output is correct |
31 |
Correct |
14 ms |
23836 KB |
Output is correct |
32 |
Correct |
15 ms |
23924 KB |
Output is correct |
33 |
Correct |
16 ms |
23956 KB |
Output is correct |
34 |
Correct |
16 ms |
24012 KB |
Output is correct |
35 |
Correct |
16 ms |
24088 KB |
Output is correct |
36 |
Correct |
16 ms |
24152 KB |
Output is correct |
37 |
Correct |
16 ms |
24128 KB |
Output is correct |
38 |
Correct |
16 ms |
24184 KB |
Output is correct |
39 |
Correct |
17 ms |
24176 KB |
Output is correct |
40 |
Correct |
17 ms |
24604 KB |
Output is correct |
41 |
Correct |
17 ms |
24596 KB |
Output is correct |
42 |
Correct |
18 ms |
24212 KB |
Output is correct |
43 |
Correct |
16 ms |
24344 KB |
Output is correct |
44 |
Correct |
17 ms |
24236 KB |
Output is correct |
45 |
Correct |
20 ms |
24268 KB |
Output is correct |
46 |
Correct |
19 ms |
24088 KB |
Output is correct |
47 |
Correct |
18 ms |
24080 KB |
Output is correct |
48 |
Correct |
18 ms |
24004 KB |
Output is correct |
49 |
Correct |
18 ms |
24088 KB |
Output is correct |
50 |
Correct |
18 ms |
24116 KB |
Output is correct |
51 |
Correct |
17 ms |
24092 KB |
Output is correct |
52 |
Correct |
17 ms |
24084 KB |
Output is correct |
53 |
Correct |
18 ms |
24140 KB |
Output is correct |
54 |
Correct |
20 ms |
24260 KB |
Output is correct |
55 |
Correct |
22 ms |
24896 KB |
Output is correct |
56 |
Correct |
43 ms |
28060 KB |
Output is correct |
57 |
Correct |
63 ms |
31064 KB |
Output is correct |
58 |
Correct |
79 ms |
33048 KB |
Output is correct |
59 |
Correct |
111 ms |
36116 KB |
Output is correct |
60 |
Correct |
169 ms |
39232 KB |
Output is correct |
61 |
Correct |
157 ms |
41344 KB |
Output is correct |
62 |
Correct |
175 ms |
43160 KB |
Output is correct |
63 |
Correct |
256 ms |
46824 KB |
Output is correct |
64 |
Correct |
224 ms |
48148 KB |
Output is correct |
65 |
Correct |
145 ms |
72064 KB |
Output is correct |
66 |
Correct |
135 ms |
72104 KB |
Output is correct |
67 |
Correct |
139 ms |
53316 KB |
Output is correct |
68 |
Correct |
202 ms |
57840 KB |
Output is correct |
69 |
Correct |
264 ms |
56240 KB |
Output is correct |
70 |
Correct |
213 ms |
56132 KB |
Output is correct |
71 |
Correct |
285 ms |
45092 KB |
Output is correct |
72 |
Correct |
278 ms |
45260 KB |
Output is correct |
73 |
Correct |
290 ms |
44624 KB |
Output is correct |
74 |
Correct |
305 ms |
44836 KB |
Output is correct |
75 |
Correct |
304 ms |
44380 KB |
Output is correct |
76 |
Correct |
325 ms |
44576 KB |
Output is correct |
77 |
Correct |
331 ms |
44252 KB |
Output is correct |
78 |
Correct |
399 ms |
44204 KB |
Output is correct |
79 |
Correct |
428 ms |
43776 KB |
Output is correct |