Submission #490123

# Submission time Handle Problem Language Result Execution time Memory
490123 2021-11-25T19:32:56 Z macktvz Harbingers (CEOI09_harbingers) C++17
70 / 100
1000 ms 21176 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];
int A[N];
int 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() {
    iostream::sync_with_stdio(false);
	cin.tie(0);
    int n; cin >> n;
    iota(id.begin(),id.begin()+n,0);
    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 6 ms 3532 KB Output is correct
3 Correct 86 ms 10640 KB Output is correct
4 Correct 118 ms 14120 KB Output is correct
5 Correct 159 ms 17664 KB Output is correct
6 Correct 205 ms 21176 KB Output is correct
7 Correct 124 ms 11096 KB Output is correct
8 Execution timed out 1081 ms 13812 KB Time limit exceeded
9 Execution timed out 1085 ms 11676 KB Time limit exceeded
10 Execution timed out 1099 ms 15168 KB Time limit exceeded