BZOJ4520 CQOI2016 K远点对 KD树 小根堆维护距离

来源:互联网 发布:动态加载js文件 编辑:程序博客网 时间:2024/06/08 17:37

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

题意:
  给您n个平面上的点,问第k远的点对的距离是多少。
  其中n[1,100000],k[1,n(n1)2],2311

题解:
因为是点对(认为无序),所以把k乘以2,然后用小根堆维护距离,堆顶就是答案。
还是KD树裸题……

/**************************************************************    Problem: 4520    User: Lazer2001    Language: C++    Result: Accepted    Time:1532 ms    Memory:18880 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 200050template < class T >class Heap  {    private :        int len ;        T a [N] ;    public :        Heap ( )  {   len = 0 ;  }        inline void push ( const T& ele )  {            a [++ len] = ele ;            std :: push_heap ( a + 1, a + 1 + len, std :: greater < T > ( ) ) ;        }        inline void clear ( )  {            len = 0 ;        }        inline T& top ( )  {            return a [1] ;        }        inline void pop ( )  {            std :: pop_heap ( a + 1, a + 1 + len, std :: greater < T > ( ) ) ;            -- len ;        }        inline bool empty ( )  {            return len == 0 ;        }} ;struct Point  {    int x, y ;    Point ( )  {    }    Point ( int x, int y ) : x ( x ), y ( y )  {    }} 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 long long dis ( const Point& a, const Point& b )  {    return 1LL * ( a.x - b.x ) * ( a.x - b.x ) + 1LL * ( 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 long long 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 ) ), :: dis ( p, Point ( r.x, l.y ) ) ) ) ;    }}  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 ++ ;}Heap < long long > 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 ;            long long st = dis ( o -> p, p ) ;            if ( st >= Q.top ( ) )  {                Q.pop ( ) ; Q.push ( st ) ;            }            long long dis [2] = {  o -> ch [0] -> dis ( p ), o -> ch [1] -> dis ( p ) } ;            bool d = dis [0] < dis [1] ;            query ( o -> ch [d], p ) ;            if ( dis [! d] >= Q.top ( ) )  query ( o -> ch [! d], p ) ;        }    public :        K_Dimensional_Tree ( )  {            null = pool ;            null -> l = Point ( 0x3f3f3f3f, 0x3f3f3f3f ),            null -> r = Point ( - 0x3f3f3f3f, - 0x3f3f3f3f ) ;        }        inline void build ( Point* p, int n )  {            this -> p = p ;            dm = 0 ;            root = build ( 1, n, 0 ) ;        }        inline void query ( const Point& p )  {            query ( root, p ) ;        }} T ;int main ( )  {    using namespace In ;    int n ( read ( ) ), k ( read ( ) << 1 ) ;    for ( int i = 1 ; i <= n ; ++ i )  {        static int x, y ;        x = read ( ), y = read ( ) ;        p [i] = Point ( x, y ) ;    }    T.build ( p, n ) ;    while ( k -- ) Q.push ( -1 ) ;    for ( int i = 1 ; i <= n ; ++ i )  T.query ( p [i] ) ;    printf ( "%lld\n", Q.top ( ) ) ;    return 0 ;}