# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1137393 | icoder178 | Harbingers (CEOI09_harbingers) | C++20 | 2 ms | 2632 KiB |
#include<iostream>
#include<vector>
using namespace std;
#define int long long
const int maxv = (int) 4e18;
const int maxn = 100005;
int n,dst[maxn],dp[maxn],s[maxn],v[maxn];
vector<pair<int,int> > conn[maxn],cht;
void dfs(int pos, int prevP) {
if(pos != 1) {
int l = -5, r = cht.size()+5;
while(l+1 < r) {
int mid = (l+r)/2;
bool isDecr;
if(mid < 0) isDecr = true;
else if(mid >= cht.size()-1) isDecr = false;
else {
int vl1 = cht[mid].second-v[pos]*cht[mid].first;
int vl2 = cht[mid+1].second-v[pos]*cht[mid+1].first;
isDecr = (vl2 < vl1);
}
if(isDecr == true) l = mid;
else r = mid;
}
dp[pos] = cht[r].second-v[pos]*cht[r].first+v[pos]*dst[pos]+s[pos];
}
vector<pair<bool,pair<int,int> > > chtop;
while(cht.size() >= 2) {
__int128_t x1 = cht[cht.size()-2].first;
__int128_t y1 = cht[cht.size()-2].second;
__int128_t x2 = cht[cht.size()-1].first;
__int128_t y2 = cht[cht.size()-1].second;
__int128_t x3 = dst[pos];
__int128_t y3 = dp[pos];
__int128_t v1 = (y3-y2)*(x2-x1);
__int128_t v2 = (y2-y1)*(x3-x2);
if(v1 > v2) break;
chtop.push_back(make_pair(false,cht.back()));
cht.pop_back();
}
chtop.push_back(make_pair(true,make_pair(dst[pos],dp[pos])));
cht.push_back(make_pair(dst[pos],dp[pos]));
for(int i = 0; i < conn[pos].size(); i++) {
int targP = conn[pos][i].first;
int newV = conn[pos][i].second;
if(targP != prevP) {
dst[targP] = dst[pos]+newV;
dfs(targP,pos);
}
}
for(int i = chtop.size()-1; i >= 0; i--) {
if(chtop[i].first == false) cht.push_back(chtop[i].second);
else cht.pop_back();
}
}
signed main () {
// freopen("Q227.in","r",stdin);
// freopen("Q227.out","w",stdout);
freopen("harbingers.in","r",stdin);
freopen("harbingers.out","w",stdout);
scanf("%lld",&n);
for(int i = 0; i < n-1; i++) {
int a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
conn[a].push_back(make_pair(b,c));
conn[b].push_back(make_pair(a,c));
}
for(int i = 2; i <= n; i++) {
scanf("%lld%lld",&s[i],&v[i]);
}
dfs(1,-1);
for(int i = 2; i <= n; i++) {
printf("%lld ",dp[i]);
}
printf("\n");
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |