# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
767933 | hafo | Harbingers (CEOI09_harbingers) | C++14 | 82 ms | 26436 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>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 7;
const ll oo = 1e18 + 69;
int n, s[maxn], p[maxn], u, v, w;
vector<pa> g[maxn];
ll dp[maxn], f[maxn];
struct line {
ll m, b;
};
double inter(line &c, line &d) {
return (double) (d.b - c.b) / (double) (c.m - d.m);
}
struct cht {
struct opr {
line ow;
int sz, pos;
};
stack<opr> undo_list;
line lines[maxn];
int sz = 1;
bool bad(line &a, line &b, line &c) {
return inter(a, c) > inter(a, b);
}
ll calc(line a, ll x) {
return a.m * x + a.b;
}
ll query(ll x) {
ll res = 0;
int l = 0, r = sz - 1, mid;
while(l < r) {
mid = l + r >> 1;
ll s1 = calc(lines[mid], x);
ll s2 = calc(lines[mid + 1], x);
mini(res, min({s1, s2}));
if(s1 < s2) {
r = mid;
} else {
l = mid + 1;
}
}
return res;
}
void add(line new_line) {
int l = 1, r = sz - 1, mid, res = sz;
while(l <= r) {
mid = l + r >> 1;
if(bad(lines[mid - 1], lines[mid], new_line)) r = mid - 1;
else {
res = mid;
l = mid + 1;
}
}
undo_list.push({lines[res], sz, res});
lines[res] = new_line;
sz = res + 1;
}
void undo() {
auto top = undo_list.top();
undo_list.pop();
lines[top.pos] = top.ow;
sz = top.sz;
}
} cht;
void dfs(int u, int par) {
for(auto e:g[u]) {
int v = e.fi, w = e.se;
if(v == par) continue;
f[v] = f[u] + w;
dp[v] = f[v] * s[v] + p[v] + cht.query(s[v]);
cht.add({-f[v], dp[v]});
dfs(v, u);
cht.undo();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(TASK".inp", "r", stdin);
//freopen(TASK".out", "w", stdout);
cin>>n;
for(int i = 1; i < n; i++) {
cin>>u>>v>>w;
g[u].pb({v, w});
g[v].pb({u, w});
}
for(int i = 2; i <= n; i++) cin>>p[i]>>s[i];
cht.lines[0] = {0, 0};
dfs(1, 0);
for(int i = 2; i <= n; i++) cout<<dp[i]<<" ";
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |