题意:给出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");
}
}
}