# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
492158 | couplefire | Harbingers (CEOI09_harbingers) | C++17 | 121 ms | 23676 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 int long long
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
const int N = 100005;
struct line{
int m, b; line(){}
line(int _m, int _b): m(_m), b(_b){}
ld isect(line o){return ((ld)o.b-b)/((ld)m-o.m);}
int eval(int x){return m*x+b;}
} st[N]; int top;
int query(int x){
int lo = 1, hi = top;
while(lo<hi){
int mid = lo+(hi-lo+1)/2;
if(st[mid].isect(st[mid-1])<=(ld)x) lo = mid;
else hi = mid-1;
} return st[lo].eval(x);
}
int add(line l){
int lo = 1, hi = top+1;
while(lo<hi){
int mid = lo+(hi-lo)/2;
if(mid>1 && st[mid].isect(l)<=st[mid].isect(st[mid-1])) hi = mid;
else lo = mid+1;
} return lo;
}
int n, s[N], w[N], d[N], dp[N];
vector<pii> adj[N];
void dfs(int v, int p){
line pline; int ptop = 0;
if(!v) dp[v] = 0, st[++top] = line(0, 0);
else{
dp[v] = query(s[v])+d[v]*s[v]+w[v];
line todo = line(-d[v], dp[v]);
int pos = add(todo);
pline = st[pos]; ptop = top;
st[top = pos] = todo;
}
for(auto [u, x] : adj[v])
if(u!=p) d[u] = d[v]+x, dfs(u, v);
if(v) st[top] = pline, top = ptop;
}
signed main(){
// freopen("a.in", "r", stdin);
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i = 0; i<n-1; ++i){
int u, v, d; cin >> u >> v >> d;
--u; --v;
adj[u].push_back({v, d});
adj[v].push_back({u, d});
}
for(int i = 1; i<n; ++i)
cin >> w[i] >> s[i];
dfs(0, 0);
for(int i = 1; i<n; ++i)
cout << dp[i] << ' ';
cout << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |