https://www.luogu.com.cn/problem/P4655
这东西长得就很像斜率优化的东西,但是不能用朴素斜率优化,因为横坐标不满足递增。
但我们可以直接用李超线段树维护即可。
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL#define debug(...) fprintf(stdout, ##__VA_ARGS__)
#else#define debug(...) void(0)
#endif
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
//#define M
//#define mo
#define N 1000010
int n, m, i, j, k, T;
int h[N], c[N], s[N], f[N], rt; struct Lichao_tree {int tot, ls[N<<2], rs[N<<2]; pair<int, int>e[N<<2]; void build(int &k, int l, int r) {if(!k) k=++tot, e[k]={0, 1e17}; if(l==r) return ;int mid=(l+r)>>1; build(ls[k], l, mid); build(rs[k], mid+1, r); }int calc(pair<int, int>p, int x) {return x * p.fi + p.se; }void add(int k, int l, int r, pair<int, int>p) {if(l==r) return e[k]=p, /*debug("y= %dx + %d\n", p.fi, p.se), */void(); int mid=(l+r)>>1, s1, t1, sl, tl, sr, tr; s1=calc(e[k], mid); t1=calc(p, mid);debug("[%lld %lld][%lld %lld]\n", e[k].fi, e[k].se, p.fi, p.se); if(t1 < s1) swap(e[k], p); //, debug("swap Success !(%d %d) newp : [%d %d]\n", t1, s1, p.fi, p.se); sl=calc(e[k], l); tl=calc(p, l);sr=calc(e[k], r); tr=calc(p, r);if(tl < sl) add(ls[k], l, mid, p); if(tr < sr) add(rs[k], mid+1, r, p); }int que(int k, int l, int r, int x) {if(l==r) return calc(e[k], x); int mid=(l+r)>>1, ans=calc(e[k], x);
// debug("%lld = %lld * %lld + %lld\n", ans, e[k].fi, x, e[k].se); if(x<=mid) ans=min(ans, que(ls[k], l, mid, x)); else ans=min(ans, que(rs[k], mid+1, r, x)); return ans; }
}Seg;signed main()
{#ifdef LOCALfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);#endif
// srand(time(NULL));
// T=read();
// while(T--) {
//
// }n=read(); for(i=1; i<=n; ++i) h[i]=read(); for(i=1; i<=n; ++i) k=read(), s[i]=s[i-1]+k; for(i=1; i<=n; ++i) c[i]=Z(h[i])+s[i-1]; for(i=1; i<=n; ++i) debug("%lld ", c[i]); debug("\n"); Seg.build(rt, 1, 1e6); f[1]=0; Seg.add(rt, 1, 1e6, {-2*h[1], f[1]-s[1]+Z(h[1])}); i=1; debug("y = %lldx + %lld\n", -2*h[i], f[i]-s[i]+Z(h[i])); for(i=2; i<=n; ++i) {f[i]=Seg.que(rt, 1, 1e6, h[i])+c[i]; Seg.add(rt, 1, 1e6, {-2*h[i], f[i]-s[i]+Z(h[i])}); debug("y = %lldx + %lld\n", -2*h[i], f[i]-s[i]+Z(h[i])); }for(i=1; i<=n; ++i) debug("%lld ", f[i]); debug("\n"); printf("%lld", f[n]); return 0;
}