树状数组
2026-07-20 12:43:18
发布于:广东
4阅读
0回复
0点赞
#include<bits/stdc++.h>
#define N 50005
using namespace std;
int n,m,b[N],c[N],lb[N];
struct node{
int x,y;
}a[N];
void add(int *arr,int x,int dat){
while(x<=N) arr[x]+=dat,x+=lb[x];
}
int sum(int *arr,int x){ //求区间[1,x]的和
int res=0;
while(x) res+=arr[x],x-=lb[x];
return res;
}
int main(){
cin>>n>>m;
for(int i=1;i<=N;i++) lb[i]=(i&-i); //最低位权
while(m--){
int k,l,r;
cin>>k>>l>>r;
if(k==1) add(b,l,1),add(c,r,1);
else cout<<sum(b,r)-sum(c,l-1)<<endl;;
}
return 0;
}
这里空空如也




有帮助,赞一个