hdu 4007 Dave(线段树+离散化+扫描线)

来源:互联网 发布:wx sample.php 编辑:程序博客网 时间:2024/05/02 17:01

Dave

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


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 21 12 23 3
 

Sample Output
3
Hint
If two people stand in one place, they are embracing.
 

Source
The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest
 

Recommend
lcy


题意:求边平行于xy轴的边长为r的正方形的最大包含人数

题解:其实可以n方二重循环过的...就是求矩形交...不过为了一天一棵树的锻炼,果断线段树离散化扫描线一起上...1A而且是第二了...心情那个开朗得...感觉搞ACM就是这样的事情是最爽最开心了~~~~


#include<stdio.h>#include<string.h>#include<stdlib.h>struct edge{    int x,y1,y2,flag;}seg[2003];int hasy[2003],tree[8003],lazy[8003],n,all;int cmp(const void *a,const void *b){    return *(int *)a>*(int *)b?1:-1;}int cmp2(const void *a,const void *b){    struct edge c=*(struct edge *)a;    struct edge d=*(struct edge *)b;    return c.x>d.x?1:-1;}void down(int pos){    tree[pos<<1]+=lazy[pos];    tree[pos<<1|1]+=lazy[pos];    lazy[pos<<1]+=lazy[pos];    lazy[pos<<1|1]+=lazy[pos];    lazy[pos]=0;}void up(int pos){    tree[pos]=tree[pos<<1]>tree[pos<<1|1]?tree[pos<<1]:tree[pos<<1|1];}void updata(int l,int r,int pos,int templ,int tempr,int val){    int mid=(l+r)>>1;    if(templ<=l&&r<=tempr)    {        tree[pos]+=val;        lazy[pos]+=val;        return;    }    down(pos);    if(tempr<=mid) updata(l,mid,pos<<1,templ,tempr,val);    else if(templ>mid) updata(mid+1,r,pos<<1|1,templ,tempr,val);    else    {        updata(l,mid,pos<<1,templ,mid,val);        updata(mid+1,r,pos<<1|1,mid+1,tempr,val);    }    up(pos);}int mysearch(int x){    int l=0,r=all-1,mid;    while(l<=r)    {        mid=(l+r)>>1;        if(hasy[mid]==x) return mid;        else if(hasy[mid]>x) r=mid-1;        else l=mid+1;    }    return -1;}int main(){    int i,r,x,y,res;    while(scanf("%d%d",&n,&r)>0)    {        for(i=0;i<n;i++)        {            scanf("%d%d",&x,&y);            seg[2*i].x=x;    seg[2*i+1].x=x+r+1;            hasy[2*i]=seg[2*i].y1=seg[2*i+1].y1=y;            hasy[2*i+1]=seg[2*i].y2=seg[2*i+1].y2=y+r+1;            seg[2*i].flag=1;   seg[2*i+1].flag=-1;        }        qsort(hasy,2*n,sizeof(hasy[0]),cmp);        qsort(seg,2*n,sizeof(seg[0]),cmp2);        for(n*=2,all=i=1;i<n;i++)        {            if(hasy[i]==hasy[i-1]) continue;            hasy[all++]=hasy[i];        }        memset(tree,0,sizeof(tree));        memset(lazy,0,sizeof(lazy));        for(res=i=0;i<n;i++)        {            x=mysearch(seg[i].y1);            y=mysearch(seg[i].y2);            updata(0,all-1,1,x,y,seg[i].flag);            if(res<tree[1]) res=tree[1];        }        printf("%d\n",res);    }    return 0;}


原创粉丝点击