# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1137470 | icoder178 | Harbingers (CEOI09_harbingers) | C++20 | 93 ms | 21316 KiB |
#include<iostream>
#include<algorithm>
using namespace std;
const long long maxn = 100005;
long long dp[maxn];
int n,dst[maxn],rmv[maxn],s[maxn],v[maxn],ncnt = 0,rcnt = 0,chcnt = 0,connl[maxn],connr[maxn];
pair<int,long long> rmvd[maxn],cht[maxn];
pair<int,int> nconn[maxn];
pair<int,pair<int,int> > conn[maxn*2];
inline void fdp(int pos) {
int l = -5, r = chcnt+5;
while(l+1 < r) {
int mid = (l+r)/2;
bool isDecr;
if(mid < 0) isDecr = true;
else if(mid >= chcnt-1) isDecr = false;
else {
long long vl1 = cht[mid].second-(long long) v[pos]*(long long) cht[mid].first;
long long vl2 = cht[mid+1].second-(long long) v[pos]*(long long) cht[mid+1].first;
isDecr = (vl2 < vl1);
}
if(isDecr == true) l = mid;
else r = mid;
}
dp[pos] = cht[r].second-(long long) v[pos]*(long long) cht[r].first+(long long) v[pos]*(long long) dst[pos]+(long long) s[pos];
}
inline void prmv(int pos) {
int l = -5, r = chcnt+5;
while(l+1 < r) {
int mid = (l+r)/2;
bool isV;
if(mid < 0) isV = true;
else if(mid >= (chcnt-1)) isV = false;
else {
double x1 = cht[mid].first;
double y1 = cht[mid].second;
double x2 = cht[mid+1].first;
double y2 = cht[mid+1].second;
double x3 = dst[pos];
double y3 = dp[pos];
double v1 = (y3-y2)*(x2-x1);
double v2 = (y2-y1)*(x3-x2);
isV = (v1 > v2+1e-5);
}
if(isV) l = mid;
else r = mid;
}
rmv[pos] = r+1;
}
void dfs(int pos, int prevP) {
int ncl = ncnt;
for(long long i = connl[pos]; i < connr[pos]; i++) {
int targP = conn[i].second.first;
int newV = conn[i].second.second;
if(targP != prevP) {
dst[targP] = dst[pos]+newV;
fdp(targP);
prmv(targP);
nconn[ncnt].first = rmv[targP];
nconn[ncnt].second = targP;
ncnt++;
}
}
int ncr = ncnt;
sort(nconn+ncl,nconn+ncr);
int rcl = rcnt;
for(int i = ncr-1; i >= ncl; i--) {
while(chcnt > nconn[i].first) {
rmvd[rcnt] = cht[chcnt-1];
rcnt++;
chcnt--;
}
int targP = nconn[i].second;
cht[chcnt].first = dst[targP];
cht[chcnt].second = dp[targP];
chcnt++;
dfs(targP,pos);
chcnt--;
}
ncnt = ncl;
int rcr = rcnt;
for(long long i = rcr-1; i >= rcl; i--) {
cht[chcnt] = rmvd[i];
chcnt++;
}
rcnt = rcl;
}
int main () {
scanf("%d",&n);
for(long long i = 0; i < n-1; i++) {
long long a,b,c;
scanf("%d%d%lld",&conn[i*2].first,&conn[i*2].second.first,&conn[i*2].second.second);
conn[i*2+1].first = conn[i*2].second.first;
conn[i*2+1].second.first = conn[i*2].first;
conn[i*2+1].second.second = conn[i*2].second.second;
}
sort(conn,conn+n*2-2);
for(long long i = 0; i < n*2-2; i++) {
connr[conn[i].first] = i+1;
}
for(long long i = n*2-3; i >= 0; i--) {
connl[conn[i].first] = i;
}
for(long long i = 2; i <= n; i++) {
scanf("%d%d",&s[i],&v[i]);
}
chcnt++;
dfs(1,-1);
for(long long i = 2; i <= n; i++) {
printf("%lld ",dp[i]);
}
printf("\n");
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |