KDtree模板(HDU4347)

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define link(x) for(edge *j=h[x];j;j=j->next)
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
const int MAXN=5e4+10;
const double eps=1e-8;
#define ll long long
const ll inf=1e15+10;
const ll INF=1e5;
using namespace std;
struct edge{int t,v;edge*next;}e[MAXN<<1],*h[MAXN],*o=e;
void add(int x,int y,int vul){o->t=y;o->v=vul;o->next=h[x];h[x]=o++;}
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int rt,pos,k,n;
typedef struct node{
    int p[6],c[2];ll maxx[6],minn[6];
    friend bool operator <(node aa,node bb){
        if(aa.p[pos]!=bb.p[pos])return aa.p[pos]<bb.p[pos];
        for(int i=pos+1;i<=k;i++){
            if(aa.p[i]!=bb.p[i])return aa.p[i]<bb.p[i];
        }
        for(int i=1;i<pos;i++){
            if(aa.p[i]!=bb.p[i])return aa.p[i]<bb.p[i];
        }
    }
}node;
node a[MAXN<<2];
void up(int x,int y){
	for(int i=1;i<=k;i++)a[x].maxx[i]=max(a[x].maxx[i],a[y].maxx[i]),a[x].minn[i]=min(a[x].minn[i],a[y].minn[i]);
}
int built(int l,int r,int now){
	if(l>r)return 0;
	int mid=(l+r)>>1;
	pos=now,nth_element(a+l,a+mid,a+r+1);
	for(int i=1;i<=k;i++)a[mid].maxx[i]=a[mid].minn[i]=a[mid].p[i];
	a[mid].c[0]=a[mid].c[1]=0;
	if(l<mid)a[mid].c[0]=built(l,mid-1,now%k+1),up(mid,a[mid].c[0]);
	if(r>mid)a[mid].c[1]=built(mid+1,r,now%k+1),up(mid,a[mid].c[1]);
	return mid;
}
typedef struct Tmp{
	int p[6];ll dis;
	friend bool operator<(Tmp aa,Tmp bb){return aa.dis<bb.dis;}
}Tmp;
ll dist(node aa,node bb){
	ll ans=0;
	for(int i=1;i<=k;i++)ans+=1LL*(aa.p[i]-bb.p[i])*(aa.p[i]-bb.p[i]);
	return ans;
}
priority_queue<Tmp>que;
node t;
stack<Tmp>s;
ll get_ans(node aa,node bb){
	ll ans=0;
	for(int i=1;i<=k;i++){
		ans+=min(1LL*(aa.minn[i]-bb.p[i])*(aa.minn[i]-bb.p[i]),1LL*(bb.p[i]-aa.maxx[i])*(bb.p[i]-aa.maxx[i]));
	}
	return ans;
}
void querty(int x,int now){
		if(!x)return ;
		ll res=dist(a[x],t);
		if(res<(que.top()).dis){
			que.pop();
			Tmp tt;tt.dis=res;
			for(int i=1;i<=k;i++)tt.p[i]=a[x].p[i];
			que.push(tt);
		}
		 ll tt=t.p[now]-a[x].p[now]; 
	     if(tt<=0){
	       	querty(a[x].c[0],now%k+1);
	         if(que.top().dis>tt*tt)
	             querty(a[x].c[1],now%k+1);
	     }
	     else{
	       	querty(a[x].c[1],now%k+1);
	         if(que.top().dis>tt*tt)
	             querty(a[x].c[0],now%k+1);         
	     }
}
int main(){
	while(scanf("%d%d",&n,&k)!=EOF){
		inc(i,1,k)a[0].minn[i]=INF,a[0].maxx[i]=-INF;
		inc(i,1,n){
			inc(j,1,k)a[i].p[j]=read();
		}
		rt=built(1,n,1);
		int q=read();
		while(q--){
			inc(i,1,k)t.p[i]=read();
			int K=read();
			Tmp t1;t1.dis=inf;
			inc(i,1,K)que.push(t1);
			querty(rt,1);
			while(!que.empty())s.push(que.top()),que.pop();
			printf("the closest %d points are:
",K);
			while(!s.empty()){
				Tmp tt=s.top();
				for(int i=1;i<=k;i++){
					if(i!=k)printf("%d ",tt.p[i]);
					else printf("%d
",tt.p[i]);
				}
				s.pop();
			}
		}
	}
	return 0;
}

          The Closest M Points

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)
Total Submission(s): 7235    Accepted Submission(s): 2219


Problem Description
The course of Software Design and Development Practice is objectionable. ZLC is facing a serious problem .There are many points in K-dimensional space .Given a point. ZLC need to find out the closest m points. Euclidean distance is used as the distance metric between two points. The Euclidean distance between points p and q is the length of the line segment connecting them.In Cartesian coordinates, if p = (p1, p2,..., pn) and q = (q1, q2,..., qn) are two points in Euclidean n-space, then the distance from p to q, or from q to p is given by:

Can you help him solve this problem?
 
Input
In the first line of the text file .there are two non-negative integers n and K. They denote respectively: the number of points, 1 <= n <= 50000, and the number of Dimensions,1 <= K <= 5. In each of the following n lines there is written k integers, representing the coordinates of a point. This followed by a line with one positive integer t, representing the number of queries,1 <= t <=10000.each query contains two lines. The k integers in the first line represent the given point. In the second line, there is one integer m, the number of closest points you should find,1 <= m <=10. The absolute value of all the coordinates will not be more than 10000.
There are multiple test cases. Process to end of file.
 
Output
For each query, output m+1 lines:
The first line saying :”the closest m points are:” where m is the number of the points.
The following m lines representing m points ,in accordance with the order from near to far
It is guaranteed that the answer can only be formed in one ways. The distances from the given point to all the nearest m+1 points are different. That means input like this:
2 2
1 1
3 3
1
2 2
1
will not exist.
 
Sample Input
3 2
1 1
1 3
3 4
2
2 3
2
2 3
1
 
Sample Output
the closest 2 points are: 1 3 3 4 the closest 1 points are: 1 3
原文地址:https://www.cnblogs.com/wang9897/p/9706457.html