/*
* @Author: RMQuan
* @Date: 2025-10-22 22:10:41
* @Last Modified by: RMQuan
* @Last Modified time: 2025-10-22 23:19:43
*/
/*idea :
*/
#include <bits/stdc++.h>
bool M1;
#define int long long
#define ll long long
#define INTMAX INT_MAX
#define INTMIN INT_MIN
#define LONGMAX LLONG_MAX
#define LONGMIN LLONG_MIN
#define fi first
#define se second
#define memfull(a,b) memset(a,b,sizeof(a));
#define endl '\n'
#define TASK "TEST"
#define file() if (fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout);}
using namespace std;
const int MOD=1e9+7;
const int maxn=1e5+7;
const int inf=1e18;
int h[maxn],w[maxn],pre[maxn],n,dp[maxn];
struct point
{
int A,B;
point()
{
A=0;
B=inf;
}
int cal(int x)
{
return A*x+B;
}
point(int _A,int _B):A(_A),B(_B){}
};
struct LCT_node
{
LCT_node *L,*R;
point ans;
LCT_node()
{
L=NULL;
R=NULL;
ans=point();
}
};
struct LCT
{
LCT_node *root;
LCT(int cnt)
{
root=new LCT_node();
}
void upd(LCT_node *p,int l,int r,point val)
{
if (val.B>=inf)return ;
int mid=(l+r)/2;
if (p->ans.cal(mid)>val.cal(mid))swap(p->ans,val);
if (l!=r)
{
if (val.A>p->ans.A)
{
if (p->L==NULL)p->L=new LCT_node();
upd(p->L,l,mid-1,val);
}
else{
if (p->R==NULL)p->R=new LCT_node();
upd(p->R,mid+1,r,val);
}
}
}
int get(LCT_node *p,int l,int r,int pos)
{
int cnt=p->ans.cal(pos);
int mid=(l+r)/2;
if (pos>mid&&p->R!=NULL)cnt=min(cnt,get(p->R,mid+1,r,pos));
else if (pos<mid&&p->L!=NULL)cnt=min(cnt,get(p->L,l,mid-1,pos));
return cnt;
}
void update(point val)
{
upd(root,0,1e9,val);
}
int query(int pos)
{
return get(root,0,1e9,pos);
}
};
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
file();
cin>>n;
for (int i=1;i<=n;i++)cin>>h[i];
for (int i=1;i<=n;i++)
{
cin>>w[i];
pre[i]=pre[i-1]+w[i];
}
LCT lct(0);
lct.update(point(-2*h[1],dp[1]+h[1]*h[1]-pre[1]));
for (int i=2;i<=n;i++)
{
dp[i]=lct.query(h[i])+(h[i]*h[i]+pre[i-1]);
// cerr<<dp[i]<<endl;
lct.update(point(-2*h[i],dp[i]+h[i]*h[i]-pre[i]));
}
cout<<dp[n];
bool M2;
cerr<<"-------------------------------------------------"<<endl;
cerr<<"Time : "<<clock()<<" ms"<<endl;
cerr<<"Memory : "<<abs(&M2-&M1)/1024/1024<<" MB"<<endl;
cerr<<"-------------------------------------------------"<<endl;
return 0;
}
Compilation message (stderr)
building.cpp: In function 'int32_t main()':
building.cpp:25:50: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | #define file() if (fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
building.cpp:103:5: note: in expansion of macro 'file'
103 | file();
| ^~~~
building.cpp:25:81: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | #define file() if (fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
building.cpp:103:5: note: in expansion of macro 'file'
103 | file();
| ^~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |