# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
294505 | LifeHappen__ | Harbingers (CEOI09_harbingers) | C++14 | 113 ms | 22644 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 forinc(a,b,c) for(int a=b, _c=c; a<=_c; ++a)
#define fordec(a,b,c) for(int a=b, _c=c; a>=_c; --a)
#define forv(a,b) for(auto &a:b)
#define ii pair<int,long long>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(a) begin(a),end(a)
#define bit(x,i) (x>>(i-1)&1ll)
#define on(x,i) (x|(1ll<<(i-1)))
#define off(x,i) (x&~(1ll<<(i-1)))
const int N=1e5+5;
int n,top,pd[N],dep[N],dd[N];
vector<ii> ad[N];
ii p[N];
ii li[N];
long long f[N];
inline double G(ii x, ii y){
ii o=make_pair(0,0);
if(x==o) return 0;
return 1.0*(x.se-y.se) / (y.fi-x.fi);
}
inline bool ok(ii x){
if(x.fi==li[top].fi) return 0;
if(top==1) return 1;
return G(x,li[top])>G(x,li[top-1]);
}
inline int fii(ii x){
int l=2, r=top, now=top;
while(l<=r){
int mid=l+(r-l)/2;
if(G(x,li[mid-1])<=G(li[mid],li[mid-1])) now=mid-1, r=mid-1;
else l=mid+1;
}
return now;
}
inline void dfs(int u){
dd[u]=1;
forv(v,ad[u]){
if(!dd[v.fi]){
dep[v.fi]=dep[u]+v.se;
pd[v.fi]=u;
int l=1, r=top, z=0;
while(l<=r){
int mid=l+(r-l)/2;
if(G(li[mid], li[mid-1]) <=p[v.fi].se) z=mid, l=mid+1;
else r=mid-1;
}
f[v.fi]=li[z].se+li[z].fi*p[v.fi].se+dep[v.fi]*p[v.fi].se+p[v.fi].fi;
ii kek=make_pair(-dep[v.fi],f[v.fi]);
ii pre; int pos=top;
top=fii(kek);
pre=li[++top];
li[top]=kek;
///long long now=f[z]+(dep[v.fi]-dep[z])*p[v.fi].se+p[v.fi].fi; => f[z] - dep[z] * p[v.fi] + dep[v.fi] * p[v.fi].se+p[v.fi].fi;
dfs(v.fi);
li[top]=pre;
top=pos;
}
}
}
const int SIZE=1<<10;
int pointer=0;
char buffer[SIZE];
inline char readchar() { if(pointer==SIZE) { fread(buffer,1,SIZE,stdin); pointer=0; } return buffer[pointer++]; }
#define getchar readchar
inline int in() { long long x=0; char c=getchar(); bool neg=false; while(c!='-'&&('0'>c||c>'9')) c=getchar(); if(c=='-') neg=true,c=getchar(); while('0'<=c&&c<='9') x=10*x+c-'0',c=getchar(); if(neg) x=-x; return x; }
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#define task "harbinge"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
}
n=in();
forinc(i,2,n){
int u,v,c;
u=in(), v=in(), c=in();
ad[u].eb(v,c); ad[v].eb(u,c);
}
forinc(i,2,n) p[i]={in(), in()};
++top;
dfs(1);
forinc(i,2,n) cout<<f[i]<<' ';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |