# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
974598 | Cookie | Harbingers (CEOI09_harbingers) | C++14 | 79 ms | 23748 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>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
const ll mod =1e9 + 7, pr = 31;
const int mxn = 1e5 + 5, mxq = 1e5 + 5, sq = 1005, mxv = 5e4 + 1;
//const int base = (1 <<18);
const ll inf = 1e18 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
struct Line{
ll m, c;
Line (){
m = c = 0;
}
Line(ll _m, ll _c){
m = _m; c = _c;
}
ll eval(ll x){
return(x * m + c);
}
ld intersect(const Line &b){
return((ld)(b.c - c) / (m - b.m));
}
};
struct E{
int siz, pos;
Line v;
};
int n;
ll dp[mxn + 1], v[mxn + 1], c[mxn + 1];
Line dq[mxn + 1];
int siz = 0;
vt<pair<int, ll>>adj[mxn + 1];
vt<E>past;
ll get(ll x){
int l = 1, r = siz - 1, ans = 0;
while(l <= r){
int mid = (l + r) >> 1;
if(dq[mid - 1].intersect(dq[mid]) <= x){
ans = mid; l = mid + 1;
}else{
r = mid - 1;
}
}
return(dq[ans].eval(x));
}
void upd(Line v){
int l = 1, r = siz - 1, ans = siz;
while(l <= r){
int mid = (l + r) >> 1;
if(dq[mid - 1].intersect(dq[mid]) >= dq[mid - 1].intersect(v)){
ans = mid; r = mid - 1;
}else{
l = mid + 1;
}
}
past.pb({siz, ans, dq[ans]});
dq[ans] = v; siz = ans + 1;
}
void rollback(){
auto [psiz, pans, pv] = past.back(); past.pop_back();
siz = psiz; dq[pans] = pv;
}
void dfs(int s, int pre, ll dep){
//dp[s] = min(dp[i] + (dep[s] - dep[i]) * v[s]);
if(s != 1)dp[s] = get(v[s]) + dep * v[s] + c[s];
//cout << s << " " << -dep << " " << dp[s] << "\n";
upd(Line(-dep, dp[s]));
for(auto [v, w]: adj[s]){
if(v != pre){
dfs(v, s, dep + w);
rollback();
}
}
}
void solve(){
cin >> n;
for(int i = 1; i < n; i++){
int u, v, d; cin >> u >> v >> d;
adj[u].pb(mpp(v, d)); adj[v].pb(mpp(u, d));
}
for(int i = 2; i <= n; i++)cin >> c[i] >> v[i];
dfs(1, -1, 0);
for(int i = 2; i <= n; i++)cout << dp[i] << " ";
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("FOOD.inp", "r", stdin);
//freopen("FOOD.out", "w", stdout);
int tt; tt = 1;
while(tt--){
solve();
}
return(0);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |