#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include <cstdint>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
#define int long long
#define double long double
const int mod=1e9+7,mxn=1e6+5,lg=30,inf=1e18,minf=-1e18;
int mode=1;
struct line{
int m,c;
mutable int p;
int get(int x) const{return (m*x)+c;}
bool operator<(line a)const{
if(mode)return m>a.m;
else return p<a.p;
}
};
struct cht{
multiset<line>v;
int div(int a,int b){return (a/b)-(!(a%b==0||(a*b)>=0));}
int intersect(multiset<line>::iterator a,multiset<line>::iterator b){return div((a->c-b->c),(b->m-a->m));}
void cal(multiset<line>::iterator a){a->p=((a==v.end())?inf:intersect(a,next(a)));}
bool isect(multiset<line>::iterator x,multiset<line>::iterator y)
{
if (y==v.end()) return x->p=inf, 0;
if (x->m==y->m) x->p=(x->c<=y->c)?inf:-inf;
else x->p=div(y->c-x->c, x->m-y->m);
return x->p>=y->p;
}
bool bad(multiset<line>::iterator a,multiset<line>::iterator b){
if(b==v.end())return a->p=inf,0;
if(a->m==b->m)a->p=(a->c<=b->c)?inf:minf;
else a->p=intersect(a,b);
return a->p>=b->p;
}/*
void add(line nw)
{
auto x=v.insert(nw), y=next(x);
while (bad(x, y)) y=v.erase(y);
if ((y=x)!=v.begin()&&bad(--x, y)) bad(x, v.erase(y));
while ((y=x)!=v.begin()&&(--x)->p>=y->p) bad(x, v.erase(y));
}*/
void add(line a){
auto it=v.insert(a);
while(bad(it,next(it)))v.erase(next(it));
if(it!=v.begin()&&bad(prev(it),it))bad(prev(it),it=v.erase(it));
if(it!=v.begin())it=prev(it);
while(it!=v.begin()&&bad(prev(it),it)){
bad(prev(it),it=v.erase(it));
it=prev(it);
}
}
int qry(int x){
if(v.empty())return inf;
mode=0;
auto it=v.lb((line){0,0,x})->get(x);
mode=1;
return it;
}
}t;
int v1[mxn+10],v2[mxn+10],dp[mxn+10];
int32_t main(){
fastio
int n;cin>>n;
for(int i=1;i<=n;i++)cin>>v1[i];
for(int i=1;i<=n;i++)cin>>v2[i],v2[i]+=v2[i-1];
t.add((line){-2*v1[1],-v2[1]+(v1[1]*v1[1]),0});
for(int i=2;i<=n;i++){
dp[i]=t.qry(v1[i])+(v1[i]*v1[i])+v2[i-1];
t.add((line){-2*v1[i],dp[i]-v2[i]+(v1[i]*v1[i]),0});
}
cout<<dp[n];
}
Compilation message
building.cpp: In member function 'void cht::add(line)':
building.cpp:76:53: warning: operation on 'it' may be undefined [-Wsequence-point]
76 | if(it!=v.begin()&&bad(prev(it),it))bad(prev(it),it=v.erase(it));
| ~~^~~~~~~~~~~~
building.cpp:79:19: warning: operation on 'it' may be undefined [-Wsequence-point]
79 | bad(prev(it),it=v.erase(it));
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
8892 KB |
Output is correct |
2 |
Correct |
36 ms |
8792 KB |
Output is correct |
3 |
Correct |
36 ms |
8856 KB |
Output is correct |
4 |
Correct |
33 ms |
8792 KB |
Output is correct |
5 |
Correct |
35 ms |
9948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
36 ms |
8892 KB |
Output is correct |
7 |
Correct |
36 ms |
8792 KB |
Output is correct |
8 |
Correct |
36 ms |
8856 KB |
Output is correct |
9 |
Correct |
33 ms |
8792 KB |
Output is correct |
10 |
Correct |
35 ms |
9948 KB |
Output is correct |
11 |
Correct |
36 ms |
8784 KB |
Output is correct |
12 |
Correct |
34 ms |
8792 KB |
Output is correct |
13 |
Correct |
26 ms |
8792 KB |
Output is correct |
14 |
Correct |
35 ms |
8896 KB |
Output is correct |
15 |
Correct |
58 ms |
14964 KB |
Output is correct |
16 |
Correct |
36 ms |
9820 KB |
Output is correct |
17 |
Correct |
17 ms |
8796 KB |
Output is correct |
18 |
Correct |
17 ms |
8668 KB |
Output is correct |