hdu 4007 Dave

Dave

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2832    Accepted Submission(s): 944


Problem Description
Recently, Dave is boring, so he often walks around. He finds that some places are too crowded, for example, the ground. He couldn't help to think of the disasters happening recently. Crowded place is not safe. He knows there are N (1<=N<=1000) people on the ground. Now he wants to know how many people will be in a square with the length of R (1<=R<=1000000000). (Including boundary).
 

Input
The input contains several cases. For each case there are two positive integers N and R, and then N lines follow. Each gives the (x, y) (1<=x, y<=1000000000) coordinates of people. 
 

Output
Output the largest number of people in a square with the length of R.
 

Sample Input
3 2 1 1 2 2 3 3
 

Sample Output
3
数据很水,加上点只有1K个,所以以前做的时候暴力水过了,也没去想过正确解法,没想到这次上海邀请赛又看到他了,点的数量变成了1W,不能再水过了,哎,以前如果想下正确解法,那比赛要节约一些时间了。。
首先点离散化,按x升序和y升序排序。在X轴维护两条扫瞄线,使扫描线的距离不超过正方形边长。在Y轴建立一棵线段树,每个叶子节点表示的是以这个点为正方形的左下角,正方形能覆盖的点数,然后就是区间更新求最直了。

/*
 *=====================
 *File Name:a.cpp
 *Author: qqspeed
 *Date: 2014年 07月 15日 星期二 14:02:06 CST
 *=====================
 */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

typedef long long ll;
#define rep(i,s,t) for(int i=s;i<t;i++)
#define red(i,s,t) for(int i=s-1;i>=t;i--)
#define ree(i,now) for(int i=head[now];i!=-1;i=edge[i].next)
#define clr(a,v) memset(a,v,sizeof a)
#define L t<<1
#define R t<<1|1
#define MID int mid=(l+r)>>1
#define max(a,b) (a<b?b:a)
#define min(a,b) (a<b?a:b)
#define SQR(a) ((a)*(a))

inline int input(){
	int ret=0;bool isN=0;char c=getchar();
	while(c<'0' || c>'9'){
		if(c=='-') isN=1;
		c=getchar();
	}
	while(c>='0' && c<='9'){
		ret=ret*10+c-'0';
		c=getchar();
	}
	return isN?-ret:ret;
}

inline void output(int x){    
    if(x<0){    
        putchar('-');x=-x;    
    }    
    int len=0,data[11];    
    while(x){    
        data[len++]=x%10;x/=10;    
    }    
    if(!len)    data[len++]=0;    
    while(len--)   
        putchar(data[len]+48);    
    putchar('\n');  
}  


const int MAXN=1005;
struct point{
	double x,y;
	int ix,iy;
	void read(){
		scanf("%lf%lf",&x,&y);
	}
}p[MAXN];
bool cmp(point a,point b){
	return a.ix<b.ix || (a.ix==b.ix&&a.iy<b.iy);
}
double x[MAXN],y[MAXN];
int cntx,cnty;
int n,r;
vector<int>v[MAXN];
int sum[MAXN<<2],lazy[MAXN<<2];

inline void push_down(int t){
	if(lazy[t]){
		lazy[L]+=lazy[t];
		sum[L]+=lazy[t];
		lazy[R]+=lazy[t];
		sum[R]+=lazy[t];
		lazy[t]=0;
	}
}

inline void add(int t,int l,int r,int x,int y,int v){
	if(l>=x && r<=y){
		lazy[t]+=v;
		sum[t]+=v;
	}
	else{
		push_down(t);
		MID;
		if(y<=mid) add(L,l,mid,x,y,v);
		else if(x>mid) add(R,mid+1,r,x,y,v);
		else{
			add(L,l,mid,x,mid,v);
			add(R,mid+1,r,mid+1,y,v);
		}
		sum[t]=max(sum[L],sum[R]);
	}
}

inline void insert(int x,int up){
	int s=v[x].size();
	rep(i,0,s){
		int id=v[x][i];
		int b=p[id].iy;
		double h=p[id].y-r;
		int a=max(lower_bound(y+1,y+cnty+1,h)-(y+1)+1,1);
		add(1,1,cnty,a,b,up);
	}
}

int main(){
	while(~scanf("%d%d",&n,&r)){
		rep(i,0,n+1) v[i].clear();
		cntx=cnty=0;
		rep(i,0,n){
			p[i].read();
			x[++cntx]=p[i].x;
			y[++cnty]=p[i].y;
		}
		sort(x+1,x+cntx+1);
		sort(y+1,y+cnty+1);
		cntx=unique(x+1,x+cntx+1)-(x+1);
		cnty=unique(y+1,y+cnty+1)-(y+1);
		rep(i,0,n){
			p[i].ix=lower_bound(x+1,x+cntx+1,p[i].x)-(x+1)+1;
			p[i].iy=lower_bound(y+1,y+cnty+1,p[i].y)-(y+1)+1;
		}
		sort(p,p+n,cmp);
		rep(i,0,n){
			v[p[i].ix].push_back(i);
		}
		int end=1,start=1;
		clr(sum,0),clr(lazy,0);
		insert(start,1);
		int ans=sum[1];
		while(start<=end && end<=cntx){
			while(x[end]-x[start]>r && start<=end){
				insert(start,-1);start++;
			}
			ans=max(ans,sum[1]);
			end++;
			if(end<=cntx) insert(end,1);
		}
		output(ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值