题目链接
将区间修改转化为两个单点修改,则查询操作的结果为r左边的(个数减去l左边的)的个数,进而想到树状维护前缀和,具体思路是c1维护(的个数,c2维护)的个数,
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N=50010;
int c1[N],c2[N];
int n,m;
int lowbit(int x){
return x&(-x);
}
void add1(int x,int y){
while(x<=n){
c1[x]+=y;
x+=lowbit(x);
}
}
void add2(int x,int y){
while(x<=n){
c2[x]+=y;
x+=lowbit(x);
}
}
int ask1(int x){
int res=0;
while(x>0){
res+=c1[x];
x-=lowbit(x);
}
return res;
}
int ask2(int x){
int res=0;
while(x>0){
res+=c2[x];
x-=lowbit(x);
}
return res;
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int op,x,y;
scanf("%d%d%d",&op,&x,&y);
if(op==1){
add1(x,1); //c1数组维护(
add2(y,1); //c2数组维护)
}else{
printf("%d\n",ask1(y)-ask2(x-1)); //注意是r-1
}
}
}