# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1104433 | BF001 | Harbingers (CEOI09_harbingers) | C++17 | 169 ms | 65536 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 N 100005
#define oo 1e18
#define int long long
#define fi first
#define se second
typedef pair<int, int> ii;
struct line{
int a, b;
line(){
a = 0;
b = oo;
}
line(int x, int y){
a = x;
b = y;
}
int operator ()(int x){
return a * x + b;
}
};
int n, dep[N], ma, v[N], s[N], dp[N];
line bit[4 * N];
stack<pair<int, line>> st;
vector<int> val;
vector<ii> adj[N];
void ud(int id, int l, int r, line nw){
if (l == r){
if (bit[id](val[l]) > nw(val[l])){
st.push({id, bit[id]});
bit[id] = nw;
}
return;
}
int mid = (l + r) >> 1;
if (bit[id].a > nw.a) {st.push({id, bit[id]}); swap(bit[id], nw);}
if (bit[id](val[mid]) > nw(val[mid])){
st.push({id, bit[id]});
swap(bit[id], nw);
ud(id * 2 + 1, l, mid, nw);
}
else ud(id * 2, l, mid, nw);
}
int get(int id, int l, int r, int pos){
if (l == r){
return bit[id](pos);
}
int mid = (l + r) >> 1;
if (pos <= val[mid]) return min(bit[id](pos), get(id * 2, l, mid, pos));
else return min(bit[id](pos), get(id * 2 + 1, mid + 1, r, pos));
}
void rollback(int si){
while ((int) st.size() > si){
int id = st.top().fi;
line tmp = st.top().se;
bit[id] = tmp;
st.pop();
}
}
void dfs(int u, int p){
if (u != 1) dp[u] = get(1, 0, ma, v[u]) + dep[u] * v[u] + s[u];
int si = (int) st.size();
ud(1, 0, ma, line(-dep[u], dp[u]));
for (auto x : adj[u]){
if (x.fi == p) continue;
dep[x.fi] = dep[u] + x.se;
dfs(x.fi, u);
}
rollback(si);
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n;
for (int i = 1; i <= n - 1; i++){
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
for (int i = 2; i <= n; i++){
cin >> s[i] >> v[i];
val.push_back(v[i]);
}
sort(val.begin(), val.end());
val.resize(unique(val.begin(), val.end()) - val.begin());
ma = (int) val.size() - 1;
dfs(1, 0);
for (int i = 2; i <= n; i++) cout << dp[i] << " ";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |