(分析见正睿10.1笔记)
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int c[440000],d[440000],a[110000],col[440000];
char s[100];
inline void build(int le,int ri,int wh,int pl,int k){
c[wh]=col[wh]=0;
d[wh]=min(d[wh],k);
if(le==ri)return;
int mid=(le+ri)>>1;
if(mid>=pl)build(le,mid,wh<<1,pl,k);
else build(mid+1,ri,wh<<1|1,pl,k);
return;
}
inline void pd(int wh){
col[wh<<1]+=col[wh];
col[wh<<1|1]+=col[wh];
d[wh<<1]-=col[wh];
d[wh<<1|1]-=col[wh];
col[wh]=0;
return;
}
inline void update(int le,int ri,int wh,int x,int y){
if(le>=x&&ri<=y){
if(d[wh]>1){
d[wh]--;
col[wh]++;
return;
}else {
if(le==ri){
d[wh]=a[le];
col[wh]=0;
c[wh]++;
return;
}
pd(wh);
int mid=(le+ri)>>1;
update(le,mid,wh<<1,x,y);
update(mid+1,ri,wh<<1|1,x,y);
d[wh]=min(d[wh<<1],d[wh<<1|1]);
c[wh]=c[wh<<1]+c[wh<<1|1];
return;
}
}
int mid=(le+ri)>>1;
pd(wh);
if(mid>=x)update(le,mid,wh<<1,x,y);
if(mid<y)update(mid+1,ri,wh<<1|1,x,y);
d[wh]=min(d[wh<<1],d[wh<<1|1]);
c[wh]=c[wh<<1]+c[wh<<1|1];
return;
}
inline int q(int le,int ri,int wh,int x,int y){
if(le>=x&&ri<=y)return c[wh];
int mid=(le+ri)>>1,ans=0;
if(mid>=x)ans+=q(le,mid,wh<<1,x,y);
if(mid<y)ans+=q(mid+1,ri,wh<<1|1,x,y);
return ans;
}
int main(){
int n,m,i,j,k;
while(scanf("%d%d",&n,&m)!=EOF){
memset(d,0x3f,sizeof(d));
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
build(1,n,1,i,a[i]);
}
for(i=1;i<=m;i++){
scanf("%s",s);
if(s[0]=='a'){
int x,y;
scanf("%d%d",&x,&y);
update(1,n,1,x,y);
}else {
int x,y;
scanf("%d%d",&x,&y);
printf("%d
",q(1,n,1,x,y));
}
}
}
return 0;
}