Submission #474804

#TimeUsernameProblemLanguageResultExecution timeMemory
474804aniervsHarbingers (CEOI09_harbingers)C++17
0 / 100
1098 ms22948 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = (int)2e5 + 5; typedef pair<ll,ll> point; #define x first #define y second point operator +(point a, point b){ return {a.x + b.x, a.y + b.y}; } point operator -(point a, point b){ return {a.x - b.x, a.y - b.y}; } ll dot(const point &a, const point &b){ return a.x * b.x + a.y * b.y; } ll cross(const point &a, const point &b){ return a.x * b.y - a.y * b.x; } ll area2(point a, point b, point c){ return cross(b - a, c - a); } struct hull : vector<point>{ void add_point(point p){ // p is bigger than all the previous ones while(size() > 1 and area2(at(size()-2), at(size()-1), p) >= 0) pop_back(); push_back(p); } ll max_dot(point p){ assert(!empty()); int l = 0, r = (int)size() - 1; while(l < r){ int mid = (l + r)>>1; if(dot(at(mid), p) <= dot(at(mid + 1), p)) l = mid + 1; else r = mid; } assert(l < (int)size()); assert(l >= 0); return dot(at(l), p); } }; vector<pair<int,ll>> G[maxn]; ll speed[maxn], start[maxn], dp[maxn], sumW[maxn]; int n, sz[maxn]; point get_point(int u){ return {sumW[u], -dp[u]}; } void dfs(int u, int p){ sz[u] = 1; for(auto [v, w]:G[u]){ if(v == p) continue; sumW[v] = sumW[u] + w; dfs(v, u); sz[u] += sz[v]; } } vector<hull> lights; hull heavy; void dfsHLD(int u, int p){ if(u != 1){ dp[u] = -1e18; for(auto h:lights){ dp[u] = max(dp[u], h.max_dot({speed[u], 1})); } if(!heavy.empty()) dp[u] = max(dp[u], heavy.max_dot({speed[u], 1})); dp[u] = sumW[u] * speed[u] + start[u] - dp[u]; } heavy.push_back(get_point(u)); int bigChild = -1; for(auto [v, w]:G[u]){ if(v == p) continue; if(bigChild == -1 or sz[bigChild] < sz[v]) bigChild = v; } lights.push_back(heavy); for(auto [v, w]:G[u]) if(v != bigChild and v != p){ heavy.clear(); dfsHLD(v, u); } heavy = lights.back(); lights.pop_back(); if(bigChild != -1) dfsHLD(bigChild, u); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 1; i < n; i++){ int u, v, w; cin >> u >> v >> w; G[u].push_back({v, w}); G[v].push_back({u, w}); } for(int i = 2; i <= n; i++) cin >> start[i] >> speed[i]; dfs(1,-1); dfsHLD(1,-1); for(int i = 2; i <= n; i++) cout << dp[i] << ' '; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...