# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
883570 | nghiaaa | Harbingers (CEOI09_harbingers) | C++14 | 91 ms | 18500 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define ii pair<int,int>
#define f first
#define s second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define all(v) v.begin(),v.end()
#define BIT(i) ((ll)1<<(i))
#define endl "\n"
#define debug(x) for (auto p: x) cout<<p<<' ';cout<<endl
#define forw(i,j,z) for(int i=(int)j;i<=(int)z;i++)
#define forw2(i,j,z,k) for(int i=(int)j;i<=(int)z;i+=k)
#define ford(i,j,z) for (int i=(int)j;i>=(int)z;i--)
#define ford2(i,j,z,k) for (int i=(int)j;i>=(int)z;i-=k)
#define sz(a) (int)a.size()
const int N=1e5+10;
struct Line{
ll a,b;
Line(){}
Line(ll a1,ll b1):a(a1),b(b1){}
ll operator ()(const int x) const{
return a*x+b;
}
};
int n,prepare[N+1],velocity[N+1],SZ=-1;
vector<ii> adj[N+1];
ll dp[N+1],h[N+1];
Line hull[N+1];
db point(Line &u,Line &v)
{
return 1.0*(u.b-v.b)/(v.a-u.a);
}
ll query(int x)
{
if (SZ==0) return hull[0](x);
int l=0,r=SZ-1;
while(l<r)
{
int mid=((l+r)>>1)+1;
if (x<point(hull[mid],hull[mid+1]))
r=mid-1;
else l=mid;
}
return min(hull[l](x),hull[l+1](x));
}
void update(Line u)
{
int l=1,r=SZ;
while(l<r)
{
int mid=(l+r)>>1;
if (point(hull[SZ-1],hull[SZ])>=point(hull[SZ],u))
r=mid;
else l=mid+1;
}
SZ=l;
// while(SZ>0&&point(hull[SZ-1],hull[SZ])>=point(hull[SZ],u))
// SZ--;
}
void dfs(int u,int dad)
{
int preSZ=SZ;Line preLine;
if (u>1){
dp[u]=query(velocity[u])+h[u]*velocity[u]+prepare[u];
update(Line(-h[u],dp[u]));
preLine=hull[++SZ];
hull[SZ]=Line(-h[u],dp[u]);
}
forw(i,0,sz(adj[u])-1) if (adj[u][i].f!=dad)
{
int v=adj[u][i].f,d=adj[u][i].s;
h[v]=h[u]+d;
dfs(v,u);
}
if (u>1){
hull[SZ]=preLine;
SZ=preSZ;
}
}
void solve()
{
cin>>n;
forw(i,2,n)
{
int u,v,d;
cin>>u>>v>>d;
adj[u].push_back(mp(v,d));
adj[v].push_back(mp(u,d));
}
forw(i,2,n){
cin>>prepare[i]>>velocity[i];
}
hull[++SZ]=Line(0,0);
dfs(1,1);
forw(i,2,n) cout<<dp[i]<<' ';
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//cout<<setprecision(6)<<fixed;
//time_t TIME_TU=clock();
int t=1;
//cin>>t;
while(t--)
solve();
//time_t TIME_TV=clock();
//cerr<<(db)(TIME_TV-TIME_TU)/CLOCKS_PER_SEC<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |