Submission #553882

#TimeUsernameProblemLanguageResultExecution timeMemory
553882jcelinHarbingers (CEOI09_harbingers)C++14
100 / 100
118 ms25224 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int ui; typedef pair<ll, ll> Line; #define ii pair<int,int> #define vi vector<int> #define vii vector<ii> #define vll vector<ll> #define matrix vector<vi> #define matrixLL vector<vLL> #define vs vector<string> #define vui vector<ui> #define msi multiset<int> #define mss multiset<string> #define si set<int> #define ss set<string> #define PB push_back #define PF push_front #define PPB pop_back #define PPF pop_front #define X first #define Y second #define MP make_pair #define FOR(i, a, b) for (int i = int(a); i < int(b); i++) #define REP(i, n) FOR(i, 0, n) #define all(x) (x).begin(), (x).end() const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; const int dxx[] = {-1, 1, 0, 0, 1, 1, -1, -1}; const int dyy[] = {0, 0, -1, 1, -1, 1, -1, 1}; const string abc="abcdefghijklmnopqrstuvwxyz"; const string ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const ld pi = 3.14159265; const int mod = 1e9 + 7; const int MOD = 1e9 + 7; const int MAXN = 1e6 + 7; const int inf = mod; const ll INF = 1e18; const ll zero = ll(0); const int logo = 20; const int off = 1 << logo; const int trsz = off << 1; const int N = 1e5 + 123; ll f(Line me, ll x){ return me.X * x + me.Y; } int CHT[N], n, sz; vector< pair<int, ll> > g[N]; ll dp[N], d[N], sp[N], sl[N]; Line cf[N]; ll get(ll x){ int l = 1, r = sz - 1; while(l <= r){ int m = (l + r) / 2; if(f(cf[CHT[m]], x) < f(cf[CHT[m + 1]], x)) r = m - 1; else l = m + 1; } return f(cf[CHT[r + 1]], x); } bool bad(int x, int y, int z) { return (cf[x].Y - cf[y].Y) * (ld)(1) / (ld)(cf[y].X - cf[x].X) >= (ld)(cf[x].Y - cf[z].Y) * ld(1) / (ld)(cf[z].X - cf[x].X); } void dfs(int v, int pr = -1){ if(pr != -1) dp[v] = get(sp[v]) + sl[v] + sp[v] * d[v]; cf[v] = {-d[v], dp[v]}; int l = 2, r = sz; while(l <= r){ int m = (l + r) / 2; if(bad(CHT[m - 1], CHT[m], v)) r = m - 1; else l = m + 1; } int bfr = CHT[r + 1]; int bfr_sz = sz; sz = r + 1, CHT[r + 1] = v; for(pair<int, ll> to: g[v]) if(to.X != pr) d[to.X] = d[v] + to.Y, dfs(to.X, v); sz = bfr_sz, CHT[r + 1] = bfr; } void solve(){ cin >> n; for(int i = 1, x, y, z; i<n; i++) cin >> x >> y >> z, g[x].PB({y, z}), g[y].PB({x, z}); for(int i = 2; i <= n; i++) cin >> sl[i] >> sp[i]; dfs(1); for(int i = 2; i <= n; i++) cout << dp[i] << " "; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t=1; //cin >> t; while(t--)solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...