Submission #1246375

#TimeUsernameProblemLanguageResultExecution timeMemory
1246375kiennguyendinhBuilding Bridges (CEOI17_building)C++20
100 / 100
58 ms65352 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100001;
const int MAXX = 1000000;
const int TREE_SIZE = 4 * MAXX;

struct Line {
    long long a, b;
    long long calc(long long x) {
        return a * x + b;
    }
    long long slope() {
        return a;
    }
};

struct Lichao {
    Line tr[TREE_SIZE];

    void build() {
        for (int i = 0; i < TREE_SIZE; ++i) {
            tr[i] = {0, (long long)1e18};
        }
    }

    void update(Line f, int id, int l, int r) {
        if (l == r) {
            if (f.calc(l) < tr[id].calc(l)) tr[id] = f;
            return;
        }
        int mid = (l + r) >> 1;
        if (f.calc(mid) < tr[id].calc(mid)) swap(f, tr[id]);
        if (f.slope() > tr[id].slope()) update(f, id << 1, l, mid);
        else update(f, id << 1 | 1, mid + 1, r);
    }

    long long query(int id, int l, int r, int pos) {
        long long res = tr[id].calc(pos);
        if (l == r) return res;
        int mid = (l + r) >> 1;
        if (pos <= mid)
            return min(res, query(id << 1, l, mid, pos));
        else
            return min(res, query(id << 1 | 1, mid + 1, r, pos));
    }
};

Lichao lc;
int n;
long long a[MAXN][2], dp[MAXN], sum = 0;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i][0];
    for (int i = 1; i <= n; ++i) {
        cin >> a[i][1];
        sum += a[i][1];
    }

    lc.build();

    dp[1] = -a[1][1];
    Line init = {-2 * a[1][0], dp[1] + a[1][0] * a[1][0]};
    lc.update(init, 1, 1, MAXX);

    for (int i = 2; i <= n; ++i) {
        long long val = lc.query(1, 1, MAXX, a[i][0]);
        dp[i] = val - a[i][1] + a[i][0] * a[i][0];

        Line ne = {-2 * a[i][0], dp[i] + a[i][0] * a[i][0]};
        lc.update(ne, 1, 1, MAXX);
    }

    cout << dp[n] + sum << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...