P2184.贪婪大陆(思维+树状数组)

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
	int x=0,f=1;char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return x*f;
}

const int maxn=2e5+100;
//分别维护i之前完全包含多少个区间
//j之后完全包含多少个区间
//区间(l,r)被(r,n)和(1,l)完全包含
int c1[maxn],c2[maxn];
int lowbit (int x) {
	return x&-x;
} 
int n,m;
void up1 (int p,int v) {
	for (int i=p;i<=n;i+=lowbit(i)) c1[i]+=v;
}
void up2 (int p,int v) {
	for (int i=p;i;i-=lowbit(i)) c2[i]+=v;
}
int sum1 (int p) {
	int ans=0;
	for (int i=p;i;i-=lowbit(i)) ans+=c1[i];
	return ans;
}
int sum2 (int p) {
	int ans=0;
	for (int i=p;i<=n;i+=lowbit(i)) ans+=c2[i];
	return ans;
}
int main () {
	n=read();
	m=read();
	int cnt=0;
	while (m--) {
		int op=read();
		int l=read();
		int r=read();
		if (op==1) {
			cnt++;
			up1(r,1);
			up2(l,1);
		}
		else {
			int ans=cnt-sum1(l-1)-sum2(r+1);
			printf("%d\n",ans);
		}
	}
}
posted @ 2021-05-24 12:28  zlc0405  阅读(40)  评论(0编辑  收藏  举报