HDU 4007 Dave (line tree + discretization + scan line)

Source: Internet
Author: User

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 this some places are too crowded, for example, the ground. He couldn ' t help to the disasters happening recently. Crowded place isn't safe. He knows there are N (1<=n<=1000) people on the ground. Now it wants to know how many people'll be being 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 Hint If Two people stand in one place, they are.
Source the 36th ACM/ICPC Asia regional Dalian Site--online Contest
Recommend LCY

The maximum number of squares that are edges r that are parallel to the XY axis

Solving: In fact, can n square double cycle ... is to ask for rectangular intersection ... But for a day a tree of exercise, decisive segment tree discretization scan line together ... 1 A and it's the second ... The mood that cheerful to ... Feel like doing ACM is such a thing is the most fun and happy ~ ~ ~ ~


#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&LT;=L&AMP;&AMP;R&LT;=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;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.