BZOJ 4373 算术天才⑨与等差数列 [线段树]

该博客介绍了一道BZOJ上的题目,涉及在数组中进行数值修改和判断区间内数列是否为等差数列的问题。解题关键在于利用线段树维护两个条件:数的总和及平方和,以确定数列是否符合等差数列的特性。代码已实现并获得正确答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:给出n个数,有两个操作:1、可以对数组其中的某一个数进行修改 2、询问数组中区间为【L,R】的数从小到大排列能否组成等差数列

题解:是否为等差数列有两个条件:1、数的总和要等于 (首项+末项)*项数/2   2、数的平方和要等于 n*mi*mi+n*(n-1)*(2*n-1)*d*d/6+n*(n-1)*d*mi;这两个都可以通过线段树来维护。(注意平方和要爆longlong 要取mod)

AC代码:

#include<stdio.h>
#include<set>
#define mod 1000000007
#define N 300005
using namespace std;
typedef long long ll;
ll a[N];
ll sum[N*4],trmin[N*4],pfsum[N*4];
ll inv;
void build(ll L,ll R,ll root)
{
	if(L==R)
	{
		sum[root]=a[L];
		trmin[root]=a[L];
		pfsum[root]=a[L]*a[L]%mod;
		return ;
	}
	ll mid=L+R>>1;
	build(L,mid,root<<1);
	build(mid+1,R,root<<1|1);
	sum[root]=sum[root<<1]+sum[root<<1|1];
	trmin[root]=min(trmin[root<<1],trmin[root<<1|1]);
	pfsum[root]=(pfsum[root<<1]+pfsum[root<<1|1])%mod;
}
void update(ll x,ll L,ll R,ll root,ll k)
{
	if(L==R)
	{
		sum[root]=k;
		trmin[root]=k;
		pfsum[root]=k*k%mod;
		return ;
	}
	ll mid=L+R>>1;
	if(x<=mid)update(x,L,mid,root<<1,k);
	else update(x,mid+1,R,root<<1|1,k);
	sum[root]=sum[root<<1]+sum[root<<1|1];
	trmin[root]=min(trmin[root<<1],trmin[root<<1|1]);
	pfsum[root]=(pfsum[root<<1]+pfsum[root<<1|1])%mod;
}
ll query_sum(ll l,ll r,ll L,ll R,ll root,ll flag)
{
	if(l<=L&&R<=r)
	{
		if(flag==1)return sum[root];
		else return pfsum[root]%mod;
	}
	ll mid=L+R>>1,res=0;
	if(r<=mid)res=query_sum(l,r,L,mid,root<<1,flag);
	else if(l>mid)res=query_sum(l,r,mid+1,R,root<<1|1,flag);
	else res=query_sum(l,mid,L,mid,root<<1,flag)+query_sum(mid+1,r,mid+1,R,root<<1|1,flag);
	if(flag==2)res%=mod;
	return res;
}
ll query_min(ll l,ll r,ll L,ll R,ll root)
{
	if(l<=L&&R<=r)
		return trmin[root];
	ll mid=L+R>>1;
	if(r<=mid)return query_min(l,r,L,mid,root<<1);
	else if(l>mid)return query_min(l,r,mid+1,R,root<<1|1);
	else return min(query_min(l,mid,L,mid,root<<1),query_min(mid+1,r,mid+1,R,root<<1|1));
}
ll Qpower(ll a,ll b)
{
	ll ans=1;
	while(b)
	{
		if(b%2==1)ans=(ans*a)%mod;
		a=(a*a)%mod;
		b/=2;
	}
	return ans;
}
ll gets1(ll a,ll l,ll k) 
{  
    return a*l+(l-1)*l/2*k;  
}  
ll gets2(ll a,ll l,ll k) 
{  
    ll ret=a*a%mod*l%mod;  
    ret=(ret+(l-1)*l%mod*k%mod*a%mod)%mod;  
    ret+=l*(l-1)%mod*(2*l-1)%mod*k%mod*k%mod*inv%mod;  
    return ret%mod;  
}  
int main()
{
	ll n,m,yy=0;
	scanf("%lld%lld",&n,&m);
	for(ll i=1;i<=n;i++)
		scanf("%lld",&a[i]);
	build(1,n,1);
	inv=Qpower(6,mod-2);
	while(m--)
	{
		ll op;
		scanf("%lld",&op);
		if(op==1)
		{
			ll x,y;
			scanf("%lld%lld",&x,&y);
			x^=yy;y^=yy;
			update(x,1,n,1,y);
		}
		else
		{
			ll l,r,k;
			scanf("%lld%lld%lld",&l,&r,&k);
			l^=yy;r^=yy;k^=yy;
			ll num=r-l+1;
			ll mi=query_min(l,r,1,n,1);
			ll sum=query_sum(l,r,1,n,1,1);
			ll anssum=gets1(mi,num,k);
			if(anssum!=sum)
			{
				printf("No\n");
				continue;
			}
			ll pfsum=query_sum(l,r,1,n,1,2)%mod;
			ll anspfsum=gets2(mi,num,k);
			if(anspfsum==pfsum)printf("Yes\n"),yy++;
			else printf("No\n");
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值