# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
547000 | Jomnoi | Harbingers (CEOI09_harbingers) | C++17 | 40 ms | 65536 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 DEBUG 0
using namespace std;
const int MAX_N = 1e5 + 10;
int S[MAX_N], V[MAX_N];
vector <pair <int, int>> adj[MAX_N];
long long ans[MAX_N];
long long divide(const long long &a, const long long &b) {
return a / b - ((a ^ b) < 0 and a % b != 0);
}
class Line {
public :
long long m, c;
Line() {}
Line(const long long &m_, const long long &c_) : m(m_), c(c_) {}
long long dot(const long long &x) const {
return m * x + c;
}
long long intersectX(const Line &o) const {
return divide(o.c - c, m - o.m);
}
};
class ConvexHullTrick {
public :
deque <Line> hull;
void addLine(const Line &f) {
if(!hull.empty() and hull.back().m == f.m and hull.back().c == f.c) {
hull.pop_back();
}
while(hull.size() >= 2 and f.intersectX(hull.back()) <= hull.back().intersectX(hull[hull.size() - 2])) {
hull.pop_back();
}
hull.push_back(f);
}
long long query(const int &x) {
if(hull.empty()) {
return 0;
}
while(hull.size() >= 2 and hull[0].dot(x) <= hull[1].dot(x)) {
hull.pop_front();
}
return hull[0].dot(x);
}
}cht[MAX_N];
void dfs(const int &u, const int &p, const int &d) {
if(u != 1) {
cht[u] = cht[p];
ans[u] = -cht[u].query(V[u]) + 1ll*d * V[u] + S[u];
}
cht[u].addLine(Line(d, -ans[u]));
for(auto [v, w] : adj[u]) {
if(v == p) {
continue;
}
dfs(v, u, d + w);
}
cht[u].hull.clear();
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
for(int i = 1; i <= n - 1; i++) {
int u, v, d;
cin >> u >> v >> d;
adj[u].emplace_back(v, d);
adj[v].emplace_back(u, d);
}
for(int i = 2; i <= n; i++) {
cin >> S[i] >> V[i];
}
dfs(1, -1, 0);
for(int i = 2; i <= n; i++) {
cout << ans[i] << ' ';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |