Submission #883290

# Submission time Handle Problem Language Result Execution time Memory
883290 2023-12-05T04:53:55 Z anarch_y Harbingers (CEOI09_harbingers) C++17
70 / 100
1000 ms 27988 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define all(x) begin(x), end(x)
#define pb push_back
#define int long long
 
const int inf = 1e18;
 
struct line{
    int m, c;
    double p;
    int eval(int x){
        return m*x+c;
    }
};
 
struct CHT{
    vector<line> hull;
    stack<line> stk;
    double getx(int m1, int c1, int m2, int c2){
        return (double)(c1-c2)/(double)(m2-m1);
    }
    void addline(int m, int c){
        double p = -inf;
        while(!hull.empty()){
            line last = hull.back();
            p = getx(m, c, last.m, last.c);
            if(p<last.p){
                stk.push(last);
                hull.pop_back();
            }    
            else break;
        }
        stk.push((line){inf, inf, inf});
        hull.pb((line){m, c, p});
    }
    int best(int x){
        int k = 0;
        int L = 0, R = hull.size()-1;
        while(L<=R){
            int mid = (L+R)/2;
            if(hull[mid].p <= x){
                k = mid; L = mid+1;
            }
            else R = mid-1;
        }
        return hull[k].eval(x);
    }
    void rollback(){
        hull.pop_back();
        stk.pop();
        while(!stk.empty()){
            auto l = stk.top();
            if(l.m == inf) break;
            stk.pop();
            hull.pb(l);
        }
    }    
};
 
vector<pii> adj[100001];
int dist[100001], dp[100001];
int S[100001], F[100001];
CHT cht;
 
void dfs(int s, int e){
    for(auto pr: adj[s]){
        int u = pr.first;
        if(u == e) continue;
        dist[u] = dist[s] + pr.second;
        dp[u] = cht.best(F[u]) + S[u] + F[u]*dist[u];
        cht.addline(-dist[u], dp[u]);
        dfs(u, s);
        cht.rollback();
    }
}
 
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int N; cin >> N;
    for(int i=0; i<N-1; i++){
        int a, b, d; cin >> a >> b >> d;
        adj[a].pb({b, d}); adj[b].pb({a, d});
    }
    for(int i=2; i<=N; i++){
        cin >> S[i] >> F[i];
    }
    dist[1] = 0; dp[1] = 0;
    cht.addline(0, 0);
    dfs(1, 0);
    for(int i=2; i<=N; i++) cout << dp[i] << ' '; 
    
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5720 KB Output is correct
2 Correct 3 ms 6232 KB Output is correct
3 Correct 29 ms 15452 KB Output is correct
4 Correct 39 ms 19468 KB Output is correct
5 Correct 51 ms 24036 KB Output is correct
6 Correct 63 ms 27988 KB Output is correct
7 Correct 37 ms 14636 KB Output is correct
8 Execution timed out 1062 ms 19068 KB Time limit exceeded
9 Execution timed out 1059 ms 16912 KB Time limit exceeded
10 Execution timed out 1040 ms 20608 KB Time limit exceeded