Submission #883587

#TimeUsernameProblemLanguageResultExecution timeMemory
883587nghiaaaHarbingers (CEOI09_harbingers)C++14
30 / 100
101 ms18516 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define db double #define ii pair<int,int> #define f first #define s second #define mp make_pair #define mt make_tuple #define pb push_back #define all(v) v.begin(),v.end() #define BIT(i) ((ll)1<<(i)) #define endl "\n" #define debug(x) for (auto p: x) cout<<p<<' ';cout<<endl #define forw(i,j,z) for(int i=(int)j;i<=(int)z;i++) #define forw2(i,j,z,k) for(int i=(int)j;i<=(int)z;i+=k) #define ford(i,j,z) for (int i=(int)j;i>=(int)z;i--) #define ford2(i,j,z,k) for (int i=(int)j;i>=(int)z;i-=k) #define sz(a) (int)a.size() const int N=1e5+10; struct Line{ ll a,b; Line(){} Line(ll a1,ll b1):a(a1),b(b1){} ll operator ()(const int x) const{ return a*x+b; } }; int n,prepare[N+1],velocity[N+1],SZ=-1; vector<ii> adj[N+1]; ll dp[N+1],h[N+1]; Line hull[N+1]; long double point(Line &u,Line &v) { return (long double)(u.b-v.b)/(long double)(v.a-u.a); } ll query(int x) { if (SZ==0) return hull[0](x); int l=0,r=SZ-1; while(l<r) { int mid=((l+r)>>1)+1; if (x<point(hull[mid],hull[mid+1])) r=mid-1; else l=mid; } return min(hull[l](x),hull[l+1](x)); } void update(Line u) { if (SZ==0) return ; int l=1,r=SZ; while(l<r) { int mid=((l+r)>>1)+1; if (point(hull[mid-1],hull[mid])>=point(hull[mid],u)) r=mid-1; else l=mid; } SZ=l; // while(SZ>0&&point(hull[SZ-1],hull[SZ])>=point(hull[SZ],u)) // SZ--; } void dfs(int u,int dad) { int preSZ=SZ;Line preLine; if (u>1){ dp[u]=query(velocity[u])+h[u]*velocity[u]+prepare[u]; update(Line(-h[u],dp[u])); preLine=hull[++SZ]; hull[SZ]=Line(-h[u],dp[u]); } forw(i,0,sz(adj[u])-1) if (adj[u][i].f!=dad) { int v=adj[u][i].f,d=adj[u][i].s; h[v]=h[u]+d; dfs(v,u); } if (u>1){ hull[SZ]=preLine; SZ=preSZ; } } void solve() { cin>>n; forw(i,2,n) { int u,v,d; cin>>u>>v>>d; adj[u].push_back(mp(v,d)); adj[v].push_back(mp(u,d)); } forw(i,2,n){ cin>>prepare[i]>>velocity[i]; } hull[++SZ]=Line(0,0); dfs(1,1); forw(i,2,n) cout<<dp[i]<<' '; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); //cout<<setprecision(6)<<fixed; //time_t TIME_TU=clock(); int t=1; //cin>>t; while(t--) solve(); //time_t TIME_TV=clock(); //cerr<<(db)(TIME_TV-TIME_TU)/CLOCKS_PER_SEC<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...