Submission #546992

# Submission time Handle Problem Language Result Execution time Memory
546992 2022-04-09T06:01:10 Z Jomnoi Harbingers (CEOI09_harbingers) C++17
0 / 100
72 ms 27388 KB
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;

const int MAX_N = 1e5 + 10;
const long long INF = LLONG_MAX;

bool QueryMode;
long long S[MAX_N], V[MAX_N];
vector <pair <int, int>> adj[MAX_N];
long long dist[MAX_N], ans[MAX_N];

long long divide(const long long &a, const long long &b) {
    return a / b - ((a ^ b) < 0 and a % b != 0);
}

class Line {
public :
    mutable long long m, c, p;
    Line() {}
    Line(const long long &m_, const long long &c_) : m(m_), c(c_), p(0) {}
    Line(const long long &m_, const long long &c_, const long long &p_) : m(m_), c(c_), p(p_) {}

    bool operator<(const Line &o) const {
        if(QueryMode == true) {
            return p < o.p;
        }
        return m < o.m;
    }
};

class LineContainer : multiset <Line> {
public :
    bool intersect(iterator now, iterator nxt) {
        if(nxt == end()) {
            now->p = INF;
            return false;
        }

        if(now->m == nxt->m) {
            now->p = INF;
            if(now->c < nxt->c) {
                now->p = -INF;
            }
        }
        else {
            now->p = divide(nxt->c - now->c, now->m - nxt->m);
        }
        return now->p >= nxt->p;
    }

    void addLine(const Line &f) {
        auto nxt = insert(f), now = nxt++, prv = now;
        while(intersect(now, nxt) == true) {
            nxt = erase(nxt);
        }
        if(prv != begin() and intersect(--prv, now) == true) {
            intersect(prv, now = erase(now));
        }
        while((now = prv) != begin() and (--prv)->p >= now->p) {
            intersect(prv, erase(now));
        }
    }

    long long query(const long long &x) {
        if(empty()) {
            return 0;
        }

        QueryMode = true;
        auto it = *lower_bound(Line(-INF, -INF, x));
        QueryMode = false;

        return it.m * x + it.c;
    }
}cht[MAX_N];

void dfs(const int &u, const int &p) {
    cht[u] = cht[p];
    ans[u] = -cht[u].query(V[u]) + dist[u] * V[u] + S[u];
    cht[u].addLine(Line(dist[u], -ans[u]));

    for(auto [v, w] : adj[u]) {
        if(v == p) {
            continue;
        }

        dist[v] = dist[u] + w;
        dfs(v, u);
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n;
    cin >> n;
    for(int i = 1; i <= n - 1; i++) {
        int u, v, d;
        cin >> u >> v >> d;
        adj[u].emplace_back(v, d);
        adj[v].emplace_back(u, d);
    }
    for(int i = 2; i <= n; i++) {
        cin >> S[i] >> V[i];
    }

    dfs(1, -1);

    for(int i = 2; i <= n; i++) {
        cout << ans[i] << ' ';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 14720 KB Execution killed with signal 11
2 Runtime error 12 ms 14964 KB Execution killed with signal 11
3 Runtime error 33 ms 19820 KB Execution killed with signal 11
4 Runtime error 49 ms 22012 KB Execution killed with signal 11
5 Runtime error 60 ms 24532 KB Execution killed with signal 11
6 Runtime error 68 ms 26804 KB Execution killed with signal 11
7 Runtime error 49 ms 23268 KB Execution killed with signal 11
8 Runtime error 72 ms 27092 KB Execution killed with signal 11
9 Runtime error 72 ms 27388 KB Execution killed with signal 11
10 Runtime error 59 ms 27092 KB Execution killed with signal 11