This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define F first
#define S second
#define P push
#define pb push_back
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define W while
#define R return
#define C continue
#define SI size()
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define pii pair<int,int>
#define SF(x) scanf("%Id",&x)
#define SF2(x,y) scanf("%Id%Id",&x,&y)
#define SF3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)
#define SF4(x,y,z,o) scanf("%I64d%I64d%I64d%I64d",&x,&y,&z,&o)
#define all(v) v.begin(),v.end()
using namespace std;
const long long INF = 1e9+1;
const long long MOD = 1e9+7;
const int MX=300015;
vector<pll> adj[MX];
ll a[MX];
ll n,m;
void dfs(ll ver,ll val){
for(auto i : adj[ver]){
dfs(i.F,val+i.S);
}
if(ver>n)a[ver]=val;
}
ll res;
pll seg(vector<pll> v){
sort(all(v));
ll sum=0,l=0,r=0;
ll mn=INF*INF;
ll rl=INF*INF,rr=0;
for(auto i : v){
if(i.S==1)
sum+=i.F-v[0].F,r++;
}
r--;
mn=sum;
rl=v[0].F,rr=v[0].F;
ll ls=sum;
for(ll i=1;i<v.size();i++){
sum-=1LL*r*(v[i].F-v[i-1].F);
sum+=1LL*l*(v[i].F-v[i-1].F);
if(v[i].S==1)r--;
else l++;
if(mn==sum){
if(ls!=sum)W(1);
rr=v[i].F;
ls=sum;
}
if(mn>sum){
ls=sum;
mn=sum;
rr=v[i].F;
rl=v[i].F;
}
}
res+=mn;
R{rl,rr};
}
pll pro(ll ver){
if(ver>n){
R {a[ver],a[ver]};
}
pll ret;
vector<pll> v;
pll p;
for(auto i : adj[ver]){
p=pro(i.F);
v.pb({p.F,1});
v.pb({p.S,2});
}
ret=seg(v);
// cout<<ver<<" "<<res<<endl;
R ret;
}
int main(){
cin>>n>>m;
for(int i=2;i<=n+m;i++){
int x,y;
scanf("%d%d",&x,&y);
adj[x].pb({i,y});
}
dfs(1,0);
pro(1);
cout<<res;
}
Compilation message (stderr)
fireworks.cpp: In function 'std::pair<long long int, long long int> seg(std::vector<std::pair<long long int, long long int> >)':
fireworks.cpp:49:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(ll i=1;i<v.size();i++){
^
fireworks.cpp: In function 'int main()':
fireworks.cpp:89:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&x,&y);
^
# | 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... |