#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;
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){
mn=sum;
rl=min(rl,v[i].F);
rr=max(rr,v[i].F);
}
if(mn>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
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:48: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:87:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&x,&y);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
7288 KB |
Output is correct |
2 |
Correct |
6 ms |
7392 KB |
Output is correct |
3 |
Correct |
7 ms |
7464 KB |
Output is correct |
4 |
Correct |
7 ms |
7572 KB |
Output is correct |
5 |
Correct |
7 ms |
7572 KB |
Output is correct |
6 |
Correct |
7 ms |
7572 KB |
Output is correct |
7 |
Correct |
7 ms |
7572 KB |
Output is correct |
8 |
Correct |
7 ms |
7588 KB |
Output is correct |
9 |
Correct |
6 ms |
7592 KB |
Output is correct |
10 |
Correct |
6 ms |
7592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
7592 KB |
Output is correct |
2 |
Correct |
7 ms |
7592 KB |
Output is correct |
3 |
Incorrect |
7 ms |
7592 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
7288 KB |
Output is correct |
2 |
Correct |
6 ms |
7392 KB |
Output is correct |
3 |
Correct |
7 ms |
7464 KB |
Output is correct |
4 |
Correct |
7 ms |
7572 KB |
Output is correct |
5 |
Correct |
7 ms |
7572 KB |
Output is correct |
6 |
Correct |
7 ms |
7572 KB |
Output is correct |
7 |
Correct |
7 ms |
7572 KB |
Output is correct |
8 |
Correct |
7 ms |
7588 KB |
Output is correct |
9 |
Correct |
6 ms |
7592 KB |
Output is correct |
10 |
Correct |
6 ms |
7592 KB |
Output is correct |
11 |
Correct |
7 ms |
7592 KB |
Output is correct |
12 |
Correct |
7 ms |
7592 KB |
Output is correct |
13 |
Incorrect |
7 ms |
7592 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
7288 KB |
Output is correct |
2 |
Correct |
6 ms |
7392 KB |
Output is correct |
3 |
Correct |
7 ms |
7464 KB |
Output is correct |
4 |
Correct |
7 ms |
7572 KB |
Output is correct |
5 |
Correct |
7 ms |
7572 KB |
Output is correct |
6 |
Correct |
7 ms |
7572 KB |
Output is correct |
7 |
Correct |
7 ms |
7572 KB |
Output is correct |
8 |
Correct |
7 ms |
7588 KB |
Output is correct |
9 |
Correct |
6 ms |
7592 KB |
Output is correct |
10 |
Correct |
6 ms |
7592 KB |
Output is correct |
11 |
Correct |
7 ms |
7592 KB |
Output is correct |
12 |
Correct |
7 ms |
7592 KB |
Output is correct |
13 |
Incorrect |
7 ms |
7592 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |