# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
490121 | macktvz | Harbingers (CEOI09_harbingers) | C++17 | 1100 ms | 21824 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;
struct line {
long long a,b;
double intersectX(line l) {return (b-l.b)/(l.a-a);}
};
const int N = 1e5+1;
stack<pair<int,int>> ch;
vector<int> id(N);
deque<int> slop;
line l[N];
long long dp[N];
vector<pair<int,int>> adj[N];
long long A[N];
long long B[N];
void rollback(int i) {
while (ch.top().first != i || ch.top().second != 1) {
auto op = ch.top();
ch.pop();
if (op.second == 1) {
slop.pop_back();
} else {
slop.push_back(op.first);
}
}
}
long double intersect(int i, int j) {
if (l[i].a > l[j].a) swap(i,j);
return (l[i].b-l[j].b)/(l[j].a-l[i].a);
}
bool cmp(int idx, long long a) {
return intersect(slop[idx],slop[idx+1]) < a;
}
void insert(int idx) {
while (slop.size() > 1 && intersect(slop[slop.size()-2],slop.back()) >= intersect(slop.back(),idx)) {
ch.push({slop.back(),-1});
slop.pop_back();
}
ch.push({idx,1});
slop.push_back(idx);
}
void dfs(int i, int p,long long curr) {
if (i != 0) {
int j = slop[*lower_bound(id.begin(),id.begin()+slop.size()-1,A[i],cmp)];
dp[i] = curr*A[i] + B[i] -(l[j].a*A[i] + l[j].b);
}
l[i].a = curr;
l[i].b = -dp[i];
insert(i);
for(pair<int,int> u : adj[i]) {
if (u.first != p) {
dfs(u.first,i,curr+u.second);
rollback(i);
}
}
}
int main() {
iota(id.begin(),id.end(),0);
int n; cin >> n;
int a,b,c;
dp[0]=0;
for(int i = 1; i < n; i++) {
cin >> a >> b >>c;
--a; --b;
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
for(int i = 1; i< n; i++) {
cin >> B[i] >> A[i];
}
dfs(0,-1,0);
for(int i = 1; i < n; i++) {
cout << dp[i] << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |