Submission #1235302

#TimeUsernameProblemLanguageResultExecution timeMemory
1235302blackslexHarbingers (CEOI09_harbingers)C++20
30 / 100
146 ms131072 KiB
#include<bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;

const int N = 1e5 + 5;
int n, x, y, z, s[N], t[N];
ll dp[N];
vector<pii> v[N];
bool qrm;

struct line {
    ll m, c;
    mutable ll p;
    line(ll m, ll c) : m(m), c(c) {}
    line(ll p) : p(p) {}
    friend bool operator < (const line &l1, const line &l2) {
        return (qrm ? l1.p < l2.p : l1.m > l2.m);
    }
};

struct convex:multiset<line> {
    ll div (ll a, ll b) {
        return (a / b) - ((a ^ b) < 0 && a % b);
    }
    bool ck (iterator x, iterator y) {
        if (y == end()) return x->p = 1e18, 0;
        if (x->m == y->m) x->p = x->c <= y->c ? 1e18 : -1e18;
        else x->p = div(x->c - y->c, y->m - x->m);
        return x->p >= y->p;
    }
    void add (ll m, ll c) {
        auto y = insert(line(m, c)), x = y++;
        while (ck(x, y)) y = erase(y);
        if ((y = x) != begin() && ck(--x, y)) ck(x, erase(y));
        while ((y = x) != begin() && (--x)->p >= y->p) ck(x, erase(y));
    }
    ll qr (ll x) {
        if (empty()) return 1e18;
        qrm = 1; auto idx = lower_bound(x); qrm = 0;
        return idx->m * x + idx->c;
    }
};

void dfs (int cur, int par, ll dist, convex hull){
    if (cur != 1) {
        dp[cur] = hull.qr(t[cur]) + s[cur] + dist * t[cur];
    }
    hull.add(-dist, dp[cur]);
    for (auto &[x, y]: v[cur]) {
        if (par == x) continue;
        dfs(x, cur, dist + y, hull);
    }
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i < n; i++) {
        scanf("%d %d %d", &x, &y, &z);
        v[x].emplace_back(y, z);
        v[y].emplace_back(x, z);
    }
    for (int i = 2; i <= n; i++) scanf("%d %d", &s[i], &t[i]);
    dp[1] = 0;
    convex hull;
    dfs(1, 0, 0, hull);
    for (int i = 2; i <= n; i++) printf("%lld ", dp[i]);
}

Compilation message (stderr)

harbingers.cpp: In function 'int main()':
harbingers.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
harbingers.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |         scanf("%d %d %d", &x, &y, &z);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp:64:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |     for (int i = 2; i <= n; i++) scanf("%d %d", &s[i], &t[i]);
      |                                  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...