// 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 CHT {
struct Line {
mutable ll a, b, p;
bool operator<(const Line& o) const {
return a < o.a;
}
bool operator<(ll x) const {
return p < x;
}
};
using itr = multiset<Line>::iterator;
static constexpr ll inf = LLONG_MAX;
multiset<Line, less<>> mts;
ll div(ll a, ll b) {
return a / b - ((a ^ b) < 0 && a % b);
}
bool isect(itr x, itr y) {
if (y == mts.end()) {
x->p = inf;
return 0;
}
if (x->a == y->a) {
x->p = (x->b > y->b) ? inf : -inf;
}
else {
x->p = div(y->b - x->b, x->a - y->a);
}
return x->p >= y->p;
}
void insert(ll a, ll b) {
auto z = mts.insert({ a, b, 0 });
auto y = z++;
auto x = y;
while (isect(y, z)) z = mts.erase(z);
if (x != mts.begin() && isect(--x, y)) {
y = mts.erase(y);
isect(x, y);
}
y = x;
while (y != mts.begin() && (--x)->p >= y->p) {
isect(x, mts.erase(y));
y = x;
}
}
ll query(ll x) {
auto l = *mts.lower_bound(x);
return l.a * x + l.b;
}
};
int n;
vector<Data> g[maxn];
Town a[maxn];
CHT 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.insert(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.insert(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 |
57 ms |
10696 KB |
Output is correct |
4 |
Correct |
79 ms |
14128 KB |
Output is correct |
5 |
Correct |
110 ms |
15916 KB |
Output is correct |
6 |
Correct |
149 ms |
18420 KB |
Output is correct |
7 |
Incorrect |
82 ms |
11640 KB |
Output isn't correct |
8 |
Incorrect |
135 ms |
17732 KB |
Output isn't correct |
9 |
Incorrect |
125 ms |
16472 KB |
Output isn't correct |
10 |
Incorrect |
118 ms |
18112 KB |
Output isn't correct |