- 一本通
- 题干与模板题不同,这道题是给出种树区间,所以,要按照种树的区间左与区间右各建一个树状数组,再求和。
- 代码
#include<bits/stdc++.h>
using namespace std;
int n,m,k,l,r,c2[1000001],c1[1000001];
int lowbit(int x){
return x&(-x);
}
void add1(int x){
while(x<=n){
c1[x]++;
x+=lowbit(x);
}
return;
}
void add2(int x){
while(x<=n){
c2[x]++;
x+=lowbit(x);
}
return;
}
int sum1(int x){
int ans=0;
while(x>0){
ans+=c1[x];
x-=lowbit(x);
}
return ans;
}
int sum2(int x){
int ans=0;
while(x>0){
ans+=c2[x];
x-=lowbit(x);
}
return ans;
}
int main(){
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d",&k);
if(k==1){
scanf("%d %d",&l,&r);
add1(l);
add2(r);
}
if(k==2){
scanf("%d %d",&l,&r);
printf("%d\n",sum1(r)-sum2(l-1));
}
}
return 0;
}