Submission #660309

# Submission time Handle Problem Language Result Execution time Memory
660309 2022-11-21T15:07:49 Z leroycut Harbingers (CEOI09_harbingers) C++17
0 / 100
134 ms 23544 KB
#include <bits/stdc++.h>
 
using namespace std;
using ll = long long;
using ld = long double;
 
const int N = 1e5 + 3;
const ll INF = 2e12 + 3;
 
struct LINE{
    ll k = 0, b = 0;
    LINE(ll _k = 0, ll _b = 0) : k(_k), b(_b) {}
};
 
ld cross(LINE a, LINE b){
    return (ld)(b.b - a.b) / (ld)(a.k - b.k);
}
 
ll f(LINE a, ll x){
    return a.k * x + a.b;
}
 
int n, st_end = 0;
ll V[N], S[N], dp[N];
vector<pair<int, ll>> g[N];
LINE st[N];
 
ll min_val(ll x){
    int l = -1, r = st_end;
    while(r - l > 1){
        int m = (l + r) / 2;
        if(cross(st[m], st[m + 1]) <= x){
            l = m;
        }else{
            r = m;
        }
    }
    return f(st[l + 1], x);
}
 
int remove_el(LINE cur){
    if(st_end < 2 || cross(st[st_end - 1], cur) >= cross(st[st_end - 1], st[st_end - 2])){
        return st_end;
    }
 
    int l = 1, r = st_end - 1;
    while(r - l > 0){
        int m = (l + r) / 2;
        if(cross(st[m], cur) < cross(st[m], st[m - 1])){
            r = m;
        }else{
            l = m + 1;
        }
    }
    return l;
}
 
void dfs(int v, int p, ll d){
    int prev_pos, prev_st_end;
    LINE prev_line;
    if(p == 0){
        dp[v] = 0;
        st[st_end++] = {0, 0};
    }else{
        dp[v] = S[v] + d * V[v] + min_val(V[v]);
        LINE cur(-d, dp[v]);
        prev_st_end = st_end;
        prev_pos = st_end = remove_el(cur);
        prev_line = st[st_end];
        st[st_end++] = cur;
    }
 
    for(auto i : g[v]){
        if(i.first == p) continue;
        dfs(i.first, v, d + i.second);
    }
 
    if(p != 0){
        st[prev_pos] = prev_line;
        st_end = prev_st_end;
    }
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b, c;
        cin >> a >> b >> c;
        g[a].push_back({b, c});
        g[b].push_back({a, c});
    }
 
    for(int i = 2; i <= n; ++i){
        cin >> S[i] >> V[i];
    }
 
    dfs(1, 0, 0);
 
    for(int i = 2; i <= n; ++i){
        cout << dp[i] << " ";
    }
    cout << "\n";
}
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4180 KB Output isn't correct
2 Incorrect 5 ms 4692 KB Output isn't correct
3 Incorrect 63 ms 12172 KB Output isn't correct
4 Incorrect 63 ms 16076 KB Output isn't correct
5 Incorrect 78 ms 19868 KB Output isn't correct
6 Incorrect 95 ms 23544 KB Output isn't correct
7 Incorrect 54 ms 12852 KB Output isn't correct
8 Incorrect 119 ms 17440 KB Output isn't correct
9 Incorrect 134 ms 19272 KB Output isn't correct
10 Incorrect 91 ms 17892 KB Output isn't correct