# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
716970 | thiago_bastos | Harbingers (CEOI09_harbingers) | C++17 | 187 ms | 50448 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"
using namespace std;
#define INF 1000000000
#define INFLL 1000000000000000000ll
#define EPS 1e-9
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
#define fi first
#define sc second
using i64 = long long;
using u64 = unsigned long long;
using ld = long double;
using ii = pair<int, int>;
using i128 = __int128;
const int N = 1e5 + 10, X = 1e4 + 10;
vector<ii> adj[N];
int n, dist[N], s[N], vel[N];
i64 dp[N];
pair<i64, i64> st[4 * X];
vector<tuple<int,pair<i64,i64>,int>> changes;
int curChange;
void initialize() {
fill(st, st + 4 * X, make_pair(0ll, INFLL));
}
void insert(i64 a, i64 b, int l, int r, int p = 1) {
int m = (l + r) / 2;
bool v1 = a * l + b < st[p].fi * l + st[p].sc;
bool v2 = a * m + b < st[p].fi * m + st[p].sc;
if(v2) {
changes.pb({p, st[p], curChange});
swap(st[p].fi, a);
swap(st[p].sc, b);
}
if(l == r) return;
else if(v1 != v2) insert(a, b, l, m, 2 * p);
else insert(a, b, m + 1, r, 2 * p + 1);
}
void insert(i64 a, i64 b) {
insert(a, b, 0, X - 1);
++curChange;
}
i64 get(int x, int l = 0, int r = X - 1, int p = 1) {
i64 y = st[p].fi * x + st[p].sc;
if(l == r) return y;
int m = (l + r) / 2;
i64 z = x <= m ? get(x, l, m, 2 * p) : get(x, m + 1, r, 2 * p + 1);
return min(y, z);
}
void undo() {
auto [p, f, _] = changes.back();
st[p] = f;
changes.pop_back();
}
void dfs(int u, int p) {
if(u != p) dp[u] = get(vel[u]) + s[u] + (i64)dist[u] * vel[u];
else dp[u] = 0;
int cur = curChange;
insert(-dist[u], dp[u]);
for(auto [v, l] : adj[u]) {
if(v == p) continue;
dist[v] = dist[u] + l;
dfs(v, u);
}
while(!changes.empty() && get<2>(changes.back()) == cur) undo();
}
void solve() {
cin >> n;
for(int i = 1; i < n; ++i) {
int a, b, c;
cin >> a >> b >> c;
--a, --b;
adj[a].pb({b, c});
adj[b].pb({a, c});
}
for(int i = 1; i < n; ++i) cin >> s[i] >> vel[i];
initialize();
dfs(0, 0);
for(int i = 1; i < n; ++i) cout << dp[i] << ' ';
cout << '\n';
}
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |