Submission #660308

# Submission time Handle Problem Language Result Execution time Memory
660308 2022-11-21T15:05:35 Z leroycut Harbingers (CEOI09_harbingers) C++17
0 / 100
122 ms 23444 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 3 ms 4180 KB Output isn't correct
2 Incorrect 4 ms 4692 KB Output isn't correct
3 Incorrect 49 ms 12276 KB Output isn't correct
4 Incorrect 61 ms 16036 KB Output isn't correct
5 Incorrect 70 ms 19792 KB Output isn't correct
6 Incorrect 96 ms 23444 KB Output isn't correct
7 Incorrect 50 ms 12836 KB Output isn't correct
8 Incorrect 122 ms 17496 KB Output isn't correct
9 Incorrect 113 ms 19264 KB Output isn't correct
10 Incorrect 104 ms 17912 KB Output isn't correct