Submission #1313566

#TimeUsernameProblemLanguageResultExecution timeMemory
1313566Zbyszek99Building Bridges (CEOI17_building)C++20
100 / 100
51 ms8856 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;

struct line
{
    ll a,b;
    ll operator()(ll x)
    {
        return a*x+b;
    }
};

struct lichao
{
    ll l = 0;
    ll r = 1e6;
    line f = {(ll)1e9,(ll)1e17};
    lichao* left = NULL;
    lichao* right = NULL;
    void add_line(line f2)
    {
        if(l > r) return;
        if(f.a == 1e9 && f.b == 1e17)
        {
            f = f2;
            return;
        }
        ll mid = (l+r)/2;
        if(f2(mid) < f(mid)) swap(f,f2);
        ll p1 = f2.b-f.b;
        ll p2 = f.a-f2.a;
        if(p2 == 0) return;
        if(p2 < 0)
        {
            p1 *= -1;
            p2 *= -1;
        }
        if(mid*p2 > p1 || (mid*p2 == p1 && f2.a > f.a))
        {
            if(left == NULL)
            {
                left = new lichao;
                left -> l = l;
                left -> r = mid-1;
            }
            left->add_line(f2);
        }
        else
        {
            if(right == NULL)
            {
                right = new lichao;
                right -> l = mid+1;
                right -> r = r;
            }
            right->add_line(f2);
        }
    }
    ll query(ll x)
    {
        ll ans = f(x);
        if(x < (l+r)/2)
        {
            if(left != NULL) ans = min(ans,left->query(x));
        }
        else
        {
            if(right != NULL) ans = min(ans,right->query(x));
        }
        return ans;
    }
};

lichao tree_;
ll pref[100001];
ll H[100001];
ll dp[100001];

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //random_start();
    int n;
    cin >> n;
    rep2(i,1,n) cin >> H[i];
    rep2(i,1,n)
    {
        cin >> pref[i];
        pref[i] += pref[i-1];
    }
    tree_.add_line({-2*H[1],-pref[1]+H[1]*H[1]});
    rep2(i,2,n)
    {
        dp[i] = tree_.query(H[i])+pref[i-1]+H[i]*H[i];
        tree_.add_line({-2*H[i],-pref[i]+H[i]*H[i]+dp[i]});
    }
    cout << dp[n] << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...