1935: [Shoi2007]Tree 园丁的烦恼

离线处理+离散化+BIT

果然读入优化这种东西还是不能懒,没判断负数WA两次TAT。

明明xi,yi都是非负数啊,为毛矩阵坐标就可以是负数了,还能不能好好玩了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=500000+5;
inline int read(){
	char ch;int x=0,f=1;
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
struct Node{
	int x,y,id,tp;
	bool operator<(const Node &rhs)const{
		if(x!=rhs.x)return x<rhs.x;
		return y<rhs.y;
	}
}t[N],q[N*4];
int hash[N*3],d[N*3],lim,ans[N];
void add(int x,int v){
	for(;x<=lim;x+=(x&-x))d[x]+=v;
}
int sum(int x){
	int ret=0;
	for(;x>0;x-=(x&-x))ret+=d[x];
	return ret;
}
int main(){
	int n,m;n=read();m=read();
	int cnt=0;
	for(int i=1;i<=n;i++){
		t[i].x=read();t[i].y=read();t[i].y++;
		hash[++lim]=t[i].y;
	}
	int aj,bj,cj,dj;
	for(int i=1;i<=m;i++){
		aj=read();bj=read();cj=read();dj=read();bj++;dj++;
		q[++cnt].id=i;q[cnt].x=cj;q[cnt].y=dj;q[cnt].tp=1;
		q[++cnt].id=i;q[cnt].x=aj-1;q[cnt].y=bj-1;q[cnt].tp=1;
		q[++cnt].id=i;q[cnt].x=aj-1;q[cnt].y=dj;q[cnt].tp=-1;
		q[++cnt].id=i;q[cnt].x=cj;q[cnt].y=bj-1;q[cnt].tp=-1;
		hash[++lim]=dj;hash[++lim]=bj-1;
	}
	hash[++lim]=0;
	sort(t+1,t+1+n);sort(q+1,q+1+cnt);sort(hash+1,hash+1+lim);
	int j=1;
	for(int i=1;i<=cnt;i++){
		while(j<=n&&t[j].x<=q[i].x){
			int y=lower_bound(hash+1,hash+1+lim,t[j].y)-hash;
			add(y,1);
			j++;
		}
		int y=lower_bound(hash+1,hash+1+lim,q[i].y)-hash;
		int tmp=sum(y);
		ans[q[i].id]+=tmp*q[i].tp;
	}
	for(int i=1;i<=m;i++)
	printf("%d\n",ans[i]);
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值