제출 #860989

#제출 시각아이디문제언어결과실행 시간메모리
86098912345678Harbingers (CEOI09_harbingers)C++17
50 / 100
69 ms17872 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long const ll nx=1e5+5, inf=1e18; ll n, ds[nx], lz[nx], sp[nx], ans[nx], u, v, w; vector<pair<int, int>> d[nx]; bool querymode; struct line { ll m, c; mutable ll p; line(ll m, ll c, ll p): m(m), c(c), p(p){} bool operator< (const line &o) const {return querymode?p<o.p:m>o.m;} }; struct convexhull:multiset<line> { ll div(ll a, ll b) { if (a%b==0||(a^b)>=0) return a/b; return a/b-1; } bool isect(iterator x, iterator y) { if (y==end()) return x->p=inf, false; if (x->m==y->m) x->p=x->m>=y->m?-inf:inf; else x->p=div(y->c-x->c, x->m-y->m); return x->p>=y->p; } void add(ll m, ll c) { auto x=insert(line(m, c, 0)), y=next(x); while (isect(x, y)) y=erase(y); if ((y=x)!=begin()&&isect(--x, y)) isect(x, erase(y)); while ((y=x)!=begin()&&(--x)->p>=y->p) isect(x, erase(y)); } ll query(ll x) { querymode=1; auto itr=lower_bound(line(0, 0, x)); querymode=0; return itr->m*x+itr->c; } } s; void pre(int u, int p) { for (auto [v, w]:d[u]) if (v!=p) ds[v]=ds[u]+w, pre(v, u); } void dfs(int u, int p) { if (u!=1) ans[u]=lz[u]+ds[u]*sp[u]+s.query(sp[u]); s.add(-ds[u], ans[u]); for (auto [v, w]:d[u]) if (v!=p) dfs(v, u); } int main() { cin.tie(NULL)->sync_with_stdio(false); cin>>n; for (int i=0; i<n-1; i++) cin>>u>>v>>w, d[u].push_back({v, w}), d[v].push_back({u, w}); for (int i=2; i<=n; i++) cin>>lz[i]>>sp[i]; pre(1, 1); dfs(1, 1); for (int i=2; i<=n; i++) cout<<ans[i]<<'\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...