Submission #490121

# Submission time Handle Problem Language Result Execution time Memory
490121 2021-11-25T19:31:49 Z macktvz Harbingers (CEOI09_harbingers) C++17
70 / 100
1000 ms 21824 KB
#include <bits/stdc++.h>
using namespace std;

struct line {
    long long a,b;
    double intersectX(line l) {return (b-l.b)/(l.a-a);}
};
const int N = 1e5+1;
stack<pair<int,int>> ch;
vector<int> id(N);
deque<int> slop;
line l[N];
long long dp[N];
vector<pair<int,int>> adj[N];
long long A[N];
long long B[N];

void rollback(int i) {
    while (ch.top().first != i || ch.top().second != 1) {
        auto op = ch.top();
        ch.pop();
        if (op.second == 1) {
            slop.pop_back();
        } else {
            slop.push_back(op.first);
        }
    }
}

long double intersect(int i, int j) {
    if (l[i].a > l[j].a) swap(i,j);
    return (l[i].b-l[j].b)/(l[j].a-l[i].a);
}

bool cmp(int idx, long long a) {
    return intersect(slop[idx],slop[idx+1]) < a;
}

void insert(int idx) {
    while (slop.size() > 1 && intersect(slop[slop.size()-2],slop.back()) >= intersect(slop.back(),idx)) {
        ch.push({slop.back(),-1});
        slop.pop_back();
    }
    ch.push({idx,1});
    slop.push_back(idx);
}

void dfs(int i, int p,long long curr) {
    if (i != 0) {
        int j = slop[*lower_bound(id.begin(),id.begin()+slop.size()-1,A[i],cmp)];
        dp[i] = curr*A[i] + B[i] -(l[j].a*A[i] + l[j].b);
    }
    l[i].a = curr;
    l[i].b = -dp[i];
    insert(i);
    for(pair<int,int> u : adj[i]) {
        if (u.first != p) {
            dfs(u.first,i,curr+u.second);
            rollback(i);
        }
    }
}

int main() {
    iota(id.begin(),id.end(),0);
    int n; cin >> n;
    int a,b,c;
    dp[0]=0;
    for(int i = 1; i < n; i++) {
        cin >> a >> b >>c;
        --a; --b;
        adj[a].push_back({b,c});
        adj[b].push_back({a,c});
    }
    for(int i = 1; i< n; i++) {
        cin >> B[i] >> A[i];
    }
    dfs(0,-1,0);
    for(int i = 1; i < n; i++) {
        cout << dp[i] << endl;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3020 KB Output is correct
2 Correct 7 ms 3532 KB Output is correct
3 Correct 129 ms 10832 KB Output is correct
4 Correct 170 ms 14500 KB Output is correct
5 Correct 230 ms 18228 KB Output is correct
6 Correct 306 ms 21824 KB Output is correct
7 Correct 180 ms 11524 KB Output is correct
8 Execution timed out 1100 ms 14540 KB Time limit exceeded
9 Execution timed out 1094 ms 12472 KB Time limit exceeded
10 Execution timed out 1090 ms 15908 KB Time limit exceeded