Submission #1015972

# Submission time Handle Problem Language Result Execution time Memory
1015972 2024-07-07T07:26:28 Z thinknoexit Building Bridges (CEOI17_building) C++17
100 / 100
53 ms 23132 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 4e18;
ll h[100100], w[100100];
ll dp[100100];
ll qs[100100];
struct line {
    ll m, c;
    line() : m(0), c(4e18) {}
    line(ll m, ll c) : m(m), c(c) {}
    ll operator() (const ll& x) {
        return m * x + c;
    }
};
struct A {
    line w;
    A* l;
    A* r;
    A() : w(), l(nullptr), r(nullptr) {}
    A(ll m, ll c) : w(m, c), l(nullptr), r(nullptr) {}
};
using pA = A*;
pA root = new A();
void update(pA w, pA now = root, int l = 0, int r = 1000000) {
    bool cl = w->w(l) < now->w(l);
    bool cr = w->w(r) < now->w(r);
    if (!cl && !cr) return;
    if (cl && cr) {
        swap(now->w, w->w);
        return;
    }
    int mid = (l + r) >> 1;
    bool cm = w->w(mid) < now->w(mid);
    if (cm) {
        swap(now->w, w->w);
        if (!cl) update(w, now->l = (now->l) ? now->l : new A(), l, mid);
        else update(w, now->r = (now->r) ? now->r : new A(), mid, r);
    }
    else {
        if (cl) update(w, now->l = (now->l) ? now->l : new A(), l, mid);
        else update(w, now->r = (now->r) ? now->r : new A(), mid, r);
    }
}
ll query(int x, pA now = root, int l = 0, int r = 1000000) {
    if (l + 1 == r) return now->w(x);
    int mid = (l + r) >> 1;
    if (x < mid)
        return min(now->w(x), query(x, now->l = (now->l) ? now->l : new A(), l, mid));
    return min(now->w(x), query(x, now->r = (now->r) ? now->r : new A(), mid, r));
}
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 1;i <= n;i++) cin >> h[i];
    for (int i = 1;i <= n;i++) {
        cin >> w[i];
        qs[i] = qs[i - 1] + w[i];
    }
    // dp[i] = min(-2 * h[i] * h[j] + dp[j] - qs[j] + h[j] * h[j]) + qs[i-1] + h[i] * h[i]
    // m = -2 * h[j], c = dp[j] - qs[j] + h[j] * h[j]
    // x = h[i]
    dp[1] = 0;
    update(new A(-2 * h[1], dp[1] - qs[1] + h[1] * h[1]));
    for (int i = 2;i <= n;i++) {
        dp[i] = query(h[i]) + qs[i - 1] + h[i] * h[i];
        update(new A(-2 * h[i], dp[i] - qs[i] + h[i] * h[i]));
    }
    cout << dp[n];
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 9808 KB Output is correct
2 Correct 46 ms 9808 KB Output is correct
3 Correct 38 ms 9884 KB Output is correct
4 Correct 33 ms 9344 KB Output is correct
5 Correct 39 ms 19540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 37 ms 9808 KB Output is correct
7 Correct 46 ms 9808 KB Output is correct
8 Correct 38 ms 9884 KB Output is correct
9 Correct 33 ms 9344 KB Output is correct
10 Correct 39 ms 19540 KB Output is correct
11 Correct 53 ms 16724 KB Output is correct
12 Correct 46 ms 16464 KB Output is correct
13 Correct 29 ms 9388 KB Output is correct
14 Correct 51 ms 16724 KB Output is correct
15 Correct 39 ms 23132 KB Output is correct
16 Correct 37 ms 19540 KB Output is correct
17 Correct 19 ms 9308 KB Output is correct
18 Correct 19 ms 9308 KB Output is correct