Given the length n of an array A such that all Ai = 0 for all 1≤i≤n.
You need to answer q queries of two types:
-
1 l r print 1 if all values of Ai, such that l≤i≤r, are equal.
-
2 l r a b update the range l r with the arithmetic progression a+b∗(i−l).
For example if l=2, r=5, a=4, and b=3 then:
A2=A2+4
A3=A3+4+3
A4=A4+4+2∗3
A5=A5+4+3∗3
Input
First line contains two integers n and q (1≤n,q≤2∗105).
Each of the next q lines follow one of two formates:
- 1 l r (1≤l≤r≤n)
- 2 l r a b (−108≤a,b≤108)
Output
For each query of the second type print 1 if the range is made up of the same value and 0 if it’s not.
Example
input
5 3
2 1 3 4 1
1 1 3
1 4 5
output
0
1
题意:1、给出一个长度为n的序列,每个数都是0 。
2、现在你有2个操作,第一个操作查询区间 [l,r],判断是不是该区间每个数都一样,如果一样请打印"1",否则打印"0"。
3、第二个操作是区间加a+b*(i-l) i表示数组对应的位置。
分析:区间的操作,一般使用线段树/数组数组来维护。可以发现,a+b*(i-l) 我们可以看成2部分,第一部分就是a,区间加a操作很容易;第二部分是b*(i-l) 这是一个等差数列。那想一想,我们能不能用差分去维护呢?
差分:(不会差分的小伙伴先去学习下再往下阅读)
这里我说明下: (d数组代表差分数组,a数组代表原数组,D表示区间加的数,l,r表示区间大小)
仔细观察前面差分数组的变化,我们可以发现,区间修改[l,r]加上D
可以等价为 d[l]+=d d[r+1]-=d
OK 上面已经很完美的处理了区间加一个数的问题了,但是我们还是没解决维护区间等差数列。
其实也很简单,和上述过程一样,把更改前和更改后的差分数组对比下,能找到关系。留给读者自己去推导。
最后推导出来的结果应该是,区间 [l+1,r] 加b ,d[r+1]-=b*(r-l)。
对于查询操作,判断一个区间是否全不相同,根据差分数组的性质是d[i]=a[i]-a[i-1]. 所以只需要维护差分数组的最大值和最小值(区间[l+1,r]) ,如果最大值==最小值并且为0,那么这区间的数都相同,否则不相同。
下面是AC代码:(注意开long long)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+100;
int n,q;
int f,l,r;
ll a,b;
struct Segment_tree
{
int l,r;
ll minn;
ll maxx;
ll lazy;
}T[N<<2];
void pushup(int p)
{
T[p].maxx=max(T[p<<1].maxx,T[p<<1|1].maxx);
T[p].minn=min(T[p<<1].minn,T[p<<1|1].minn);
}
void pushdown(int p)
{
if(T[p].lazy!=0)
{
T[p<<1].maxx+=T[p].lazy;
T[p<<1|1].maxx+=T[p].lazy;
T[p<<1].minn+=T[p].lazy;
T[p<<1|1].minn+=T[p].lazy;
T[p<<1].lazy+=T[p].lazy;
T[p<<1|1].lazy+=T[p].lazy;
T[p].lazy=0;
}
}
void build(int l,int r,int p)
{
T[p].l=l,T[p].r=r,T[p].lazy=0;
if(l==r)
{
T[p].minn=0;
T[p].maxx=0;
return ;
}
int mid=(l+r)>>1;
build(l,mid,p<<1),build(mid+1,r,p<<1|1);
pushup(p);
}
void update1(int pos,ll val,int p)
{
if(T[p].l==T[p].r)
{
T[p].maxx+=val;
T[p].minn+=val;
return ;
}
int mid=(T[p].l+T[p].r)>>1;
pushdown(p);
if(pos<=mid)update1(pos,val,p<<1);
if(pos>mid)update1(pos,val,p<<1|1);
pushup(p);
}
void update2(int l,int r,int p,ll val)
{
if(l<=T[p].l&&T[p].r<=r)
{
T[p].maxx+=val;
T[p].minn+=val;
T[p].lazy+=val;
return ;
}
int mid=(T[p].l+T[p].r)>>1;
pushdown(p);
if(l<=mid)update2(l,r,p<<1,val);
if(r>mid)update2(l,r,p<<1|1,val);
pushup(p);
}
bool query(int l,int r,int p)
{
if(l<=T[p].l&&T[p].r<=r)
{
if((T[p].maxx==T[p].minn)&&T[p].maxx==0)
{
return true;
}
else
{
return false;
}
}
int mid=(T[p].l+T[p].r)>>1;
bool ans=true;
pushdown(p);
if(l<=mid)ans&=query(l,r,p<<1);
if(r>mid)ans&=query(l,r,p<<1|1);
return ans;
}
int main()
{
scanf("%d%d",&n,&q);
build(1,n,1);
while(q--)
{
scanf("%d",&f);
if(f==2)
{
scanf("%d%d%lld%lld",&l,&r,&a,&b);
update1(l,a,1);
if(r+1<=n)update1(r+1,-a,1);
if(l+1<=r)
{
update2(l+1,r,1,b);
if(r+1<=n)update1(r+1,-b*1ll*(r-l),1);
}
}
else if(f==1)
{
scanf("%d%d",&l,&r);
if(l==r)
{
printf("1\n");
}
else
{
printf("%d\n",query(l+1,r,1));
}
}
}
return 0;
}