CDQ分治 85pts求助
查看原帖
CDQ分治 85pts求助
780698
TallBanana楼主2024/7/15 21:45

WA最后两个点,和大多数cdq题解一样,但是没有归并,直接sort。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N=1e5+10;
LL n,h[N],w[N],f[N],q[N],id[N],hh,tt;
__int128 Y(LL i) { return f[i]+h[i]*h[i]-w[i]; }
__int128 X(LL i) { return h[i]; }
bool cmp(LL a,LL b) { return X(a)<X(b); }
void cdq(LL l,LL r)
{
	if(l==r) return;
	LL mid=l+r>>1;
	cdq(l,mid);
	for(int i=l;i<=r;i++) id[i]=i;
	sort(id+l,id+mid+1,cmp);
	sort(id+mid+1,id+r+1,cmp);
	hh=0; tt=-1;
	for(int i=l;i<=mid;i++)
	{
		while(hh<tt&&(Y(q[tt])-Y(q[tt-1]))*(X(id[i])-X(q[tt]))>=(Y(id[i])-Y(q[tt]))*(X(q[tt])-X(q[tt-1]))) tt--;
		q[++tt]=id[i];
	}
	for(int j=mid+1;j<=r;j++)
	{
		if(hh>tt) continue;
		LL K=2*h[id[j]];
		while(hh<tt&&Y(q[hh+1])-Y(q[hh])<=K*(X(q[hh+1])-X(q[hh]))) hh++;
		int opt=q[hh];
		f[id[j]]=min(f[id[j]],(h[id[j]]-h[opt])*(h[id[j]]-h[opt])+f[opt]+w[id[j]-1]-w[opt]);
	}
	cdq(mid+1,r);
}
int main()
{
	scanf("%lld",&n);
	for(int i=1;i<=n;i++) scanf("%lld",h+i);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",w+i);
		w[i]+=w[i-1];
	}
	for(int i=2;i<=n;i++) f[i]=1e18;
	cdq(1,n);
	printf("%lld",f[n]);
    return 0;
}
2024/7/15 21:45
加载中...