# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
526501 | kaxzert | Harbingers (CEOI09_harbingers) | C++17 | 153 ms | 46164 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;
#define fto(i, a, b) for(int i = a; i <= b; ++i)
#define fdto(i, a, b) for(int i = a; i >= b; --i)
#define bugarr(a, i, j) cout << #a << "{" << i << "..." << j << "}:"; fto(k, i, j-1) cout << a[k] << ", "; cout << a[j] << endl;
#define ll long long
#define db double
#define ldb long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define vt vector
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define trav(i, a) for(auto &i : a)
#define sz(a) (int)a.size()
#define pi(a, b) pair<a, b>
#define fast ios::sync_with_stdio(false); cin.tie(0)
void setIO(string s) {
if (sz(s) != 0) {
freopen((s+".inp").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
}
void setIOusaco(string s) {
if (sz(s) != 0) {
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
}
template<typename T, typename V>
bool ckmin(T &a, V b) {return (b < a)? a = b, true : false;}
template<typename T, typename V>
bool ckmax(T &a, V b) {return (b > a)? a = b, true : false;}
void print(int x) {cout << x;}
void print(long long x) {cout << x;}
void print(unsigned x) {cout << x;}
void print(unsigned long long x) {cout << x;}
void print(double x) {cout << fixed << x;}
void print(long double x) {cout << fixed << x;}
void print(char x) {cout << "'" << x << "'";}
void print(string x) {cout << '"' << x << '"';}
void print(bool x) {cout << (x ? "true" : "false");}
template<typename T, typename V>
void print(const pair<T, V> &x) {cout << '{'; print(x.ff); cout << ", "; print(x.ss); cout << '}';}
template<typename T>
void print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? ", " : ""), print(i); cout << "}";}
void _print() {cout << "]" << endl;}
template <typename T, typename... V>
void _print(T t, V... v) {print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#ifdef TAP
#define bug(x...) cout << "[" << #x << "] = ["; _print(x)
#else
#define bug(x...)
#endif
struct cht {
struct line {
ll a, b;
line(ll a, ll b):a(a), b(b){}
ll intersect(line o) {
ll x = o.b - b;
ll y = a - o.a;
return (x+y-1)/y;
}
ll val(ll x) {
return a*x + b;
}
};
deque<pair<pair<line, ll>, int> > dq;
void insert(ll a, ll b, int pos) {
line newline(a, b);
while(!dq.empty() && dq.back().ff.ss >= dq.back().ff.ff.intersect(newline)) {
dq.pop_back();
}
if (dq.empty()) {
dq.pb({{newline, 0}, pos});
}else dq.pb({{newline, -dq.back().ff.ff.intersect(newline)}, pos});
}
ll query2(ll x) {
auto pos = *lower_bound(rall(dq), mp(mp(line(0, 0), -x), 0), [=](pair<pair<line, ll>, int> a, pair<pair<line, ll>, int> b) {
return a.ff.ss < b.ff.ss;
});
return pos.ff.ff.val(x);
}
};
const int maxN = 1000008;
ll s[maxN], f[maxN], v[maxN];
vt<pi(int, ll)> ke[maxN], ans;
cht lines;
void dfs(int u, ll d = 0, int p = 0) {
if (u != 1) {
f[u] = lines.query2(v[u])+d*v[u]+s[u];
}
lines.insert(-d, f[u], u);
trav(v, ke[u]) {
if (v.ff == p) continue;
dfs(v.ff, d+v.ss, u);
}
if (lines.dq.back().ss == u) lines.dq.pop_back();
}
int main() {
#ifndef TAP
setIO("");
//setIOusaco("harbingers");
#endif
fast;
int n;
cin >> n;
fto(i, 1, n-1) {
int u, v, w;
cin >> u >> v >> w;
ke[u].eb(v, w);
ke[v].eb(u, w);
}
fto(i, 1, n) f[i] = LLONG_MAX;
f[1] = 0;
fto(i, 1, n-1) {
cin >> s[i+1] >> v[i+1];
}
dfs(1);
fto(i, 2, n) cout << f[i] << " \n"[i == n];
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |