Submission #988645

# Submission time Handle Problem Language Result Execution time Memory
988645 2024-05-25T11:19:33 Z Aviansh Building Bridges (CEOI17_building) C++17
0 / 100
44 ms 68324 KB
#include <bits/stdc++.h>

using namespace std;

struct line{
    double m,c;
    double eval(double x){
        return (m*x)+c;
    }
    double long longersect(line l){
        return (l.c-c)/(m-l.m);
    }
};

template<class T> class liChaoTree{
    private:
        long long n;
        line *st;
        line def = {0,2e9};
    public:
        liChaoTree(long long siz){
            long long x = (long long) pow(2,ceil(log2(siz)));
            n=siz-1;
            st=new line[2*x];
            realST(0,n,0);
        }
        void realST(long long l, long long r, long long ind){
            st[ind]=def;
            if(l!=r){
                long long mid = (l+r)/2;
                realST(l,mid,ind*2+1);
                realST(mid+1,r,ind*2+2);
            }
        }
        double realQuer(double x, long long l, long long r, long long ind){
            long long mid = (l+r)/2;
            if(r-l==1){
                return st[ind].eval(x);
            }
            else if(x<mid){
                return min(st[ind].eval(x),realQuer(x,l,mid,ind*2+1));
            }
            else{
                return min(st[ind].eval(x),realQuer(x,mid,r,ind*2+2));
            }
        }
        double quer(double x){
            return realQuer(x,0,n,0);
        }
        void realUpdate(long long s, long long e, line nw, long long ind){
            long long mid = (s+e)/2;
            bool lef = st[ind].eval(s) > nw.eval(s);
            bool mi = st[ind].eval(mid) > nw.eval(mid);
            if(mi){
                swap(st[ind],nw);
            }
            if(e-s==1){
                return;
            }
            else if(lef!=mid){
                realUpdate(s,mid,nw,ind*2+1);
            }
            else{
                realUpdate(mid,e,nw,ind*2+2);
            }
        }
        void addLine(line l){
            realUpdate(0,n,l,0);
        }
};

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    long long n;
    cin >> n;
    long long h[n],w[n];
    long long tot = 0;
    for(long long i = 0;i<n;i++){
        cin >> h[i];
    }
    for(long long i = 0;i<n;i++){
        cin >> w[i];
        tot+=w[i];
    }
    long long dp[n];
    dp[0]=-w[0];
    liChaoTree<long long> lct(2e6);
    for(long long i = 1;i<n;i++){
        line l;
        l.m=-2*h[i-1];
        l.c=dp[i - 1] + h[i - 1] * h[i - 1];
        lct.addLine(l);
        dp[i]=lct.quer(h[i]) - w[i]+h[i]*h[i];
    }
    cout << tot+dp[n-1];
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 30 ms 65872 KB Output is correct
2 Incorrect 15 ms 65884 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 44 ms 68324 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 65872 KB Output is correct
2 Incorrect 15 ms 65884 KB Output isn't correct
3 Halted 0 ms 0 KB -