답안 #964355

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

using namespace std;

struct line {
    long long m, b;

    long long eval (long long 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) -1e9, (int) 1e9, h[i]);
        insert (root, (int) -1e9, (int) 1e9, 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 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 3028 KB Output is correct
2 Correct 44 ms 2900 KB Output is correct
3 Correct 49 ms 3032 KB Output is correct
4 Correct 38 ms 2392 KB Output is correct
5 Correct 38 ms 6472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 52 ms 3028 KB Output is correct
7 Correct 44 ms 2900 KB Output is correct
8 Correct 49 ms 3032 KB Output is correct
9 Correct 38 ms 2392 KB Output is correct
10 Correct 38 ms 6472 KB Output is correct
11 Correct 56 ms 6428 KB Output is correct
12 Correct 52 ms 6228 KB Output is correct
13 Correct 48 ms 3404 KB Output is correct
14 Correct 54 ms 6364 KB Output is correct
15 Correct 29 ms 6732 KB Output is correct
16 Correct 32 ms 6520 KB Output is correct
17 Execution timed out 3055 ms 4440 KB Time limit exceeded
18 Halted 0 ms 0 KB -