# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
738647 | bgnbvnbv | Harbingers (CEOI09_harbingers) | C++14 | 1088 ms | 29172 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>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<ll,ll>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=1e5+10;
const ll inf=1e18;
const ll mod=1e9+7;
#define line pli
using ld=long double;
struct ConvexHullTrick
{
vector<line> dt;
vector<ld> x;
struct qq
{
line a;
ld b;
bool c;
};
vector<qq>D;
void rollback(ll cc)
{
while(D.size()>cc)
{
if(D.back().c==1)
{
dt.pop_back();
x.pop_back();
}
else
{
dt.pb(D.back().a);
x.pb(D.back().b);
}
D.pop_back();
}
}
ld giao(line l1, line l2)
{
return 1. * (l2.se - l1.se) / (l1.fi - l2.fi);
}
bool chk(line l2, line l1, line l)
{
return giao(l, l1) > giao(l, l2);
}
void add(line l)
{
while (dt.size() >= 2 && !chk(dt[dt.size() - 2], dt.back(), l))
{
D.pb({dt.back(),x.back(),0});
dt.pop_back();
x.pop_back();
}
if (dt.empty())
x.pb(-inf);
else
x.pb(giao(l, dt.back()));
dt.pb(l);
D.pb({dt.back(),x.back(),1});
}
ll query(ll uoc)
{
int p = upper_bound(x.begin(), x.end(), uoc) - x.begin() - 1;
return dt[p].fi * uoc + dt[p].se;
}
}cht;
ll dp[maxN];
ll v[maxN];
ll s[maxN];
ll h[maxN];
vector<pli>g[maxN];
void dfs(ll u=1,ll p=0)
{
dp[1]=0;
if(u!=1)
{
dp[u]=cht.query(v[u])+s[u]+h[u]*v[u];
}
ll x=cht.D.size();
cht.add({-h[u],dp[u]});
for(auto zz:g[u])
{
if(zz.fi!=p)
{
h[zz.fi]=h[u]+zz.se;
dfs(zz.fi,u);
}
}
cht.rollback(x);
}
ll n;;
void solve()
{
cin >> n;
for(int i=1;i<n;i++)
{
ll 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] >> v[i];
}
dfs();
for(int i=2;i<=n;i++) cout << dp[i]<<' ';
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |