# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
562543 | hoanghq2004 | Harbingers (CEOI09_harbingers) | C++14 | 203 ms | 28268 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>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
const int N = 1e5 + 10;
int n;
vector <pair <int, int> > g[N];
long long f[N];
int s[N], v[N], d[N];
struct Line {
long long a, b;
Line() {
a = 0, b = 1e18;
}
Line(long long a, long long b): a(a), b(b) {};
long long operator()(long long x) {
return a * x + b;
}
};
struct PersistentConvexHullTrick {
Line convex[N];
int bad(Line L1, Line L2, Line L3) {
return 1.0L * (L2.b - L1.b) / (L1.a - L2.a) >= 1.0L * (L3.b - L1.b) / (L1.a - L3.a);
}
int par[N][20];
void add(int u, int v, Line L) {
for (int i = 19; i >= 0; --i)
if (par[par[u][i]][0] && bad(convex[par[par[u][i]][0]], convex[par[u][i]], L)) u = par[u][i];
while (par[u][0] && bad(convex[par[u][0]], convex[u], L)) u = par[u][0];
convex[v] = L;
par[v][0] = u;
for (int i = 1; i < 20; ++i) par[v][i] = par[par[v][i - 1]][i - 1];
}
long long get(int u, long long x) {
for (int i = 19; i >= 0; --i)
if (par[par[u][i]][0] && convex[par[par[u][i]][0]](x) < convex[par[u][i]](x)) u = par[u][i];
return min(convex[u](x), convex[par[u][0]](x));
}
} DS;
void dfs(int u, int p) {
f[u] = (u == 1 ? 0 : DS.get(p, v[u]) + 1LL * d[u] * v[u] + s[u]);
DS.add(p, u, {- d[u], f[u]});
for (auto [v, w]: g[u]) {
if (v == p) continue;
d[v] = d[u] + w;
dfs(v, u);
}
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v, w;
cin >> u >> v >> w;
g[u].push_back({v, w});
g[v].push_back({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] << " \n"[i == n];
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |