답안 #964356

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
964356 2024-04-16T17:44:17 Z sheldon Building Bridges (CEOI17_building) C++14
100 / 100
55 ms 5712 KB
#include <bits/stdc++.h>

using namespace std;

struct line {
    long long m, b;

    long long eval (int x) {
        return m * x + b;
    }
};

struct Node {
    line ln;

    Node *lchild, *rchild;
    Node (line _l) : ln (_l) , lchild (nullptr), rchild (nullptr) {}
};

void insert (Node *node, int l, int r, line lnn) {
    if (l == r) {
        if (node->ln.eval (l) > lnn.eval (l)) {
            node->ln = lnn;
        }
        return;
    }

    int mid = (l + r) / 2;
    if (node->ln.m > lnn.m) {
        swap (node->ln, lnn);
    }
    if (node->ln.eval (mid) > lnn.eval (mid)) {
        swap (node->ln, lnn);

        if (!node->rchild) {
            node->rchild = new Node (lnn);
        } else
        insert (node->rchild, mid + 1, r, lnn);
    } else {
        if (!node->lchild) {
            node->lchild = new Node (lnn);
        } else
        insert (node->lchild, l, mid, lnn);
    }
}


long long query (Node *node, int l, int r, int x) {
    if (l == r) {
        return node->ln.eval (l);
    }

    int mid = (l + r) / 2;

    if (x <= mid) {
        if (node->lchild) {
            return min (node->ln.eval (x), query (node->lchild, l, mid, x));
        }
    } else {
        if (node->rchild) {
            return min (node->ln.eval (x), query (node->rchild, mid + 1, r, x));
        }
    }
    return node->ln.eval (x);
}


void solve() {
    int n;
    cin >> n;
    vector<long long> h (n);
    for (int i = 0; i < n; ++i) {
        cin >> h[i];
    }
    int we;
    cin >> we;
    line li = line {-2 * h[0], -we + h[0] * h[0]};
    Node *root = new Node (li);
    long long pref = we;
    long long ans = 2e18;
    for (int i = 1; i < n; ++i) {
        cin >> we;
        pref += we;
        ans = 1LL * h[i] * h[i] + pref - we + query (root, (int) 0, (int) 1e6, h[i]);
        insert (root, 0, 1e6, line {-2 * h[i], h[i] * h[i] - pref + ans}); 
    }
    cout << ans;
}   

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 2024 KB Output is correct
2 Correct 35 ms 1904 KB Output is correct
3 Correct 35 ms 1880 KB Output is correct
4 Correct 30 ms 1372 KB Output is correct
5 Correct 30 ms 4828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 36 ms 2024 KB Output is correct
7 Correct 35 ms 1904 KB Output is correct
8 Correct 35 ms 1880 KB Output is correct
9 Correct 30 ms 1372 KB Output is correct
10 Correct 30 ms 4828 KB Output is correct
11 Correct 55 ms 5164 KB Output is correct
12 Correct 50 ms 5200 KB Output is correct
13 Correct 34 ms 2064 KB Output is correct
14 Correct 44 ms 5204 KB Output is correct
15 Correct 32 ms 5712 KB Output is correct
16 Correct 26 ms 4956 KB Output is correct
17 Correct 21 ms 1384 KB Output is correct
18 Correct 21 ms 2260 KB Output is correct