# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
706060 | mychecksedad | Harbingers (CEOI09_harbingers) | C++17 | 102 ms | 20192 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.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define all(x) x.begin(), x.end()
const int N = 1e5+100, M = 1e5+10, K = 20;
struct Edge{
int to, w;
};
struct Line{
ll m, c;
ll eval(ll x){
return m*x + c;
}
};
int n, dist[N], chtsz = 0;
vector<Edge> g[N];
ll s[N], c[N], dp[N];
Line cht[N];
double intersection(const Line &a, const Line &b){
return (double) (a.c - b.c) / (b.m - a.m);
}
void pre(int v, int p){
for(auto U: g[v]){
int u = U.to;
ll w = U.w;
if(u != p){
dist[u] = dist[v] + w;
pre(u, v);
}
}
}
ll search(ll val){
int l = 0, r = chtsz - 2, ans = 0;
while(l <= r){
int m = l+r>>1;
if(intersection(cht[m], cht[m + 1]) < val){
l = m + 1;
ans = m + 1;
}else{
r = m - 1;
}
}
return ans;
}
ll rem(Line L){
if(chtsz < 2 || intersection(L, cht[chtsz - 1]) >= intersection(cht[chtsz - 1], cht[chtsz - 2]))
return chtsz;
int l = 1, r = chtsz - 1, ans = 1;
while(l <= r){
int m = l+r>>1;
if(intersection(L, cht[m]) < intersection(cht[m], cht[m - 1])){
ans = m;
r = m - 1;
}else{
l = m + 1;
}
}
return ans;
}
void dfs(int v, int p){
for(auto U: g[v]){
int u = U.to;
if(u != p){
int prev_sz, prev_pos;
Line prev_line;
int j = search(c[u]);
dp[u] = cht[j].c + cht[j].m * c[u] + s[u] + c[u] * dist[u];
Line L{-dist[u], dp[u]};
prev_sz = chtsz;
prev_pos = chtsz = rem(L);
prev_line = cht[chtsz];
cht[chtsz++] = L;
dfs(u, v);
chtsz = prev_sz;
cht[prev_pos] = prev_line;
}
}
}
void solve(){
cin >> n;
for(int i = 0; i < n - 1; ++i){
int u, v, w; cin >> u >> v >> w;
g[u].pb({v, w});
g[v].pb({u, w});
}
for(int i = 2; i <= n; ++i) cin >> s[i] >> c[i];
dist[1] = 0;
pre(1, 0);
dp[1] = 0;
cht[0] = {0, 0};
chtsz = 1;
dfs(1, 0);
for(int i = 2; i <= n; ++i) cout << dp[i] << ' ';
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
cout << '\n';
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |