#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||(a*b)>=0));}
int intersect(multiset<line>::iterator a,multiset<line>::iterator b){return div(a->c-b->c,b->m-a->m);}
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 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:61:53: warning: operation on 'it' may be undefined [-Wsequence-point]
61 | if(it!=v.begin()&&bad(prev(it),it))bad(prev(it),it=v.erase(it));
| ~~^~~~~~~~~~~~
building.cpp:64:19: warning: operation on 'it' may be undefined [-Wsequence-point]
64 | bad(prev(it),it=v.erase(it));
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 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 |
37 ms |
8780 KB |
Output is correct |
2 |
Correct |
36 ms |
9812 KB |
Output is correct |
3 |
Correct |
36 ms |
9808 KB |
Output is correct |
4 |
Correct |
33 ms |
9560 KB |
Output is correct |
5 |
Correct |
36 ms |
10840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 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 |
37 ms |
8780 KB |
Output is correct |
7 |
Correct |
36 ms |
9812 KB |
Output is correct |
8 |
Correct |
36 ms |
9808 KB |
Output is correct |
9 |
Correct |
33 ms |
9560 KB |
Output is correct |
10 |
Correct |
36 ms |
10840 KB |
Output is correct |
11 |
Correct |
34 ms |
9820 KB |
Output is correct |
12 |
Correct |
35 ms |
9796 KB |
Output is correct |
13 |
Correct |
28 ms |
10072 KB |
Output is correct |
14 |
Correct |
35 ms |
10076 KB |
Output is correct |
15 |
Correct |
45 ms |
15964 KB |
Output is correct |
16 |
Correct |
38 ms |
10844 KB |
Output is correct |
17 |
Correct |
17 ms |
9820 KB |
Output is correct |
18 |
Correct |
18 ms |
10048 KB |
Output is correct |