#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("popcnt")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MAX=2e5+10,P=1e9+7;
const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f;
const ldb eps=1e-6;
const ldb PI=acos(-1.0);
const int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
template<typename T>
using vvector = vector<vector<T>>;
struct Node {
int l,r;
pll ln;
Node *lft,*rt;
Node(int l,int r):l(l),r(r),ln({0,-oo}),lft(nullptr),rt(nullptr) {};
void update(pll ln2) {
if (l==r) {
if (ln2.F*l+ln2.S>=ln.F*l+ln.S) swap(ln,ln2);
return;
}
if (ln.F<ln2.F) swap(ln,ln2);
int mid=l+r>>1;
if (ln2.F*mid+ln2.S<=ln.F*mid+ln.S) {
if (!lft) lft=new Node(l,mid);
lft ->update(ln2);
} else {
if (!rt) rt = new Node(mid+1,r);
rt->update(ln);
ln=ln2;
}
// cout<<l<<" "<<r<<" "<<ln.F<<" "<<ln.S<<" l r ln\n";
}
ll query(ll x) {
int mid=l+r>>1;
ll ans=ln.F*x+ln.S;
if (x<=mid and lft) ans=min(ans,lft->query(x));
else if (x>mid and rt) ans=min(ans,rt->query(x));
return ans;
}
};
int main() {
speed;
int n;
cin>>n;
vector<ll> h(n+1);
vector<ll> pre(n+1);
for (int i=1;i<=n;i++) {
cin>>h[i];
}
for (int i=1;i<=n;i++) {
cin>>pre[i];
pre[i]+=pre[i-1];
}
vector<ll> dp(n+1);
Node *rt = new Node(0,1e6);
rt->update({-2*h[1],h[1]*h[1]-pre[1]});
for (int i=2;i<=n;i++) {
dp[i]=rt->query(h[i])+h[i]*h[i]+pre[i-1];
rt->update({-2*h[i],h[i]*h[i]-pre[i]+dp[i]});
// cout<<dp[i]<<" dp\n";
// cout<<-2*h[i]<<" "<<h[i]*h[i]-pre[i]+dp[i]<<" ln\n";
}
cout<<dp[n]<<"\n";
return 0;
}
/*
6
3 8 7 1 6 6
0 -1 9 1 2 0
*/
Compilation message
building.cpp: In member function 'void Node::update(pll)':
building.cpp:37:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | int mid=l+r>>1;
| ~^~
building.cpp: In member function 'll Node::query(ll)':
building.cpp:49:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
49 | int mid=l+r>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
3920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |