Submission #1166749

#TimeUsernameProblemLanguageResultExecution timeMemory
116674912345678Building Bridges (CEOI17_building)C++17
100 / 100
36 ms9104 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const ll nx=1e5+5, inf=1e18;

ll n, h[nx], w[nx], dp[nx], sm, querymode;

struct line
{
    ll m, c;
    mutable ll p;
    line(ll m, ll c, ll p): m(m), c(c), p(p){}
    bool operator<(const line &o) const {return querymode?p<o.p:m>o.m;}
};

struct convexhulltrick:multiset<line>
{
    ll div(ll a, ll b)
    {
        if ((a%b)==0||(a^b)>=0) return a/b;
        return a/b-1;
    }
    bool isect(iterator x, iterator y)
    {
        if (y==end()) return x->p=inf, 0;
        if (x->m==y->m) x->p=(x->c<=y->c)?inf:-inf;
        else x->p=div(x->c-y->c, y->m-x->m);
        return x->p>=y->p;
    }
    void add(ll m, ll c)
    {
        auto x=insert(line(m, c, 0)), y=next(x);
        while (isect(x, y)) y=erase(y);
        if ((y=x)!=begin()&&isect(--x, y)) isect(x, y=erase(y));
        while ((y=x)!=begin()&&isect(--x, y)) isect(x, y=erase(y));
    }
    ll query(ll x)
    {
        querymode=1;
        auto itr=lower_bound(line(0, 0, x));
        querymode=0;
        return itr->m*x+itr->c;
    }
} c;

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n;
    for (int i=1; i<=n; i++) cin>>h[i];
    for (int i=1; i<=n; i++) cin>>w[i], sm+=w[i];
    dp[1]=-w[1];
    for (int i=2; i<=n; i++)
    {
        c.add(2*h[i-1], h[i-1]*h[i-1]+dp[i-1]);
        dp[i]=c.query(-h[i])+h[i]*h[i]-w[i];
    }
    cout<<dp[n]+sm;
}

/*
6
3 8 7 1 6 6
0 -1 9 1 2 0
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...