# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
716978 | thiago_bastos | Harbingers (CEOI09_harbingers) | C++17 | 170 ms | 44024 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;
vector<ii> adj[N];
int n, dist[N], s[N], vel[N], coords[N];
i64 dp[N];
using T = pair<int, i64>;
T st[4 * N];
vector<pair<int,T>> changes;
int cnt_changes;
void initialize() {
fill(st, st + 4 * n, make_pair(0ll, INFLL));
}
void insert(int a, i64 b, int l, int r, int p = 1) {
int m = (l + r) / 2;
bool v1 = (i64)a * coords[l] + b < (i64)st[p].fi * coords[l] + st[p].sc;
bool v2 = (i64)a * coords[m] + b < (i64)st[p].fi * coords[m] + st[p].sc;
if(v2) {
changes.pb({p, st[p]});
swap(st[p].fi, a);
swap(st[p].sc, b);
++cnt_changes;
}
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);
}
int insert(int a, i64 b) {
cnt_changes = 0;
insert(a, b, 0, n - 1);
return cnt_changes;
}
i64 get(int x, int l, int r, 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 <= coords[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], 0, n - 1) + s[u] + (i64)dist[u] * vel[u];
else dp[u] = 0;
int cnt = insert(-dist[u], dp[u]);
for(auto [v, l] : adj[u]) {
if(v == p) continue;
dist[v] = dist[u] + l;
dfs(v, u);
}
while(cnt--) 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];
coords[i] = vel[i];
}
sort(coords, coords + n);
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... |