# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
377291 | jhwest2 | Harbingers (CEOI09_harbingers) | C++14 | 145 ms | 20588 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("Ofast")
#pragma GCC optimize("unroll-loops")
#define va first
#define vb second
using namespace std;
typedef long long lint;
typedef pair<int, int> pint;
typedef pair<lint, lint> plint;
const int MAX = 1e5 + 10;
int n, S[MAX], V[MAX], Dist[MAX];
lint Dp[MAX];
vector<pint> G[MAX];
struct CHT {
plint V[MAX];
int P[20][MAX], pos, sz;
void update(lint a, lint b) {
for (int i = 19; i >= 0; i--) {
if (P[0][P[i][pos]] == 0) continue;
int c = P[i][pos], p = P[0][c];
if ((V[c].vb - V[p].vb) * (V[c].va - a) > (b - V[c].vb) * (V[p].va - V[c].va)) pos = p;
}
P[0][++sz] = pos; pos = sz;
for (int i = 1; i < 20; i++) P[0][P[i - 1][P[i - 1][pos]]];
}
lint query(lint x) {
int t = pos;
for (int i = 19; i >= 0; i--) {
if (P[0][P[i][t]] == 0) continue;
int c = P[i][t], p = P[0][c];
if ((V[c].vb - V[p].vb) > (V[p].va - V[c].vb) * x) t = p;
}
return V[t].va * x + V[t].vb;
}
} C;
void dfs(int prv, int cur) {
if (cur != 1) Dp[cur] = C.query(V[cur]) + (lint)V[cur] * Dist[cur] + S[cur];
C.update(-Dist[cur], Dp[cur]);
for (auto nxt : G[cur]) {
if (prv == nxt.vb) continue;
Dist[nxt.vb] = Dist[cur] + nxt.va;
dfs(cur, nxt.vb);
}
C.pos = C.P[0][C.pos];
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 1; i < n; i++) {
int x, y, w; cin >> x >> y >> w;
G[x].emplace_back(w, y); G[y].emplace_back(w, x);
}
for (int i = 2; i <= n; i++) cin >> S[i] >> V[i];
dfs(0, 1);
for (int i = 2; i <= n; i++) cout << Dp[i] << ' ';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |