//块状链表
//分块排序，然后每次查找时在暴力查找头和尾两个块。
//中间那些块，因为有序所以只需2分查找即可。我用的是lower_pound();
//插入是，也是头和尾暴力插入，中间那些加到一个累计里即可。
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
int a[1000005],b[1000005],lei[1000005],dian[1000005],l[1000005],r[1000005];
int n,m,len,len1;
char ch[2];
void jian(int a1)
{
l[a1]=(a1-1)*len1+1;
r[a1]=min(a1*len1,n);
for(int i=l[a1];i<=r[a1];i++)
b[i]=a[i];
sort(b+l[a1],b+r[a1]+1);
}
void jia(int a1,int a2,int a3)
{
if(dian[a1]==dian[a2])
{
for(int i=a1;i<=a2;i++)
a[i]+=a3;
jian(dian[a1]);
return;
}
for(int i=a1;i<=r[dian[a1]];i++)
a[i]+=a3;
for(int i=l[dian[a2]];i<=a2;i++)
a[i]+=a3;
for(int i=dian[a1]+1;i<dian[a2];i++)
lei[i]+=a3;
jian(dian[a1]);
jian(dian[a2]);
return;
}
int zhao(int a1,int a2,int a3)
{
int sum=0,s;
if(dian[a1]==dian[a2])
{
for(int i=a1;i<=a2;i++)
if(a[i]+lei[dian[a1]]>=a3)
sum++;
return sum;
}
for(int i=a1;i<=r[dian[a1]];i++)
if(a[i]+lei[dian[a1]]>=a3)
sum++;
for(int i=l[dian[a2]];i<=a2;i++)
if(a[i]+lei[dian[a2]]>=a3)
sum++;
for(int i=dian[a1]+1;i<dian[a2];i++)
{
s=lower_bound(b+l[i],b+r[i]+1,a3-lei[i])-b;
sum+=r[i]+1-s;
}
return sum;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
len1=floor(sqrt(n));
len=n/len1;
if(n%len1)
len++;
for(int i=1;i<=len;i++)
jian(i);
for(int i=1;i<=n;i++)
dian[i]=(i-1)/len1+1;
for(int i=0;i<m;i++)
{
int a1,a2,a3;
scanf("%s%d%d%d",ch,&a1,&a2,&a3);
if(ch[0]=='A')
printf("%d\n",zhao(a1,a2,a3));
else
jia(a1,a2,a3);
}
return 0;
}

posted on 2016-01-14 17:22  xiyuedong  阅读(210)  评论(0编辑  收藏