贪婪大陆
对于一个区间[l,r],右端点在l左边即[1,l-1]中的区间与区间[l,r]没有交集,
左端点在r右边即[r,n]中的区间与区间[l,r]没有交集,
其余区间必与[l,r]有交集,
因此维护两个树状数组,一个维护从1开始右端点的数量,
另一个维护从n开始左端点的数量,插入时取值n-x+1插入即可
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 #define N 200010 6 #define lowbit(x) (x&(-x)) 7 int n,m,tree[N][2],cnt,ans; 8 inline int read(){ 9 int x=0; char c=getchar(); 10 while(c<'0'||c>'9') c=getchar(); 11 while('0'<=c&&c<='9') { x=(x<<3)+(x<<1)+c-'0'; c=getchar(); } 12 return x; 13 } 14 inline void add(int p,int f){ 15 for(int i=p;i<=n;i+=lowbit(i)) 16 tree[i][f]++; 17 } 18 inline int query(int p,int f){ 19 int ans=0; 20 for(int i=p;i;i-=lowbit(i)) 21 ans+=tree[i][f]; 22 return ans; 23 } 24 int main() 25 { 26 scanf("%d%d",&n,&m); 27 int f,l,r; 28 while(m--){ 29 f=read(); 30 l=read(); r=read(); 31 if(f==1){ 32 cnt++; 33 add(r,0); 34 add(n-l+1,1); 35 } 36 else{ 37 ans=cnt-query(l-1,0)-query(n-r,1); 38 printf("%d ",ans); 39 } 40 } 41 return 0; 42 }