# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
334966 | Dymo | Harbingers (CEOI09_harbingers) | C++14 | 64 ms | 65540 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 pb push_back
#define eb emplace_back
#define ll long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define endl "\n"
const ll maxn=3e5+10;
const ll mod =1e9+7 ;
const ll base=3e18;
bool ktu=false;
struct Line {
mutable ll k, m, p;
bool operator<(const Line& o) const { return k > o.k; }
bool operator<(ll x) const { return p < x; }
};
struct tk : multiset<Line, less<>> {
static const ll inf = LLONG_MAX;
ll div(ll a, ll b) {
return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = inf, 0;
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(ll k, ll m) {
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
ll query(ll x) {
if (empty()) return base;
auto l = *lower_bound(x);
return l.k * x + l.m;
}
};
vector<pll> adj[maxn];
ll dep[maxn];
ll dep1[maxn];
ll siz[maxn];
tk st[4*maxn];
ll par[maxn];
ll get1(ll id,ll left,ll right,ll x,ll y,ll t )
{
if (x>right||y<left) return base;
if (x<=left&&y>=right)
{
/* if (ktu)
{
cout <<"WTF"<<" "<<id<<" "<<st[id].query(t)<<endl;
}*/
return st[id].query(t);
}
ll mid=(left+right)/2;
return min(get1(id*2,left,mid, x, y,t ),get1(id*2+1,mid+1, right, x, y,t ));
}
void update(ll id,ll left,ll right,ll x,ll y,ll pos)
{
if (pos>right||pos<left) return ;
/* if (pos==1)
{
cout <<left<<" "<<right<<" "<<x<<" "<<y<<" "<<id<<endl;
}*/
st[id].add(x,y);
if (left==right) return;
ll mid=(left+right)/2;
if (mid>=pos) update(id*2,left,mid,x, y, pos);
else update(id*2+1, mid+1, right, x, y, pos);
}
void dfs(ll u,ll par1)
{
siz[u]=1;
for (auto p:adj[u])
{
ll h=p.ff;
if (h==par1) continue;
dep[h]=dep[u]+p.ss;
dep1[h]=dep1[u]+1;
par[h]=u;
dfs(h,u);
siz[u]+=siz[h];
}
}
ll dp[maxn];
ll cntnw=0;
ll nchain=1;
ll chainhead[maxn];
ll chainid[maxn];
ll id[maxn];
void hld(ll u )
{
if (!chainhead[nchain])
chainhead[nchain]=u;
cntnw++;
chainid[u]=nchain;
id[u]=cntnw;
ll nxt=-1;
for (auto p:adj[u])
{
ll to=p.ff;
if (to==par[u]) continue;
if (nxt==-1||siz[nxt]<siz[to])
{
nxt=to;
}
}
if (nxt!=-1)
{
hld(nxt);
}
for (auto p:adj[u])
{
ll to= p.ff;
if (to==par[u]) continue;
if (to==nxt)
continue;
nchain++;
hld(to);
}
}
ll get(ll l,ll r,ll x)
{
return get1(1,1,cntnw,l,r,x);
}
void dosth(ll u,ll a ,ll x)
{
ll p=chainid[u];
ll chk= chainid[a];
ll p1=u;
dp[p1]=base;
while (1)
{
if (p==chk)
{
dp[p1]=min(dp[p1],get(id[a],id[u],x));
break;
}
dp[p1]=min(dp[p1],get(id[chainhead[p]],id[u],x));
u=par[chainhead[p]];
p=chainid[u];
}
}
ll nxt[maxn];
ll s[maxn];
ll v[maxn];
void dfs1(ll u)
{
if (u==1)
{
dp[u]=0;
}
else
{
dosth(u,nxt[u],-v[u]);
/* if (u==2)
{
cout <<q[u]+dep[u]*p[u]<<" "<<dp[u]<<endl;
}*/
dp[u]=(dp[u]+v[u]*dep[u]+s[u]);
}
update(1,1,cntnw,dep[u],dp[u],id[u]);
// cout <<dp[u]<<" "<<u<<endl;
for (auto p:adj[u])
{
ll to=p.ff;
if (to==par[u]) continue;
dfs1(to);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen("harbingers.in", "r"))
{
freopen("harbingers.in", "r", stdin);
freopen("harbingers.out", "w", stdout);
}
ll n;
cin>>n ;
for (int i=1;i<=n-1;i++)
{
ll x, y, d;
cin>> x>> y>> d;
adj[x].pb(make_pair(y,d));
adj[y].pb(make_pair(x,d));
}
dfs(1,0);
hld(1);
for (int i=2;i<=n;i++)
{
nxt[i]=1;
cin>>s[i]>>v[i];
}
dfs1(1);
for (int i=2;i<=n;i++)
{
cout <<dp[i]<<" ";
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |