bzoj3745: [Coci2015]Norma

这道题维护了一大堆前缀和,真是。。。。因为MOD又调了好久。。。。。

考虑分治思想,对于l,r,mid可以枚举左端点,右端点在mid右侧的情况,利用单调性和前缀和达到O(nlogn)。

细节看代码:(Pr开头的都是前缀和)

#include
#include
#define N 500005
#define ll long long
#define MOD 1000000000
using namespace std;
ll n,a[N],Pr[N],Pr0[N],Pr1[N],Pr2[N],Pr3[N],MAX[N],MIN[N],Px1[N],Px2[N],Px3[N];
ll read()
{
	char c=getchar();int x=0;
	while (c<'0'||c>'9') c=getchar();
	while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x;
}
ll work(ll l,ll r)
{
	if (l>r) return 0;
	if (l==r) return a[l]*a[r]%MOD;
	ll mid=(l+r)>>1,i,j1,j2,Min,Max,Ans=0;
	i=mid;j1=j2=mid;Min=Max=a[mid];
	MAX[mid]=MIN[mid]=Pr1[mid]=Pr2[mid]=Px1[mid]=Px2[mid]=Px3[mid]=a[mid];
	Pr3[mid]=a[mid]*a[mid]%MOD;
	Pr[mid]=Pr0[mid]=1;
	for (ll i=mid+1;i<=r;i++)
	{
		MAX[i]=max(MAX[i-1],a[i]);
		MIN[i]=min(MIN[i-1],a[i]);
		Pr[i]=Pr[i-1]+1;;
		Pr0[i]=(Pr0[i-1]+Pr[i])%MOD;
		Px1[i]=(Px1[i-1]+MAX[i])%MOD;
		Px2[i]=(Px2[i-1]+MIN[i])%MOD;
		Px3[i]=(Px3[i-1]+MAX[i]*MIN[i])%MOD;
		Pr1[i]=(Pr1[i-1]+MAX[i]*Pr[i])%MOD;
		Pr2[i]=(Pr2[i-1]+MIN[i]*Pr[i])%MOD;
		Pr3[i]=(Pr3[i-1]+MAX[i]*MIN[i]%MOD*Pr[i])%MOD;
	}
	
	for (;i>=l;i--)
	{
		ll t=mid-i;
		Min=min(Min,a[i]);Max=max(Max,a[i]);
		while (j1=Min) j1++;
		while (j2


你可能感兴趣的:(bzoj)