Submission #986865

#TimeUsernameProblemLanguageResultExecution timeMemory
986865mimiHarbingers (CEOI09_harbingers)C++17
60 / 100
1064 ms33184 KiB
#include <iostream> #include <vector> #include <stack> #include <algorithm> #define ff first #define ss second #define pp push_back #define all(x) (x).begin(),(x).end() #pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") using namespace std; using ll = long long; using ld = long double; using pii = pair <ll,ll>; const char el ='\n'; const char sp = ' '; const ll maxn = 1e5+5; ll n,cnt=1,sz; vector <pii> adj[maxn]; pii a[maxn]; ll dp[maxn],h[maxn]; struct previous { ll sz,pos,val; ll cur,pos1; ld val1; }; previous mark[maxn]; struct CHT { ll n,sz=0,cur=1; ll a[maxn],b[maxn]; ll line [maxn]; ld poll [maxn]; CHT(){ poll[0]=-1e18;} ld llersect (ll x, ll y){ return (ld)1.0 * (b[x]-b[y])/(a[y]-a[x]);} void add(ll i, ll u) { mark[u].sz=sz; mark[u].cur=cur; while(sz>0) { if(a[line[sz-1]]==a[i]) { if(b[line[sz-1]]<=b[i]) break; else { --sz; if(sz>0) --cur; } } else { if(sz<2) break; if(llersect(i,line[sz-1])<llersect(i,line[sz-2])) { --sz; if(sz>0) --cur; } else break; } } if(sz==0 or a[line[sz-1]]!=a[i]) { if(sz>0) { mark[u].val1=poll[cur]; poll[cur] = llersect(line[sz-1],i); mark[u].pos1=cur; ++cur; } mark[u].pos=sz; mark[u].val=line[sz]; line[sz]=i; ++sz; } } void undo(ll u) { sz=mark[u].sz; cur=mark[u].cur; line[mark[u].pos]=mark[u].val; poll[mark[u].pos1]=mark[u].val1; } ll gt(ll x) { ll id=upper_bound(poll,poll+cur,x)-poll-1; return a[line[id]]*x+b[line[id]]; } }; CHT f; void DFS(ll u, ll v) { for(pii i:adj[u]) { if(i.ff==v) continue; h[i.ff]=h[u]+i.ss; DFS(i.ff,u); } } void DFS1(ll u, ll v) { if(u!=1) { dp[u]=a[u].ss*h[u]+a[u].ff+f.gt(a[u].ss); f.a[cnt]=-h[u]; f.b[cnt]=dp[u]; f.add(cnt,u); ++cnt; } for(pii i:adj[u]) { if(i.ff==v) continue; DFS1(i.ff,u); } f.undo(u); } void input() { cin>>n; for(ll i=1,u,v,s;i<n;++i) { cin>>u>>v>>s; adj[u].pp({v,s}); adj[v].pp({u,s}); } for(ll i=2,u,v;i<=n;++i) { cin>>u>>v; a[i]={u,v}; } f.a[0]=0; f.b[0]=0; f.add(0,0); DFS(1,0); DFS1(1,0); for(ll i=2;i<=n;++i) cout<<dp[i]<<sp; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); input(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...