Submission #474832

# Submission time Handle Problem Language Result Execution time Memory
474832 2021-09-20T02:47:43 Z aniervs Harbingers (CEOI09_harbingers) C++17
100 / 100
150 ms 25760 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = (int)2e5 + 5;

typedef pair<ll,ll> point;
#define x first
#define y second
point operator +(const point &a, const point &b){ return {a.x + b.x, a.y + b.y}; }
point operator -(const point &a, const point &b){ return {a.x - b.x, a.y - b.y}; }
ll dot(const point &a, const point &b){ return a.x * b.x + a.y * b.y; }
ll cross(const point &a, const point &b){ return a.x * b.y  - a.y * b.x; }
ll area2(const point &a, const point &b, const point &c){ return cross(b - a, c - a); }

struct hull : vector<point>{
    void add_point(const point &p){ // p is greater than all the previous ones
        while(size() > 1 and area2(at(size()-2), at(size()-1), p) >= 0)
            pop_back();
        push_back(p);
    }

    ll max_dot(const point &p){
        if(empty())
            return LLONG_MIN;
        int l = 0, r = (int)size() - 1;

        while(l < r){
            int mid = (l + r)>>1;
            if(dot(at(mid), p) <= dot(at(mid + 1), p))
                l = mid + 1;
            else
                r = mid;
        }

        return dot(at(l), p);
    }

};

vector<pair<int,int>> G[maxn];
ll speed[maxn], start[maxn], dp[maxn], sumW[maxn];
int n, sz[maxn], par[maxn];

void dfs(int u){
    sz[u] = 1;
    for(auto [v, w]:G[u]){
        if(v == par[u])
            continue;
        par[v] = u;
        sumW[v] = sumW[u] + w;
        dfs(v);
        sz[u] += sz[v];
    }
}

hull CH[maxn];


int topHeavy[maxn];

ll query(int u){
    const point p = {speed[u], 1};
    ll ans = LLONG_MIN;
    while(u){
        int top = topHeavy[u];
        ans = max(ans, CH[top].max_dot(p));
        u = par[top];
    }

    return ans;
}

void dfsHLD(int u, int topChain){

    topHeavy[u] = topChain;

    if(u != 1)
        dp[u] = sumW[u] * speed[u] + start[u] - query(u);

    CH[topChain].add_point({sumW[u], -dp[u]});

    int bigChild = -1;
    for(auto [v, _]:G[u])
        if(v != par[u] and (bigChild == -1 or sz[bigChild] < sz[v]))
            bigChild = v;
        
    for(auto [v, w]:G[u])
        if(v != bigChild and v != par[u])
            dfsHLD(v, v);
    

    if(bigChild != -1)
        dfsHLD(bigChild, topChain);
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);

    cin >> n;
    for(int i = 1; i < n; i++){
        int u, v, w;
        cin >> u >> v >> w;
        G[u].push_back({v, w});
        G[v].push_back({u, w});
    }

    for(int i = 2; i <= n; i++)
        cin >> start[i] >> speed[i];
    
    dfs(1);

    dfsHLD(1, 1);

    for(int i = 2; i <= n; i++)
        cout << dp[i] << ' ';   
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 8 ms 10060 KB Output is correct
3 Correct 52 ms 16676 KB Output is correct
4 Correct 75 ms 20028 KB Output is correct
5 Correct 103 ms 22680 KB Output is correct
6 Correct 126 ms 25760 KB Output is correct
7 Correct 72 ms 17892 KB Output is correct
8 Correct 150 ms 23716 KB Output is correct
9 Correct 140 ms 25496 KB Output is correct
10 Correct 123 ms 24640 KB Output is correct