zoukankan      html  css  js  c++  java
  • #K-D Tree#洛谷 4357 [CQOI2016]K 远点对

    题目

    已知平面内 (n) 个点的坐标,求欧氏距离下的第 (k) 远点对。


    分析

    先将(k)乘2转换为第(k)远有序点对。
    由于(O(n^2))即枚举一个点再枚举另一个点会超出时限,
    一个明显的优化就是如果一堆点它们不能对点对距离产生贡献
    那么就不必去枚举这些点
    考虑用K-D Tree维护区间坐标最小值和坐标最大值,如果不能产生贡献直接退出,
    然后在跳左区间或右区间时估价判断哪个可能产生更大贡献那么就改变访问顺序,
    然后开一个全为0的小根堆判断即可


    代码

    #include <cstdio>
    #include <cctype>
    #include <algorithm>
    #include <queue>
    #define rr register
    using namespace std;
    const int N=200011;
    typedef long long lll;
    int ran,root,n,k;
    priority_queue<lll>q;
    inline signed iut(){
    	rr int ans=0,f=1; rr char c=getchar();
    	while (!isdigit(c)) f=(c=='-')?-f:f,c=getchar();
    	while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
    	return ans*f;
    }
    inline void print(int ans){
    	if (ans<0) putchar('-'),ans=-ans;
    	if (ans>9) print(ans/10);
    	putchar(ans%10+48);
    }
    inline signed min(int a,int b){return a<b?a:b;}
    inline lll max(lll a,lll b){return a>b?a:b;}
    struct rec{
    	int p[2];
    	bool operator <(const rec &t)const{
    	    return p[ran]<t.p[ran];
    	}
    };
    inline lll SQR(lll x){return x*x;}
    struct KD_Tree{
    	int mn[N][2],mx[N][2],son[N][2]; rec p[N];
    	inline void pup(int now){
    		for (rr int i=0;i<2;++i){
    			mn[now][i]=mx[now][i]=p[now].p[i];
    			if (son[now][0]){
    				mn[now][i]=min(mn[now][i],mn[son[now][0]][i]);
    				mx[now][i]=max(mx[now][i],mx[son[now][0]][i]);
    			}
    			if (son[now][1]){
    				mn[now][i]=min(mn[now][i],mn[son[now][1]][i]);
    				mx[now][i]=max(mx[now][i],mx[son[now][1]][i]);
    			}
    		}
    	}
    	inline signed build(int l,int r,int Ran){
    		if (l>r) return 0;
    		rr int mid=(l+r)>>1;
    		ran=Ran,nth_element(p+l,p+mid,p+1+r);
    		son[mid][0]=build(l,mid-1,Ran^1);
    		son[mid][1]=build(mid+1,r,Ran^1);
    		pup(mid);
    		return mid;
    	}
    	inline lll calc(int t,int x){
    		return max(SQR(p[x].p[0]-mn[t][0]),SQR(p[x].p[0]-mx[t][0]))+max(SQR(p[x].p[1]-mn[t][1]),SQR(p[x].p[1]-mx[t][1]));
    	}
    	inline void query(int now,int x){
    		rr lll t=SQR(p[x].p[0]-p[now].p[0])+SQR(p[x].p[1]-p[now].p[1]);
    		if (t>-q.top()) q.pop(),q.push(-t);
    		rr lll c0=calc(son[now][0],x),c1=calc(son[now][1],x);
    		if (son[now][0]&&son[now][1]){
    			if (c0>c1&&c0>-q.top()){
    				query(son[now][0],x);
    				if (c1>-q.top()) query(son[now][1],x);
    			}else if (c1>-q.top()){
    				query(son[now][1],x);
    				if (c0>-q.top()) query(son[now][0],x);
    			}
    		}else if (son[now][0]){
    			if (c0>-q.top()) query(son[now][0],x);  
    		}else if (son[now][1]){
    			if (c1>-q.top()) query(son[now][1],x);
    		}
    	}
    }Tre;
    signed main(){
    	n=iut(),k=iut()<<1;
    	for (rr int i=1;i<=k;++i) q.push(0);
    	for (rr int i=1;i<=n;++i) Tre.p[i].p[0]=iut(),Tre.p[i].p[1]=iut();
    	root=Tre.build(1,n,0);
    	for (rr int i=1;i<=n;++i) Tre.query(root,i);
    	return !printf("%lld",-q.top());
    }
    
  • 相关阅读:
    JSON的使用总结
    pc端分页插件的使用
    简单修改选择文件样式
    H5中的本地存储
    H5中的 meta 标签及 移动页面单位
    1001. A+B Format (20)
    查看mysql的注册表路径
    win10 64位安装mysql
    [POLITICS] S Korea lawmakers vote to impeach leader
    5-17 Hashing (25分)
  • 原文地址:https://www.cnblogs.com/Spare-No-Effort/p/14738240.html
Copyright © 2011-2022 走看看