This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//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 (stderr)
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){}
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |