您的位置:首页 > 其它

1935: [Shoi2007]Tree 园丁的烦恼

2015-12-18 16:37 309 查看
离线处理+离散化+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
,q[N*4];
int hash[N*3],d[N*3],lim,ans
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: