When I first saw this question, I used a tree array to optimize the constant optimization of prefix and difference, and it turned out to be AC? (This data is too watery~)
The first block question I did, I debugged it for a long time, but I didn't expect that there would be an error on the second score! (must be noted) only this commemoration;
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
int a[1000010],b[1000010],belong[1000010]; //原来的
int lazy[1000010],l[1000010],r[1000010]; //块上的
int block,tot,n,m;
void build()
{
block=sqrt(n);
tot=n/block;
if(n%block){
++tot;
}
for(register int i=1;i<=n;i++){
b[i]=a[i];
belong[i]=(i-1)/block+1;
}
for(register int i=1;i<=tot;i++){
l[i]=(i-1)*block+1;
r[i]=i*block;
}
r[tot]=n;
for(register int i=1;i<=tot;i++){
sort(b+l[i],b+r[i]+1);
}
return;
}
void change(int x,int y,int k)
{
if(belong[x]==belong[y]){
for(register int i=x;i<=y;i++){
a[i]+=k;
}
for(register int i=l[belong[x]];i<=r[belong[x]];i++){
b[i]=a[i];
}
sort(b+l[belong[x]],b+r[belong[x]]+1);
return ;
}
for(register int i=x;i<=r[belong[x]];i++){
a[i]+=k;
}
for(register int i=l[belong[x]];i<=r[belong[x]];i++){
b[i]=a[i];
}
sort(b+l[belong[x]],b+r[belong[x]]+1);
for(register int i=l[belong[y]];i<=y;i++){
a[i]+=k;
}
for(register int i=l[belong[y]];i<=r[belong[y]];i++){
b[i]=a[i];
}
sort(b+l[belong[y]],b+r[belong[y]]+1);
for(register int i=belong[x]+1;i<=belong[y]-1;i++){
lazy[i]+=k;
}
}
int query(int x,int y,int goal)
{
int ans=0;
if(belong[x]==belong[y]){
for(register int i=x;i<=y;i++){
if(a[i]+lazy[belong[x]]>=goal) ++ans;
}
return ans;
}
for(register int i=x;i<=r[belong[x]];i++){
if(a[i]+lazy[belong[x]]>=goal) ++ans;
}
for(register int i=l[belong[y]];i<=y;i++){
if(a[i]+lazy[belong[y]]>=goal) ++ans;
}
for(register int i=belong[x]+1;i<=belong[y]-1;i++){
int L=l[i],R=r[i],mid;
while(L<R){
int mid=(L+R)/2;
if(b[mid]+lazy[i]>=goal){
R=mid;
}
else{
L=mid+1;
}
}
if(L==r[i]){
ans+=((b[L]+lazy[i])>=goal);
continue;
}
ans+=(r[i]-L+1);
}
return ans;
}
int main()
{
cin>>n>>m;
for(register int i=1;i<=n;i++) scanf("%d",&a[i]);
build();
for(register int i=1;i<=m;i++){
char type;
int x,y,k;
cin>>type>>x>>y>>k;
if(type=='M'){
change(x,y,k);
}
else{
cout<<query(x,y,k)<<endl;
}
}
}
Reprinted in: https://www.cnblogs.com/kamimxr/p/11600198.html