Submission #1014890

# Submission time Handle Problem Language Result Execution time Memory
1014890 2024-07-05T16:41:21 Z ByeWorld Building Bridges (CEOI17_building) C++14
100 / 100
291 ms 12884 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
#define ll long long
#define int long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)/2)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii, int> ipii;
const int MAXN = 1e5+10;
const int MAXA = 1e6+10;
const int INF = 1e18+10;
const int INF2 = 1e18+10;
const int LOG = 19;
const int MOD = 1e9+7;
const int SQRT = 450;
const vector<int> NOL = {};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const vector<int> dx = {1, -1, 0, 0};
const vector<int> dy = {0, 0, 1, -1};
void chmn(int &a, int b){ a = min(a, b); }
int n;
int h[MAXN], w[MAXN], pr[MAXN], dp[MAXN];

struct line {
    int m, c;
    int y(int x){ return m*x+c; }
};

struct seg {
    seg *le, *ri; line val;
    int l, r, mid;

    void bd(int x, int y){
        l = x; r = y; mid = md;
        val.m = 0; val.c = INF2;
        le = NULL; ri = NULL;
    }
    void ADD(line add){
        if(add.c==INF2) return;

        if(val.y(mid) > add.y(mid)) swap(val, add);
        if(val.m == add.m) return; // kalah
        if(val.m < add.m){ 
            if(le==NULL){ le = new seg(); le->bd(l, mid); }
            le->ADD(add); return; 
        }
            if(ri==NULL){ ri = new seg(); ri->bd(mid+1, r); }
            ri->ADD(add); return; 
    }
    void upd(int x, int y, line add){
        if(x<=l && r<=y){ ADD(add); return; }
        if(r<x || y<l) return;

        if(le==NULL){ le = new seg(); le->bd(l, mid); }
        le->upd(x, y, add);
        if(ri==NULL){ ri = new seg(); ri->bd(mid+1, r); }
        ri->upd(x, y, add);
    }
    int que(int x){
        int te = val.y(x);
        if(x < mid){
            if(le!=NULL) chmn(te, le->que(x));
        } else {
            if(ri!=NULL) chmn(te, ri->que(x));
        }
        return te;
    }
} *A;

signed main(){
    cin >> n;
    for(int i=1; i<=n; i++) cin >> h[i];
    for(int i=1; i<=n; i++){
        cin >> w[i]; pr[i] = pr[i-1]+w[i];
    }
    A = new seg(); A->bd(0, MAXA);
    dp[1] = 0;
    int i = 1;
    line nw; 
    nw.m = -2*h[i]; 
    nw.c = dp[i] - pr[i] + h[i]*h[i];
    // cout << "pp\n";
    A->upd(0, MAXA, nw);

    for(int i=2; i<=n; i++){
        dp[i] = h[i]*h[i] + pr[i-1];
        dp[i] += A->que(h[i]);

        line nw; 
        nw.m = -2*h[i]; 
        nw.c = dp[i] - pr[i] + h[i]*h[i]; // 1e18
        A->upd(0, MAXA, nw);
        // cout << i<< ' ' << dp[i] << " pp\n";
        // for(int j=1; j<=i-1; j++){
        //     int te = dp[j] + pr[i-1]-pr[j] + (h[j]-h[i])*(h[j]-h[i]);
        //     chmn(dp[i], te);
        // }
    }
    cout << dp[n] << '\n';
}
/*
Rumus:
dp[i] = min:
(dp[j]-pr[j]+hj^2 +  hi -2hj) + hi^2 + pr[i-1]
( c                   x   m ) +  k
m gk monotonic -> dynamic CHT
6
3 8 7 1 6 6
0 -1 9 1 2 0
->17
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 9900 KB Output is correct
2 Correct 72 ms 9808 KB Output is correct
3 Correct 67 ms 10008 KB Output is correct
4 Correct 61 ms 8272 KB Output is correct
5 Correct 291 ms 12136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 66 ms 9900 KB Output is correct
7 Correct 72 ms 9808 KB Output is correct
8 Correct 67 ms 10008 KB Output is correct
9 Correct 61 ms 8272 KB Output is correct
10 Correct 291 ms 12136 KB Output is correct
11 Correct 70 ms 12112 KB Output is correct
12 Correct 61 ms 12640 KB Output is correct
13 Correct 69 ms 12116 KB Output is correct
14 Correct 66 ms 12884 KB Output is correct
15 Correct 37 ms 12884 KB Output is correct
16 Correct 286 ms 12112 KB Output is correct
17 Correct 29 ms 3412 KB Output is correct
18 Correct 26 ms 3420 KB Output is correct