# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
608640 | Do_you_copy | Harbingers (CEOI09_harbingers) | C++17 | 125 ms | 21132 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 taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000007;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 1e5 + 1;
int n;
vector <pii> adj[maxN];
int s[maxN], v[maxN];
pll cht[maxN];
int sz = 0;
int dp[maxN];
inline ld intersect(pll l, pll r){
return ld(r.se - l.se) / ld(l.fi - r.fi);
}
ll get(ll x){
int l = 0, r = sz - 1;
while (l < r){
int mid = (l + r) / 2;
if (intersect(cht[mid], cht[mid + 1]) < x) l = mid + 1;
else r = mid;
}
return cht[l].fi * x + cht[l].se;
}
int update(pll line){
if (sz < 2 || intersect(line, cht[sz - 1]) >= intersect(cht[sz - 1], cht[sz - 2])){
return sz;
}
int l = 1, r = sz - 1;
while (l < r){
int mid = (l + r) / 2;
if (intersect(line, cht[mid]) < intersect(cht[mid], cht[mid - 1])) r = mid;
else l = mid + 1;
}
return l;
}
void dfs(int u, int p, ll dis = 0){
int pre_size, pre_pos;
pll pre_line;
if (u == 1){
cht[sz++] = {0, 0};
}
else{
dp[u] = get(v[u]) + dis * v[u] + s[u];
pll line = {-dis, dp[u]};
pre_size = sz;
pre_pos = sz = update(line);
pre_line = cht[pre_pos];
cht[sz++] = line;
}
for (const auto &i: adj[u]){
if (i.fi == p) continue;
dfs(i.fi, u, i.se + dis);
}
if (u != 1){
sz = pre_size;
cht[pre_pos] = pre_line;
}
}
void Init(){
cin >> n;
for (int i = 1; i < n; ++i){
int u, v, w;
cin >> u >> v >> w;
adj[v].pb({u, w});
adj[u].pb({v, w});
}
for (int i = 2; i <= n; ++i){
cin >> s[i] >> v[i];
}
dfs(1, 0);
for (int i = 2; i <= n; ++i) cout << dp[i] << " ";
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |