#include <bits/stdc++.h>
// #include <ext/rope>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
using namespace std;
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define bit(mask, i) ((mask >> i) & 1)
#define el '\n'
#define F first
#define S second
#define int long long
template <class X, class Y> bool maximize(X &x, const Y &y) { return (x < y ? x = y, 1 : 0); }
template <class X, class Y> bool minimize(X &x, const Y &y) { return (x > y ? x = y, 1 : 0); }
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;
const int MULTI = 0;
const ld eps = 1e-9;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; // R D L U
const int ddx[4] = {-1, 1, 1, -1}, ddy[4] = {1, 1, -1, -1}; // UR DR DL UL
const char cx[4] = {'R', 'D', 'L', 'U'};
const ll base = 31;
const int nMOD = 2;
const ll mods[] = {(ll)1e9 + 10777, (ll)1e9 + 19777, (ll)1e9 + 3, (ll)1e9 + 3777};
const int maxn = 1e5 + 5;
int n, top;
vector<pair<int, ll>> adj[maxn];
ll S[maxn], V[maxn], f[maxn], d[maxn];
struct operation {
int pos, top;
pair<ll, ll> overwrite;
operation(int _p, int _t, pair<int, int> _o) {
pos = _p;
top = _t;
overwrite = _o;
}
};
vector<operation> undoLst;
pair<ll, ll> lines[maxn];
ll eval(pair<int, int> line, ll x) {
return line.F * x + line.S;
}
bool bad(pair<ll, ll> a, pair<ll, ll> b, pair<ll, ll> c) {
return (double)(b.S - a.S) / (a.F - b.F) >= (double)(c.S - a.S) / (a.F - c.F);
// return (c.S - a.S) * (a.F - b.F) <= (b.S - a.S) * (a.F - c.F);
}
ll getMin(ll coord) {
int l = 0, r = top - 1;
ll ans = eval(lines[l], coord);
while (l < r) {
int mid = (l + r) >> 1;
ll x = eval(lines[mid], coord);
ll y = eval(lines[mid + 1], coord);
if (x > y) l = mid + 1;
else r = mid;
ans = min(ans, min(x, y));
}
return ans;
}
void insertLine(pair<ll, ll> newLine) {
int l = 1, r = top - 1, k = top;
while (l <= r) {
int mid = (l + r) >> 1;
if (bad(lines[mid - 1], lines[mid], newLine)) {
k = mid;
r = mid - 1;
} else l = mid + 1;
}
undoLst.push_back(operation(k, top, lines[k]));
top = k + 1;
lines[k] = newLine;
}
void undo() {
operation ope = undoLst.back(); undoLst.pop_back();
top = ope.top; lines[ope.pos] = ope.overwrite;
}
void dfs(int u, int par) {
if (u > 1) f[u] = getMin(V[u]) + S[u] + V[u] * d[u];
insertLine(make_pair(-d[u], f[u]));
for (auto g: adj[u]) {
int v = g.F;
ll w = g.S;
if (v == par) continue;
d[v] = d[u] + w;
dfs(v, u);
}
undo();
}
void solve() {
cin >> n;
for (int i = 1, u, v, w; i < n; ++i) {
cin >> u >> v >> w;
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
for (int i = 2; i <= n; ++i)
cin >> S[i] >> V[i];
dfs(1, 0);
for (int i = 2; i <= n; ++i)
cout << f[i] << ' ';
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (!MULTI) solve();
else {
int test; cin >> test;
while (test--) solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |