제출 #968516

#제출 시각아이디문제언어결과실행 시간메모리
968516noyancanturkHarbingers (CEOI09_harbingers)C++17
80 / 100
96 ms22864 KiB
#include "bits/stdc++.h" using namespace std; #define int int64_t #define pb push_back const int lim=1e5+100; const int mod=1e9+7; using pii=pair<int,int>; int a[lim],b[lim]; vector<pii>v[lim]; int dep[lim]; int ans[lim]; int sz=0; int cur[lim]; #define calc(i) (ans[i]-a[node]*dep[i]) void dfs(signed node,signed par){ if(node==1){ ans[node]=0; }else{ int l=1,r=sz-1; ans[node]=calc(cur[0]); while(l<=r){ int mid1=l+(r-l)/3,mid2=r-(r-l)/3; int val1=calc(cur[mid1]),val2=calc(cur[mid2]); //cerr<<ans[node]<<" "<<val1<<" "<<val2<<"\n"; if(val1<val2){ if(val1<ans[node])ans[node]=val1; r=mid2-1; }else{ if(val2<ans[node])ans[node]=val2; l=mid1+1; } } ans[node]+=b[node]+a[node]*dep[node]; } int removed=sz; if( 1<sz&& (ans[node]-ans[cur[sz-1]])*(dep[cur[sz-2]]-dep[cur[sz-1]])> (ans[cur[sz-1]]-ans[cur[sz-2]])*(dep[cur[sz-1]]-dep[node]) ){ removed=sz-1; int l=1,r=sz-2; while(l<=r){ int mid=(l+r)>>1; if( (ans[node]-ans[cur[mid]])*(dep[cur[mid-1]]-dep[cur[mid]])> (ans[cur[mid]]-ans[cur[mid-1]])*(dep[cur[mid]]-dep[node]) ){ removed=mid; r=mid-1; }else{ l=mid+1; } } } int removedval=cur[removed],pastsz=sz; cur[removed]=node; sz=removed+1; for(auto[j,c]:v[node]){ if(j^par){ dep[j]=dep[node]+c; dfs(j,node); } } sz=pastsz; cur[removed]=removedval; } void solve(){ int n; cin>>n; for(int i=0;i<n-1;i++){ int x,y,z; cin>>x>>y>>z; v[x].pb({y,z}); v[y].pb({x,z}); } for(int i=2;i<=n;i++){ cin>>b[i]>>a[i]; } dfs(1,0); for(int i=2;i<=n;i++){ cout<<ans[i]<<" "; } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); #ifdef Local freopen(".in","r",stdin); freopen(".out","w",stdout); #endif int t=1; //cin>>t; while (t--) { solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...