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

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

题意:
  给您 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 200050

template < 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 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值