#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Line {
mutable ll k, m, p;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
static const ll inf = LLONG_MAX;
ll divFloor(ll a, ll b) {
return a / b - ((a ^ b) < 0 && a % b);
}
bool isect(iterator x, iterator y) {
if (y == end()) { x->p = inf; return false; }
if (x->k == y->k) x->p = (x->m > y->m ? inf : -inf);
else x->p = divFloor(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
struct Change { int type; Line val; };
// type=0: previously ERASED (we should re-insert on rollback)
// type=1: previously INSERTED (we should remove on rollback)
vector<Change> history;
// helper: find an element in multiset with matching (k,m)
iterator find_km(ll k, ll m) {
// find lower_bound by key k
auto it = lower_bound({k, m, 0});
// scan rightwards through equal k's to find matching m
while (it != end() && it->k == k) {
if (it->m == m) return it;
++it;
}
// also scan leftwards a bit in case lower_bound returned later equal slot
it = lower_bound({k, m, 0});
if (it != begin()) {
auto jt = it;
do {
--jt;
if (jt->k != k) break;
if (jt->m == m) return jt;
if (jt == begin()) break;
} while (true);
}
return end();
}
iterator add(ll k, ll m) {
auto z = insert({k,m,0}), y = z++, x = y;
history.push_back({1,*y}); // record insertion
while (isect(y,z)) {
history.push_back({0,*z});
z = erase(z);
}
if (x != begin() && isect(--x,y)) {
history.push_back({0,*y});
erase(y);
}
while ((y=x) != begin() && (--x)->p >= y->p) {
history.push_back({0,*y});
erase(y);
}
// return iterator to inserted line (may be invalid if erased later, but we use history to rollback)
// find current position of inserted (k,m)
auto itcur = find_km(k,m);
return itcur;
}
void rollback(int snap) {
while ((int)history.size() > snap) {
auto ch = history.back(); history.pop_back();
if (ch.type == 0) {
// re-insert the erased line
insert(ch.val);
} else {
// remove one inserted line matching (k,m)
auto it = find_km(ch.val.k, ch.val.m);
if (it != end()) erase(it);
// else: should not happen, but ignore safely
}
}
}
int snapshot() { return history.size(); }
ll query(ll x) {
assert(!empty());
auto l = *lower_bound(x);
return l.k * x + l.m;
}
};
int N;
vector<vector<pair<int,ll>>> adj;
vector<ll> S, V, dist, opt;
LineContainer cht;
void dfs(int u, int p) {
if (u == 1) {
opt[u] = 0;
} else {
// query: code uses sign-flip trick -> stored lines are (k = +dist, m = -opt)
ll best_neg = cht.query(V[u]); // this returns max of (-opt[j] + dist[j]*V[u])
ll best = -best_neg;
opt[u] = S[u] + V[u]*dist[u] + best;
}
int snap = cht.snapshot();
// add line for u: we store (k = +dist[u], m = -opt[u]) so hull is for max
cht.add(dist[u], -opt[u]);
for (auto [v,w] : adj[u]) if (v != p) {
dist[v] = dist[u] + w;
dfs(v,u);
}
cht.rollback(snap);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
if (!(cin >> N)) return 0;
adj.assign(N+1, {});
for (int i=0;i<N-1;i++){
int u,v; ll w; cin >> u >> v >> w;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
S.assign(N+1,0);
V.assign(N+1,0);
dist.assign(N+1,0);
opt.assign(N+1,0);
for (int i=2;i<=N;i++) cin >> S[i] >> V[i];
dfs(1,0);
for (int i=2;i<=N;i++) cout << opt[i] << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |