제출 #967745

#제출 시각아이디문제언어결과실행 시간메모리
967745noyancanturkHarbingers (CEOI09_harbingers)C++17
0 / 100
121 ms31828 KiB
#include "bits/stdc++.h" using namespace std; #define int int64_t #define pb push_back const int lim=3e5+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]; deque<int>cur; #define calc(i) (ans[i]-a[node]*(dep[i])) void dfs(int node,int par){ if(node==1){ ans[node]=0; }else{ int l=0,r=cur.size()-1; while(l<r){ int mid1=l+(r-l)/3,mid2=r-(r-l)/3; int val1=calc(cur[mid1]),val2=calc(cur[mid2]); ans[node]=LLONG_MAX; 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]; } cur.push_front(node); for(auto[j,c]:v[node]){ if(j^par){ dep[j]=dep[node]+c; dfs(j,node); } } cur.pop_front(); } 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...