[SHOI2007]园丁的烦恼

裸的二维数点

#include"cstdio"
#include"cstring"
#include"iostream"
#include"algorithm"
using namespace std;

const int MAXN=5e5+5;

int n,m,maxn;
int Tib[MAXN*3];
struct rpg{
	int kd,x,y,id;
	int rey,val;
}pnt[MAXN*5];

bool cmp1(rpg a,rpg b){return a.y<b.y;}
bool cmp2(rpg a,rpg b)
{
	if(a.x!=b.x) return a.x<b.x;
	if(a.y!=b.y) return a.y<b.y;
	return a.kd<b.kd;
}

inline int read()
{
	int x=0;char ch=getchar();
	while(ch<'0'||'9'<ch) ch=getchar();
	while('0'<=ch&&ch<='9') x=x*10+(ch^48),ch=getchar();
	return x;
}

void init()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i){
		int x=read(),y=read();
		pnt[i]=(rpg){1,x,y,m+n};
	}for(int i=1;i<=m;++i){
		int a=read(),b=read(),c=read(),d=read();
		pnt[n+i]=(rpg){2,a-1,b-1,i};
		pnt[n+m+i]=(rpg){3,a-1,d,i};
		pnt[n+(m<<1)+i]=(rpg){4,c,b-1,i};
		pnt[n+(m*3)+i]=(rpg){5,c,d,i};
	}sort(pnt+1,pnt+n+(m<<2)+1,cmp1);pnt[1].rey=1;
	for(int i=2;i<=n+(m<<2);++i) pnt[i].rey=pnt[i].y==pnt[i-1].y?pnt[i-1].rey:pnt[i-1].rey+1;
	maxn=pnt[n+(m<<2)].rey;sort(pnt+1,pnt+n+(m<<2)+1,cmp2);
	return;
}

void ins(int x)
{
	for(int i=x;i<=maxn;i+=i&-i) ++Tib[i];
	return;
}

int query(int x)
{
	int sum=0;
	for(int i=x;i;i&=i-1) sum+=Tib[i];
	return sum;
}

bool cmp3(rpg a,rpg b){return a.id==b.id?a.kd<b.kd:a.id<b.id;}

void solve()
{
	for(int i=1;i<=n+(m<<2);++i){
		if(pnt[i].kd==1) ins(pnt[i].rey);
		else pnt[i].val=query(pnt[i].rey);
	}sort(pnt+1,pnt+n+(m<<2)+1,cmp3);
	for(int i=1;i<=(m<<2);i+=4){
		printf("%d\n",pnt[i].val-pnt[i+1].val-pnt[i+2].val+pnt[i+3].val);
	}return;
}

int main()
{
	init();
	solve();
	return 0;
}
posted @ 2018-12-04 12:49  A·H  阅读(149)  评论(0编辑  收藏  举报