# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
225131 | MKopchev | Harbingers (CEOI09_harbingers) | C++14 | 171 ms | 16760 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;
const int nmax=1e5+42;
int n;
vector< pair<int/*to*/,int/*size*/> > adj[nmax];
int depth[nmax];
long long dp[nmax];
int parent[nmax];
int start[nmax],speed[nmax];
void dfs(int node,int par)
{
for(auto k:adj[node])
if(k.first!=par)
{
depth[k.first]=depth[node]+k.second;
parent[k.first]=node;
dfs(k.first,node);
}
}
pair<int/*a*/,long long/*b*/> active[nmax];//ax+b
double cross(pair<int/*a*/,long long/*b*/> u,pair<int/*a*/,long long/*b*/> v)
{
return 1.0*(u.second-v.second)/(v.first-u.first);
}
long long ask(int pos,int x)
{
return 1LL*active[pos].first*x+active[pos].second;
}
int pointer=0;
long long ask_query(int x)
{
if(pointer==0)return 0;
long long ret=0;
ret=min(ret,ask(1,x));
ret=min(ret,ask(pointer,x));
int ok=0,not_ok=pointer;
while(not_ok-ok>1)
{
int av=(ok+not_ok)/2;
ret=min(ret,ask(av,x));
ret=min(ret,ask(av+1,x));
if(cross(active[av],active[av+1])<=x)ok=av;
else not_ok=av;
}
return ret;
}
void solve(int node,int par)
{
pair<int/*a*/,long long/*b*/> mem_active;
int mem_pointer;
if(node!=1)
{
dp[node]=ask_query(speed[node]);
dp[node]+=start[node]+1LL*depth[node]*speed[node];
pair<int/*a*/,long long/*b*/> current={-depth[node],dp[node]};
int ok=pointer,not_ok=0;
while(ok-not_ok>1)
{
int av=(ok+not_ok)/2;
if(cross(active[av],active[av+1])>=cross(active[av],current))ok=av;
else not_ok=av;
}
mem_active=active[ok+1];
mem_pointer=pointer;
pointer=ok+1;
active[pointer]=current;
}
for(auto k:adj[node])
if(k.first!=par)
{
solve(k.first,node);
}
if(node!=1)
{
active[pointer]=mem_active;
pointer=mem_pointer;
}
}
int main()
{
scanf("%i",&n);
int u,v,d;
for(int i=1;i<n;i++)
{
scanf("%i%i%i",&u,&v,&d);
adj[u].push_back({v,d});
adj[v].push_back({u,d});
}
for(int i=2;i<=n;i++)scanf("%i%i",&start[i],&speed[i]);
dfs(1,0);
solve(1,0);
for(int i=2;i<=n;i++)
{
printf("%lld",dp[i]);
if(i==n)printf("\n");
else printf(" ");
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |