題目傳送門
每個詢問其實是求前面有多少個區間與之相交
答案就是:前面區間數量-與前面區間不相交數量
與前面區間不相交數量=前面區間 l 大於詢問 r 的數量 + 前面區間 r 小於詢問 l 的數量
代碼:
#include<cstdio>
using namespace std;
#define low(x) (x&(-x))
const int maxn=50000+100;
int treel[maxn],treer[maxn];
int n,m;
void Add(int *tree,int x){
while(x<=n){
tree[x]++;
x+=low(x);
}
}
int GetSum(int *tree,int x){
int sum=0;
while(x){
sum+=tree[x];
x-=low(x);
}
return sum;
}
int main(){
scanf("%d%d",&n,&m);
int type,l,r;
int typeonenum=0;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&type,&l,&r);
if(type==2) printf("%d\n",typeonenum-GetSum(treel,n)+GetSum(treel,r)-GetSum(treer,l-1));
if(type==2) continue;
else typeonenum++;
Add(treel,l);
Add(treer,r);
}
}