This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
// segment add min get, returns INF if no segment added
struct Line{
ll a = 0, b = INF;
};
struct Lichao {
vector<Line> L;
vector<ll> x;
int N, m;
Lichao(vector<ll> dom){
N = 1;
x = dom;
m = x.size();
sort(x.begin(), x.end());
while(N < m) N*=2;
for(int i = m; i <= N; i++){
x.push_back(INF);
}
for(int i = N; i >= 1; i--){
x[i] = x[i-1];
}
L.resize(N*2+1, Line());
};
bool better(Line a, Line b, ll x){
return a.a*x + a.b < b.a*x + b.b;
}
void addline(int node, int low, int high, Line e){
int mid = (low+high)/2;
if(better(e, L[node], x[mid])){
swap(e, L[node]);
}
if(low == high) return;
if(e.a >= L[node].a){
addline(node*2, low, mid, e);
}else{
addline(node*2+1, mid+1, high, e);
}
}
void add(Line e){
addline(1, 1, N, e);
}
ll query(ll v){
int k = lower_bound(x.begin()+1, x.end(), v) - x.begin();
k = k+N-1;
ll res = INF;
while(k > 0){
res = min(res, L[k].a*v + L[k].b);
k /=2;
}
return res;
}
};
void solve(){
int n;
cin >> n;
vector<ll> h(n+1), w(n+1, 0);
for(int i = 1; i <= n; i++){
cin >> h[i];
}
ll tot = 0;
for(int i = 1; i <= n; i++){
cin >> w[i];
w[i] += w[i-1];
tot += w[i];
}
vector<ll> dom;
for(int i = 1; i <= n; i++){
dom.push_back(h[i]);
}
Lichao T(dom);
vector<ll> f(n+1, 0);
f[1] = -w[1];
T.add({-h[1]*2, f[1]+h[1]*h[1]});
for(int i = 2; i <= n; i++){
f[i] = T.query(h[i]) + h[i]*h[i] + w[i-1];
T.add({-h[i]*2, f[i]-w[i]+h[i]*h[i]});
}
cout << f[n];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |