# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1071028 | Mike_Vu | Harbingers (CEOI09_harbingers) | C++14 | 1059 ms | 34384 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;
typedef long long ll;
typedef unsigned long long ull;
typedef double dou;
#define pii pair<int, int>
#define pb push_back
#define fi first
#define se second
bool maximize(ll &x, ll y ){
if (x < y) {x = y; return true;};
return false;
}
bool minimize(ll &x, ll y){
if (x > y) {x = y; return true;}
return false;
}
ll divi(ll x, ll y) {
return x/y-(x%y&&(x^y)<0);
}
struct Line{
ll m, b;
Line(ll slope, ll intercept){
m = slope;
b = intercept;
}
ll cal(ll x) {
return m*x+b;
}
};
struct CHT{
ll inter(Line a, Line b){
return divi(b.b-a.b, a.m-b.m);
}
vector<pair<Line, int>> hull;
void update(Line a, vector<pair<Line, int>> &temp) {
while (!hull.empty() && hull.back().se >= inter(hull.back().fi, a)){
temp.pb(hull.back());
hull.pop_back();
}
if (hull.empty()) {
hull.push_back({a, LLONG_MIN});
}
else {
hull.push_back({a, inter(a, hull.back().fi)});
}
}
void placeback(vector<pair<Line, int>> &temp) {
hull.pop_back();
for (int i = (int)temp.size()-1; i >= 0; i--){
hull.pb(temp[i]);
}
}
ll getval(ll x) {
int k = 0;
for (int i = (int)hull.size(); i; i >>= 1) {
while (k+i < (int)hull.size() && hull[k+i].se < x) k += i;
}
return hull[k].fi.cal(x);
}
} cht;
const int maxn = 1e5+5;
int n;
vector<pair<int, ll>> a[maxn];
ll b[maxn], s[maxn], dp[maxn], ps[maxn];
void dfs(int u, int p) {
if (p > -1) {
//cal
dp[u] = s[u]+b[u]*ps[u]+cht.getval(b[u]);
}
vector<pair<Line, int>> temp; //temp
cht.update(Line(-ps[u], dp[u]), temp);
for (int i = 0; i < (int)a[u].size(); i++) {
int v = a[u][i].fi;
ll w = a[u][i].se;
if (v == p) continue;
ps[v] = ps[u] + w;
dfs(v, u);
}
cht.placeback(temp);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#define task "harbingers"
if (fopen(task".in", "r")){
freopen(task".in", "r", stdin);
freopen(task".out", "w", stdout);
}
dp[1] = ps[1] = 0;
cin >> n;
for (int i= 1; i < n; i++) {
int u, v;
ll w;
cin >> u >> v >> w;
a[u].pb({v, w});
a[v].pb({u, w});
}
for (int i = 2; i <= n; i++) {
cin >> s[i] >> b[i];
}
dfs(1, -1);
for (int i = 2; i <= n; i++) {
cout << dp[i] << ' ';
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |