Submission #557240

#TimeUsernameProblemLanguageResultExecution timeMemory
557240CyberSleeperBuilding Bridges (CEOI17_building)C++17
100 / 100
72 ms66476 KiB
#include <bits/stdc++.h>
#define fastio      ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define debug(x)    cout << "Line " << __LINE__ << ", " << #x << " is " << x << endl
#define all(x)      x.begin(), x.end()
#define fi          first
#define se          second
#define mp          make_pair
#define pb          push_back
#define ll          long long
#define ull         unsigned long long
#define pll         pair<ll, ll>
#define pii         pair<ll, ll>
#define ld          long double
#define nl          '\n'
#define tb          '\t'
#define sp          ' '
#define sqr(x)      (x)*(x)
using namespace std;

const ll MX=100005, MOD=1000000007, BLOCK=327, INF=2e9+7;
const ll INFF=1000000000000000007;
const ld ERR=1e-6, pi=3.14159265358979323846;

ll N, H[MX], W[MX], DP[MX], sumW;

struct Seg{

    #define mi      (le+ri)/2
    #define idxl    (idx*2+1)
    #define idxr    (idx*2+2)

    ll x;
    struct line{
        ll m=0, c=INFF;
    }ST[40*MX], now;

    ll f(line g, ll xx){
        return g.m*xx+g.c;
    }
    void upd(ll idx, ll le, ll ri){
        bool lef=(f(now, le)<f(ST[idx], le)), mid=(f(now, mi)<f(ST[idx], mi));
        if(mid)
            swap(now, ST[idx]);
        if(le+1==ri)
            return;
        if(lef!=mid)
            upd(idxl, le, mi);
        else
            upd(idxr, mi,  ri);
    }
    ll que(ll idx, ll le, ll ri){
        if(le+1==ri)
            return f(ST[idx], x);
        if(x<=mi)
            return min(f(ST[idx], x), que(idxl, le, mi));
        return min(f(ST[idx], x), que(idxr, mi, ri));
    }

    void update(ll mm, ll cc){
        now.m=mm, now.c=cc;
        upd(0, 0, 1000000);
    }
    ll query(ll xx){
        x=xx;
        return que(0, 0, 1000000);
    }

}LCT;

int main(){
    fastio;
    cin >> N;
    for(ll i=1; i<=N; i++)
        cin >> H[i];
    for(ll i=1; i<=N; i++){
        cin >> W[i];
        sumW+=W[i];
    }
    DP[1]=sumW-W[1];
    LCT.update(-2ll*H[1], sqr(H[1])+DP[1]);
    for(ll i=2; i<=N; i++){
        DP[i]=sqr(H[i])-W[i]+LCT.query(H[i]);
        LCT.update(-2ll*H[i], sqr(H[i])+DP[i]);
    }
    cout << DP[N] << nl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...