제출 #1161582

#제출 시각아이디문제언어결과실행 시간메모리
1161582spycoderytBuilding Bridges (CEOI17_building)C++20
100 / 100
33 ms7236 KiB
#include <bits/stdc++.h>
#define int long long 
using namespace std;
const int MX = 1e6+5;
const int MN = -MX;
struct Line {
    int m,b;
    int operator()(int x){return m * x + b;}
    Line(int x,int y) : m(x),b(y) {}
};
struct Node{
    Line line;
    Node *left, *right;
    Node(Line ln) : line(ln),left(nullptr),right(nullptr) {}
    void upd(Line nw,int l=MN,int r=MX) {
        if(l+1==r){
            if(nw(l) < line(l))swap(nw,line);
            return;
        }
        int mid = (l+r)/2;
        int ls = nw(l) < line(l);
        int ms = nw(mid) < line(mid);
        if(ms)swap(line,nw);
        if(ls!=ms){
            if(!left)left = new Node(nw);
            else left->upd(nw,l,mid);
        } else {
            if(!right)right = new Node(nw);
            else right->upd(nw,mid,r);
        }
    }  
    int qry(int x,int l=MN,int r=MX) {
        if(l+1==r)return line(x);
        int mid = (l+r)/2;
        if(x<mid) {
            if(left)return min(line(x),left->qry(x,l,mid));
        } else if (right) return min(line(x),right->qry(x,mid,r));
        return line(x);
    }
};
int32_t main() {
    cin.tie(0)->sync_with_stdio(0);
    int n;
    cin>>n;
    vector<int> h(n+1),pref(n+1),dp(n+1);
    for(int i = 1;i<=n;i++) cin >> h[i];
    for(int i = 1;i<=n;i++) cin >> pref[i], pref[i]+=pref[i-1];
    Line tmp = Line(-2*h[1], h[1] * h[1] - pref[1]);
    Node t = Node(tmp);
    for(int i = 2;i<=n;i++) {
        dp[i] = h[i] * h[i] + pref[i-1] + t.qry(h[i]);
        t.upd(Line(-2*h[i],dp[i] + h[i] * h[i] - pref[i]));
    }
    cout << dp[n];
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...