HDU 4007 Dave 线段树

题意:广场上有许多的人,现在给你一个边长为R的正方形,求正方形最多能框住的人,包括边界。
题解:

#include <algorithm>  
#include <iostream>  
using namespace std;  
  
#define N 2010  
#define L(u) ( u * 2 )  
#define R(u) ( u * 2 + 1 )  
#define max(a,b) ( a > b ? a : b )  
  
unsigned int Y[N];  
  
struct TreeNode  
{  
    int l, r, v, add;  
} node[N*3];  
  
struct Point  
{  
    unsigned int x, y;
	int v;  
} point[N];  
  
bool cmp ( Point & a, Point & b )  
{  
    if ( a.x == b.x )  
        return a.v > b.v; 
    return a.x < b.x;  
}  
  
int bfind ( int l, int r, unsigned int key )  
{  
    while ( l <= r )  
    {  
        int mid = ( l + r ) / 2;  
        if ( Y[mid] == key )  
            return mid;  
        if ( key < Y[mid] )  
            r = mid - 1;  
        else  
            l = mid + 1;  
    }  
    return -1;  
}  
  
void build ( int u, int l, int r )  
{  
    node[u].l = l;  
    node[u].r = r;  
    node[u].v = node[u].add = 0;  
    if ( l == r ) return;  
    int mid = ( l + r ) / 2;  
    build ( L(u), l, mid );  
    build ( R(u), mid + 1, r );  
}  
  
void update ( int u, int l, int r, int val )  
{  
    if ( l <= node[u].l && node[u].r <= r )  
    {  
        node[u].add += val;  
        node[u].v += val;  
        return;  
    }  
  
    if ( node[u].add != 0 )  
    {  
        node[L(u)].add += node[u].add;  
        node[L(u)].v += node[u].add;  
        node[R(u)].add += node[u].add;  
        node[R(u)].v += node[u].add;  
        node[u].add = 0;  
    }  
          
    int mid = ( node[u].l + node[u].r ) / 2;  
  
    if ( r <= mid )   
        update ( L(u), l, r, val );  
    else if ( l > mid )  
        update ( R(u), l, r, val );  
    else  
    {  
        update ( L(u), l, mid, val );  
        update ( R(u), mid + 1, r, val );  
    }  
    node[u].v = max ( node[L(u)].v, node[R(u)].v );  
}  
  
int main()  
{  
    int n, R, x, y, i, j;  
    while ( scanf("%d%d",&n,&R) != EOF )  
    {  
        for ( i = 1; i <= n; i++ )  
        {  
            scanf("%d%d",&x,&y);  
            point[i].x = x;  
            point[i].y = y;  
            point[i].v = 1;  
            Y[i] = y;  
        }  
   
        for ( i = 1; i <= n; i++ )  
        {  
            point[i+n].x = point[i].x + R;  
            point[i+n].y = point[i].y;  
            point[i+n].v = -1;  
            Y[i+n] = point[i].y + R;  
        }  
  
        sort ( point+1, point+1+n*2, cmp );  
        sort ( Y+1, Y+1+n*2 );  
          
        for ( i = j = 1; i <= n*2; i++ )  
        {  
            if ( Y[j] != Y[i] )  
                Y[++j] = Y[i];  
        }  
  
        build ( 1, 1, j );  
     
        int l, r, ans = 0;    
        for ( i = 1; i <= n * 2; i++ )  
        {  
            l = bfind ( 1, j, point[i].y );  
            r = bfind ( 1, j, point[i].y + R );  
            update ( 1, l, r, point[i].v );  
            if ( ans < node[1].v ) ans = node[1].v;  
        }  
        printf("%d\n", ans );  
    }  
    return 0;  
}  


你可能感兴趣的:(Algorithm,struct,Build,iostream,2010)