#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 1e5 + 5;
int n, x, y, z, s[N], t[N];
ll dp[N];
vector<pii> v[N];
bool qrm;
struct line {
ll m, c;
mutable ll p;
line(ll m, ll c) : m(m), c(c) {}
line(ll p) : p(p) {}
friend bool operator < (const line &l1, const line &l2) {
return (qrm ? l1.p < l2.p : l1.m > l2.m);
}
};
struct convex:multiset<line> {
ll div (ll a, ll b) {
return (a / b) - ((a ^ b) < 0 && a % b);
}
bool ck (iterator x, iterator y) {
if (y == end()) return x->p = 1e18, 0;
if (x->m == y->m) x->p = x->c <= y->c ? 1e18 : -1e18;
else x->p = div(x->c - y->c, y->m - x->m);
return x->p >= y->p;
}
void add (ll m, ll c) {
auto y = insert(line(m, c)), x = y++;
while (ck(x, y)) y = erase(y);
if ((y = x) != begin() && ck(--x, y)) ck(x, erase(y));
while ((y = x) != begin() && (--x)->p >= y->p) ck(x, erase(y));
}
ll qr (ll x) {
if (empty()) return 1e18;
qrm = 1; auto idx = lower_bound(x); qrm = 0;
return idx->m * x + idx->c;
}
} hull[N];
void dfs (int cur, int par, ll dist) {
if (cur != 1) {
dp[cur] = hull[cur].qr(t[cur]) + s[cur] + dist * t[cur];
}
hull[cur].add(-dist, dp[cur]);
for (auto &[x, y]: v[cur]) {
if (par == x) continue;
hull[x] = hull[cur];
dfs(x, cur, dist + y);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d %d %d", &x, &y, &z);
v[x].emplace_back(y, z);
v[y].emplace_back(x, z);
}
for (int i = 2; i <= n; i++) scanf("%d %d", &s[i], &t[i]);
dp[1] = 0;
dfs(1, 0, 0);
for (int i = 2; i <= n; i++) printf("%lld ", dp[i]);
}
Compilation message (stderr)
harbingers.cpp: In function 'int main()':
harbingers.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
harbingers.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%d %d %d", &x, &y, &z);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp:65:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | for (int i = 2; i <= n; i++) scanf("%d %d", &s[i], &t[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |