# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
608643 | Do_you_copy | Harbingers (CEOI09_harbingers) | C++17 | 164 ms | 21948 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 <ll, ll>;
using pil = pair <ll, ll>;
using pli = pair <ll, ll>;
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 ll maxN = 1e5 + 1;
ll n;
vector <pii> adj[maxN];
ll s[maxN], v[maxN];
pll cht[maxN];
ll sz = 0;
ll dp[maxN];
inline ld llersect(pll l, pll r){
return ld(r.se - l.se) / ld(l.fi - r.fi);
}
ll get(ll x){
ll l = 0, r = sz - 1;
while (l < r){
ll mid = (l + r) / 2;
if (llersect(cht[mid], cht[mid + 1]) < x) l = mid + 1;
else r = mid;
}
return cht[l].fi * x + cht[l].se;
}
ll update(pll line){
if (sz < 2 || llersect(line, cht[sz - 1]) >= llersect(cht[sz - 1], cht[sz - 2])){
return sz;
}
ll l = 1, r = sz - 1;
while (l < r){
ll mid = (l + r) / 2;
if (llersect(line, cht[mid]) < llersect(cht[mid], cht[mid - 1])) r = mid;
else l = mid + 1;
}
return l;
}
void dfs(ll u, ll p, ll dis = 0){
ll 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 (ll i = 1; i < n; ++i){
ll u, v, w;
cin >> u >> v >> w;
adj[v].pb({u, w});
adj[u].pb({v, w});
}
for (ll i = 2; i <= n; ++i){
cin >> s[i] >> v[i];
}
dfs(1, 0);
for (ll i = 2; i <= n; ++i) cout << dp[i] << " ";
}
signed 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();
}
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |