# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
474832 | aniervs | Harbingers (CEOI09_harbingers) | C++17 | 150 ms | 25760 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 +(const point &a, const point &b){ return {a.x + b.x, a.y + b.y}; }
point operator -(const point &a, const 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(const point &a, const point &b, const point &c){ return cross(b - a, c - a); }
struct hull : vector<point>{
void add_point(const point &p){ // p is greater 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(const point &p){
if(empty())
return LLONG_MIN;
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;
}
return dot(at(l), p);
}
};
vector<pair<int,int>> G[maxn];
ll speed[maxn], start[maxn], dp[maxn], sumW[maxn];
int n, sz[maxn], par[maxn];
void dfs(int u){
sz[u] = 1;
for(auto [v, w]:G[u]){
if(v == par[u])
continue;
par[v] = u;
sumW[v] = sumW[u] + w;
dfs(v);
sz[u] += sz[v];
}
}
hull CH[maxn];
int topHeavy[maxn];
ll query(int u){
const point p = {speed[u], 1};
ll ans = LLONG_MIN;
while(u){
int top = topHeavy[u];
ans = max(ans, CH[top].max_dot(p));
u = par[top];
}
return ans;
}
void dfsHLD(int u, int topChain){
topHeavy[u] = topChain;
if(u != 1)
dp[u] = sumW[u] * speed[u] + start[u] - query(u);
CH[topChain].add_point({sumW[u], -dp[u]});
int bigChild = -1;
for(auto [v, _]:G[u])
if(v != par[u] and (bigChild == -1 or sz[bigChild] < sz[v]))
bigChild = v;
for(auto [v, w]:G[u])
if(v != bigChild and v != par[u])
dfsHLD(v, v);
if(bigChild != -1)
dfsHLD(bigChild, topChain);
}
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);
dfsHLD(1, 1);
for(int i = 2; i <= n; i++)
cout << dp[i] << ' ';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |