제출 #1045504

#제출 시각아이디문제언어결과실행 시간메모리
1045504phoenix0423Building Bridges (CEOI17_building)C++17
100 / 100
36 ms14424 KiB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
// #pragma GCC optimize("Ofast")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
#define lowbit(x) x&-x
const int maxn = 1e6 + 5;
const int INF = 4e18;
int n;
int h[maxn], w[maxn];

struct LiChaoTree{
    struct line{
        int m, k;
        line(){}
        line(int _m, int _k) : m(_m), k(_k){}
        int operator()(int x){
            return m * x + k;
        }
    };
    struct node{
        node *l, *r;
        line ans;
        node() : l(nullptr), r(nullptr), ans(line(0, INF)){}
        node(node *nd) : l(nd->l), r(nd->r), ans(nd->ans){}
        node(line _ans) : l(nullptr), r(nullptr), ans(_ans){}
    } *rt;
    void init(){ rt = new node();}
    void upd(line cur, node *&nd, int l, int r){
        if(!nd){
            nd = new node(cur);
            return;
        }
        if(l == r){
            if(cur(l) < nd->ans(l)) nd->ans = cur;
            return;
        }
        int m = (l + r) / 2;
        if(cur(m) < nd->ans(m)) swap(cur, nd->ans);
        if(cur.m < nd->ans.m) upd(cur, nd->r, m + 1, r);
        else upd(cur, nd->l, l, m);
    }
    void upd(line cur){ upd(cur, rt, 0, maxn - 1);}
    void upd(int m, int k){ upd(line(m, k), rt, 0, maxn - 1);}
    int qry(int pos, node *nd, int l, int r){
        if(!nd) return INF;
        if(l == r) return nd->ans(pos);
        int m = (l + r) / 2;
        if(pos <= m) return min(nd->ans(pos), qry(pos, nd->l, l, m));
        else return min(nd->ans(pos), qry(pos, nd->r, m + 1, r));
    }
    int qry(int val){ return qry(val, rt, 0, maxn - 1);}
} tree;

int dp[maxn];

signed main(void){
    fastio;
    cin>>n;
    for(int i = 0; i < n; i++) cin>>h[i];
    for(int i = 0; i < n; i++) cin>>w[i];
    tree.init();
    tree.upd(-2 * h[0], h[0] * h[0] - w[0]);
    for(int i = 1; i < n; i++){
        w[i] += w[i - 1];
        dp[i] = tree.qry(h[i]) + w[i - 1] + h[i] * h[i];
        tree.upd(-2 * h[i], dp[i] + h[i] * h[i] - w[i]);
    }
    cout<<dp[n - 1]<<"\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...