Submission #46981

#TimeUsernameProblemLanguageResultExecution timeMemory
46981dqhungdlBuilding Bridges (CEOI17_building)C++17
100 / 100
119 ms78036 KiB
#include <bits/stdc++.h> using namespace std; typedef pair<int64_t,int64_t> ii; int64_t n,a[100005],b[100005],F[100005]; ii tree[4000005]; int64_t f(ii line,int64_t x) { return line.first*x+line.second; } void Update(int64_t k,int64_t l,int64_t r,ii line) { int64_t mid=(l+r)/2; bool cleft=(f(line,l)<f(tree[k],l)); bool cmid=(f(line,mid)<f(tree[k],mid)); if(cmid==true) swap(tree[k],line); if(r-l==1) return; if(cleft!=cmid) Update(2*k,l,mid,line); else Update(2*k+1,mid,r,line); } int64_t Query(int64_t k,int64_t l,int64_t r,int64_t x) { int64_t mid=(l+r)/2; if(r-l==1) return f(tree[k],x); if(x<=mid) return min(Query(2*k,l,mid,x),f(tree[k],x)); return min(Query(2*k+1,mid,r,x),f(tree[k],x)); } void Naive() { for(int64_t i=2;i<=n;i++) { F[i]=1e15; for(int64_t j=1;j<i;j++) F[i]=min(F[i],F[j]+(a[i]-a[j])*(a[i]-a[j])+b[i-1]-b[j]); } for(int64_t i=1;i<=n;i++) cout<<F[i]<<' '; exit(0); } int main() { ios_base::sync_with_stdio(false); //freopen("TEST.INP","r",stdin); cin>>n; for(int64_t i=1;i<=n;i++) cin>>a[i]; for(int64_t i=1;i<=n;i++) { cin>>b[i]; b[i]+=b[i-1]; } //Naive(); for(int64_t i=1;i<=4e6;i++) tree[i]=ii(0,1e15); Update(1,0,1e6,ii(-2*a[1],F[1]+a[1]*a[1]-b[1])); for(int64_t i=2;i<=n;i++) { F[i]=Query(1,0,1e6,a[i])+a[i]*a[i]+b[i-1]; Update(1,0,1e6,ii(-2*a[i],F[i]+a[i]*a[i]-b[i])); } cout<<F[n]; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...