Submission #968498

#TimeUsernameProblemLanguageResultExecution timeMemory
968498noyancanturkHarbingers (CEOI09_harbingers)C++17
70 / 100
1077 ms25172 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,popsz=0; int cur[lim]; int popped[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=0,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=0; while(1<(sz)){ if( (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]) ){ popped[popsz++]=cur[sz-1]; sz--; removed++; }else{ break; } } cur[sz++]=node; for(auto[j,c]:v[node]){ if(j^par){ dep[j]=dep[node]+c; dfs(j,node); } } sz--; while(removed--){ cur[sz++]=popped[--popsz]; } } 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...