// looking to shine brighter in this world...
#define TASK "harbingers"
#ifdef LOCAL
#include "local.h"
#else
#include <bits/stdc++.h>
#define debug(...)
#define DB(...)
#endif
using namespace std;
using ll = long long;
using ld = long double;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second
#define For(i, l, r) for (int i = (l); i <= (r); ++i)
#define Ford(i, r, l) for (int i = (r); i >= (l); --i)
#define Rep(i, n) For (i, 0, (n) - 1)
#define Repd(i, n) Ford (i, (n) - 1, 0)
#define Fe(...) for (auto __VA_ARGS__)
template <class C> inline int isz(const C& c) { return static_cast<int>(c.size()); }
template <class T> inline bool chmin(T& a, const T& b) { return (a > b) ? a = b, true : false; }
template <class T> inline bool chmax(T& a, const T& b) { return (a < b) ? a = b, true : false; }
const bool __initialization = []() {
cin.tie(nullptr)->sync_with_stdio(false);
if (fopen(TASK".inp", "r")) {
(void)(!freopen(TASK".inp", "r", stdin));
(void)(!freopen(TASK".out", "w", stdout)); }
cout << setprecision(12) << fixed;
return true;
}();
constexpr ld eps = 1e-9;
constexpr int inf = 1e9;
constexpr ll linf = 1e18;
// =============================================================================
constexpr int maxn = 1e5 + 5;
struct Data {
int v; ll c;
Data() : v(0), c(0) {}
Data(int _v, ll _c) : v(_v), c(_c) {}
};
struct Town {
ll init, speed;
Town() : init(0), speed(0) {}
Town(ll _i, ll _s) : init(_i), speed(_s) {}
};
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<>> {
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
static const ll inf = LLONG_MAX;
ll div(ll a, ll b) { // floored division
return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = inf, 0;
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(ll k, ll m) {
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
ll query(ll x) {
assert(!empty());
auto l = *lower_bound(x);
return l.k * x + l.m;
}
};
int n;
vector<Data> g[maxn];
Town a[maxn];
LineContainer hull;
ll dp[maxn];
ll dist[maxn];
void Dfs(int u, int p) {
dp[u] = a[u].init + a[u].speed * dist[u]
- hull.query(a[u].speed);
hull.add(dist[u], -dp[u]);
Fe (&[v, c] : g[u]) {
if (v == p) continue;
dist[v] = dist[u] + c;
Dfs(v, u);
}
}
signed main() {
cin >> n;
Rep (_, n - 1) {
int u, v; ll c; cin >> u >> v >> c;
g[u].emplace_back(v, c);
g[v].emplace_back(u, c);
}
For (i, 2, n) cin >> a[i].init >> a[i].speed;
hull.add(0, 0);
Dfs(1, -1);
// dp[u] + a[v].init + a[v].speed * (dist[v] - dist[u])
// dp[u] + s[v] + v[v] * (d[v] - d[u])
// dp[u] + s[v] + v[v] * d[v] - v[v] * d[u]
// s[v] + v[v] * d[v] - v[v] * d[u] + dp[u]
// s[v] + v[v] * d[v] + f(v[v])
For (i, 2, n) {
cout << dp[i] << " \n"[i == n];
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4172 KB |
Output isn't correct |
2 |
Correct |
5 ms |
4556 KB |
Output is correct |
3 |
Correct |
56 ms |
10292 KB |
Output is correct |
4 |
Correct |
80 ms |
13708 KB |
Output is correct |
5 |
Correct |
93 ms |
15388 KB |
Output is correct |
6 |
Correct |
137 ms |
17988 KB |
Output is correct |
7 |
Incorrect |
77 ms |
11204 KB |
Output isn't correct |
8 |
Incorrect |
133 ms |
17248 KB |
Output isn't correct |
9 |
Incorrect |
120 ms |
16148 KB |
Output isn't correct |
10 |
Incorrect |
115 ms |
17848 KB |
Output isn't correct |