BZOJ2626 JZPFAR KD树求第k远点 小根堆维护

来源:互联网 发布:java rar 压缩包解压 编辑:程序博客网 时间:2024/06/03 11:17

大家都很强, 可与之共勉 。

题意:
  给您n个点,m次询问,每次问您距离(x,y)k远的点与之的欧几里得距离是多少。

题解:
  KD树,用小根堆维护前k远的,比堆顶远就弹出堆顶然后加入该点。
  KD树就是暴力启发式搜索
  STLpair重载运算符无效,手动再见

/**************************************************************    Problem: 2626    User: Lazer2001    Language: C++    Result: Accepted    Time:14668 ms    Memory:52088 kb****************************************************************/# include <bits/stdc++.h>namespace In  {# define In_Len 2000000    static std :: streambuf *fb ( std :: cin.rdbuf ( ) ) ;    static char buf [In_Len], *ss ( 0 ), *tt ( 0 ) ;# define pick( )  ( (ss == tt) ? ( tt = buf + fb -> sgetn ( ss = buf, In_Len ), ((ss == tt) ? -1 : *(ss ++)) ) :*(ss ++) )    inline char getc ( )  {  register char c; while ( ! isalpha ( c = pick ( ) ) ) ;  return c ;  }    inline int read ( )  {        register int x, c ;        bool opt ( 1 ) ;        while ( ! isdigit ( c = pick ( ) ) && ( c ^ -1 ) && ( c ^ 45 ) ) ;        if ( c == 45 )  c = pick ( ), opt = 0 ;        for ( x = -48 + c ; isdigit ( c = pick ( ) ) ; ( x *= 10 ) += c - 48 ) ;        return opt ? x : -x ;    }# undef pick# undef In_Len}template < class T >  inline T max ( const T& a, const T& b )  {  return a > b ? a : b ;  }template < class T >  inline T min ( const T& a, const T& b )  {  return a < b ? a : b ;  }template < class T >  inline void smin ( T& d, const T& x )  {  ( d > x ) ? ( d = x ) : 0 ;  }template < class T >  inline void smax ( T& d, const T& x )  {  ( d < x ) ? ( d = x ) : 0 ;  }# define N 500050template < class T >class Heap  {    private :        int len ;        T a [40] ;    public :        Heap ( )  {   len = 0 ;  }        inline void push ( const T& ele )  {            a [++ len] = ele ;            std :: push_heap ( a + 1, a + 1 + len ) ;        }        inline void clear ( )  {            len = 0 ;        }        inline T& top ( )  {            return a [1] ;        }        inline void pop ( )  {            std :: pop_heap ( a + 1, a + 1 + len ) ;            -- len ;        }        inline bool empty ( )  {            return len == 0 ;        }} ;struct Point  {    int x, y, id ;    Point ( )  {    }    Point ( int x, int y, int id ) : x ( x ), y ( y ), id ( id )  {    }} p [N] ;bool dm ;inline bool cmp ( const Point& a, const Point& b )  {    if ( dm )  return ( a.x == b.x ) ? a.y < b.y : a.x < b.x ;    return ( a.y == b.y ) ? a.x < b.x : a.y < b.y ;}inline double dis ( const Point& a, const Point& b )  {    return 1.0 * ( a.x - b.x ) * ( a.x - b.x ) + 1.0 * ( a.y - b.y ) * ( a.y - b.y ) ;}struct node *null ;struct node  {    Point p, l, r ;    node *ch [2] ;    inline void update ( )  {        smin ( l.x, min ( ch [0] -> l.x, ch [1] -> l.x ) ) ;        smin ( l.y, min ( ch [0] -> l.y, ch [1] -> l.y ) ) ;        smax ( r.x, max ( ch [0] -> r.x, ch [1] -> r.x ) ) ;        smax ( r.y, max ( ch [0] -> r.y, ch [1] -> r.y ) ) ;    }    inline double dis ( const Point& p )  {        if ( this == null )  return 0x3f3f3f3f ;        return max ( max ( :: dis ( p, l ), :: dis ( p, r ) ), max ( :: dis ( p, Point ( l.x, r.y, -1 ) ), :: dis ( p, Point ( r.x, l.y, -1 ) ) ) ) ;    }}  pool [N << 1], *root ;inline node* newnode ( const Point& p )  {    static node* pt ( pool + 1 ) ;    pt -> ch [0] = pt -> ch [1] = null ;    pt -> p = pt -> l = pt -> r = p ;    return pt ++ ;}struct state  {    double dis ;    int id ;    state ( double dis = 0, int id = 0 ): dis ( dis ), id ( id )  {  }    inline bool operator < ( const state& s ) const  {        return dis > s.dis || ( dis == s.dis && id < s.id ) ;    }} ;Heap < state > Q ;class K_Dimensional_Tree  {    private :        Point* p ;        inline node* build ( int lf, int rg, bool d )  {            if ( lf > rg )  return null ;            int mid = ( lf + rg ) >> 1 ;            dm = d ;            std :: nth_element ( p + lf, p + mid, p + rg + 1, cmp ) ;            node* o = newnode ( p [mid] ) ;            o -> ch [0] = build ( lf, mid - 1, ! d ), o -> ch [1] = build ( mid + 1, rg, ! d ) ;            o -> update ( ) ;            return o ;        }        inline void query ( node*& o, const Point& p )  {            if ( o == null )  return ;            state st = state ( dis ( o -> p, p ), o -> p.id ) ;            if ( st < Q.top ( ) )  { // < is bigger                Q.pop ( ) ; Q.push ( st ) ;            }            double dis [2] = {  o -> ch [0] -> dis ( p ), o -> ch [1] -> dis ( p ) } ;            bool d = dis [0] < dis [1] ; //  far one !!!            query ( o -> ch [d], p ) ;            if ( state ( dis [! d], o -> ch [! d] -> p.id ) < Q.top ( ) )  query ( o -> ch [! d], p ) ;        }    public :        K_Dimensional_Tree ( )  {            null = pool ;            null -> l = Point ( 0x3f3f3f3f, 0x3f3f3f3f, -1 ),            null -> r = Point ( - 0x3f3f3f3f, - 0x3f3f3f3f, -1 ) ;        }        inline void build ( Point* p, int n )  {            this -> p = p ;            dm = 0 ;            root = build ( 1, n, 0 ) ;        }        inline int query ( const Point& p, int k )  {            Q.clear ( ) ;            while ( k -- )  Q.push ( state ( -0x3f3f3f3f, -1 ) ) ;            query ( root, p ) ;            return Q.top ( ).id ;        }} T ;int main ( )  {//  freopen ( "in.txt", "r", stdin ) ;    using namespace In ;    int n ( read ( ) ) ;    for ( int i = 1 ; i <= n ; ++ i )  {        static int x, y ;        x = read ( ), y = read ( ) ;        p [i] = Point ( x, y, i ) ;    }    T.build ( p, n ) ;    for ( int m = read ( ) ; m ; -- m )  {        static int x, y, k ;        x = read ( ), y = read ( ), k = read ( ) ;        printf ( "%d\n", T.query ( Point ( x, y, -1 ), k ) ) ;    }    return 0 ;}
原创粉丝点击