답안 #474806

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
474806 2021-09-19T23:24:29 Z aniervs Harbingers (CEOI09_harbingers) C++17
60 / 100
1000 ms 22392 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 +(point a, point b){ return {a.x + b.x, a.y + b.y}; }
point operator -(point a, 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(point a, point b, point c){ return cross(b - a, c - a); }

struct hull : vector<point>{
    void add_point(point p){ // p is bigger 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(point p){
        assert(!empty());
        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,ll>> G[maxn];
ll speed[maxn], start[maxn], dp[maxn], sumW[maxn];
int n, sz[maxn];

point get_point(int u){
    return {sumW[u], -dp[u]};
}


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


vector<hull> lights;
hull heavy;

void dfsHLD(int u, int p){

    assert(lights.size() <= 20);

    if(u != 1){
        dp[u] = -1e18;
        for(auto h:lights){
            dp[u] = max(dp[u], h.max_dot({speed[u], 1}));
        }

        if(!heavy.empty())
            dp[u] = max(dp[u], heavy.max_dot({speed[u], 1}));
        dp[u] = sumW[u] * speed[u] + start[u] - dp[u];
    }

    heavy.add_point(get_point(u));
    
/*
    dp[v] = LLONG_MAX;
    for(int p:ancestors)
        dp[v] = min(dp[v], dot({sumW[p], dp[p]}, {-speed[v], 1});
    dp[v] += sumW[v] * speed[v];
    dp[v] += start[v];
*/

    int bigChild = -1;
    for(auto [v, w]:G[u]){
        if(v == p) continue;
        if(bigChild == -1 or sz[bigChild] < sz[v])
            bigChild = v;
    }
    lights.push_back(heavy);
        
    for(auto [v, w]:G[u])
        if(v != bigChild and v != p){
            heavy.clear();
            dfsHLD(v, u);
        }
    heavy = lights.back();
    lights.pop_back();

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

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,-1);

    dfsHLD(1,-1);

    for(int i = 2; i <= n; i++)
        cout << dp[i] << ' ';
    
    return 0;
}



/*

5
1 2 20
2 3 12
2 4 1
4 5 3
26 9
1 10
500 2
2 30

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 5 ms 5452 KB Output is correct
3 Correct 273 ms 12404 KB Output is correct
4 Execution timed out 1089 ms 15916 KB Time limit exceeded
5 Correct 131 ms 19012 KB Output is correct
6 Correct 128 ms 22392 KB Output is correct
7 Correct 73 ms 13608 KB Output is correct
8 Execution timed out 1081 ms 18084 KB Time limit exceeded
9 Execution timed out 1081 ms 19660 KB Time limit exceeded
10 Execution timed out 1073 ms 18684 KB Time limit exceeded