# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
985780 | OAleksa | Harbingers (CEOI09_harbingers) | C++14 | 1084 ms | 32852 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define f first
#define s second
#define int long long
using namespace std;
const int N = 1e5 + 69;
struct line {
int k, n;
line(int _k, int _n) {
k = _k;
n = _n;
}
int val(int x) {
return x * k + n;
}
double inter(line t) {
return (double)(n - t.n) / (t.k - k);
}
};
int n, s[N], w[N], dp[N], dep[N];
vector<pair<int, int>> g[N];
deque<pair<line, double>> dq;
void dfs(int v, int p) {
int l = 0, r = dq.size() - 1, id = -1;
while (l <= r) {
int mid = (l + r) / 2;
if ((double)w[v] >= dq[mid].s) {
id = mid;
l = mid + 1;
}
else
r = mid - 1;
}
assert(id != -1);
dp[v] = dq[id].f.val(w[v]) + s[v] + w[v] * dep[v];
line ln = {-dep[v], dp[v]};
vector<pair<line, int>> dead;
while (dq.size() > 1 && ln.inter(dq.back().f) <= dq.back().s) {
dead.push_back(dq.back());
dq.pop_back();
}
double in = ln.inter(dq.back().f);
dq.push_back({ln, in});
for (auto u : g[v]) {
if (u.f == p)
continue;
dep[u.f] = dep[v] + u.s;
dfs(u.f, v);
}
if (dq.back().f.k == ln.k && dq.back().f.n == ln.n)
dq.pop_back();
while (!dead.empty()) {
dq.push_back(dead.back());
dead.pop_back();
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n;
for (int i = 1;i <= n - 1;i++) {
int a, b, c;
cin >> a >> b >> c;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
for (int i = 2;i <= n;i++)
cin >> s[i] >> w[i];
dq.push_back({{0, 0}, 0});
for (auto j : g[1]) {
dep[j.f] = j.s;
dfs(j.f, 1);
}
for (int i = 2;i <= n;i++)
cout << dp[i] << ' ';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |