# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
998508 | Neco_arc | Harbingers (CEOI09_harbingers) | C++17 | 70 ms | 26452 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>
//#include <bits/debug.hpp>
#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Harbingers"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 1e5 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
int n;
vector<pair<int, ll>> edges[maxn];
pair<ll, ll> a[maxn];
ll h[maxn], dp[maxn];
struct roll {
pair<ll, ll> val;
long double x;
int siz;
};
struct CHT {
int N = 0;
pair<ll, ll> P[maxn];
long double X[maxn];
long double inter(pair<ll, ll> x, pair<ll, ll> y) {
return 1. * (y.second - x.second) / (x.first - y.first);
}
void Change(roll x) {
P[N] = x.val, X[N] = x.x;
N = x.siz;
}
void watch() { fi(i, 1, N) cout << P[i].first << " " << P[i].second << '\n'; cout << '\n'; }
roll Add(pair<ll, ll> p) {
p.first *= -1, p.second *= -1;
int oldN = N, vt = 1;
if(N > 0) {
int l = 1, r = N;
while(l <= r) {
int mid = (l + r) >> 1;
if(inter(p, P[mid]) < X[mid]) r = mid - 1;
else l = mid + 1;
}
vt = l;
}
roll x = {P[vt], X[vt], oldN};
N = vt;
P[N] = p;
X[N] = N == 1 ? -1e18 : inter(p, P[N - 1]);
return x;
}
ll get(ll x) {
if(!N) return 1e18;
int it = upper_bound(X + 1, X + 1 + N, x) - X - 1;
return -(P[it].first * x + P[it].second);
}
} Cht;
void dfs(int u, int par) {
if(u != 1) dp[u] = h[u] * a[u].second + a[u].first + Cht.get(a[u].second);
roll x = Cht.Add({ -h[u], dp[u] });
for(auto [v, w] : edges[u]) if(v != par) {
h[v] = h[u] + w;
dfs(v, u);
}
Cht.Change(x);
}
void solve()
{
cin >> n;
fi(i, 1, n - 1) {
int x, y, w; cin >> x >> y >> w;
edges[x].push_back({y, w});
edges[y].push_back({x, w});
}
fi(i, 2, n) cin >> a[i].first >> a[i].second;
dfs(1, 0);
fi(i, 2, n) cout << dp[i] << " ";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |