Submission #474801

#TimeUsernameProblemLanguageResultExecution timeMemory
474801aniervsHarbingers (CEOI09_harbingers)C++17
0 / 100
83 ms11976 KiB
#ifdef LOCAL #include <prettyprint.hpp> #endif #include<bits/stdc++.h> #define endl '\n' #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define lg2(x) __lg(x) #define rem_dupl(x) sort(all(x)); x.erase(unique(all(x)), x.end()) using namespace std; typedef long long ll; const int maxn = (int)2e5 + 5; struct point{ ll x, y; point(){ x = y = 0; } point(ll _x, ll _y) : x(_x), y(_y) {} bool operator <(const point & other){ return make_pair(x, y) < make_pair(other.x, other.y); } point operator +(const point &other){ return {x + other.x, y + other.y}; } point operator -(const point &other){ return {x + other.x, y + other.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 for(auto q:*this) assert(q < p); while(size() > 1 and area2(at(size()-2), at(size()-1), p) >= 0) pop_back(); push_back(p); } ll max_dot(point p){ if(empty()) return LLONG_MIN; int l = 0, r = (int)size() - 1; while(l < r){ int mid = (l + r)>>1; assert(mid+1 < (int)size()); assert(mid >= 0); 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){ sz[u] = 1; for(auto [v, w]:G[u]){ sumW[v] = sumW[u] + w; dfs(v); sz[u] += sz[v]; } } vector<hull> lights; hull heavy; void dfsHLD(int u){ if(u != 1){ dp[u] = LLONG_MIN; 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)); /* dp[v] = LLONG_MAX; for(int p:ancestors) dp[v] = min(dp[v], dot({sumW[p], dp[p]}, {-speed[v], 1}); dp[v] += sumW[v] * speed[v]; dp[v] += start[v]; */ int bigChild = -1; for(auto [v, w]:G[u]) if(bigChild == -1 or sz[bigChild] < sz[v]) bigChild = v; lights.push_back(heavy); for(auto [v, w]:G[u]) if(v != bigChild){ heavy.clear(); dfsHLD(v); } heavy = lights.back(); lights.pop_back(); if(bigChild != -1) dfsHLD(bigChild); } 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}); } for(int i = 2; i <= n; i++) cin >> start[i] >> speed[i]; dfs(1); dfsHLD(1); for(int i = 2; i <= n; i++) cout << dp[i] << ' '; cout << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...