1. 程式人生 > >hdu4317(kd樹模板)

hdu4317(kd樹模板)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#define mem(a,x) memset(a,x,sizeof(a))
#define s1(x) scanf("%d",&x)
#define s2(x,y) scanf("%d%d",&x,&y)
#define s3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s4(x,y,z,k) scanf("%d%d%d%d",&x,&y,&z,&k)
#define ff(a,n) for(int i = 0 ; i < n; i++) scanf("%d",a+i)
#define ls 2*rt
#define rs 2*rt+1
#define lson ls,L,mid
#define rson rs,mid+1,R
#define ll long long
using namespace std;
typedef pair<int,int> pii;
//inline ll ask(int x){ll res=0;while(x)res+=c[x],x-=x&(-x);return res;}
//inline void add(int x,int d){while(x<=n)c[x]+=d,x+=x&(-x);}
//int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b);}
const ll inf = 0x3f3f3f3f;
const int mx = 5e4+10;

int n,dim,k,m;
struct no{
    int v[12];
    bool operator < (const no &a)const{
        return v[k] < a.v[k];
    } 
}p[mx*2],ob,data[mx],sta[12];
bool f[mx*4];
//priority_queue< pair<double,no> > que;
priority_queue<pair<double,no> > que;
double dis(no a, no b){
    double ans = 0;
    for(int i = 0;i < dim; i++){
        ans+=(a.v[i]-b.v[i])*(a.v[i]-b.v[i]);
    }
    return sqrt(1.0*ans);
}
void build(int l, int r,int rt,int dir){
    k = dir; 
    if( l>r)
        return;
    f[rt] = 1;
    int mid = (l+r)/2;
    nth_element(data+l,data+mid,data+r+1);
    p[rt] = data[mid];
    build(l,mid-1,rt*2,(dir+1)%dim);
    build(mid+1,r,rt*2+1,(dir+1)%dim);
}
void query(int rt,int dir){
    k = dir; 
    if(!f[rt])
        return;
    double d = dis(ob,p[rt]);
     pair<double,no> cur(d,p[rt]); 
    bool fa = 0;
    int son;
    if(ob.v[dir] >= p[rt].v[dir]){
        son = rs;
    }
    else
        son = ls;
    if(f[son])
        query(son,(dir+1)%dim);
    if(que.size() < m){
        que.push(cur);
        fa = 1;
    }
    else{
        if(cur < que.top()){
            que.pop();
            que.push(cur);
        }
        if(fabs(ob.v[dir]-p[rt].v[dir]) < que.top().first){
            fa = 1;
        }
    }
    if(f[son^1]&&fa)
        query(son^1,(dir+1)%dim);
}

int main(){
    //    freopen("F:\\in.txt","r",stdin);
    //int T=10;    scanf("%d",&T);
    while(s2(n,dim) != EOF){
        mem(f,0);
        for(int i = 1; i <= n ;i++)
            for(int j = 0; j < dim; j++){
                s1(data[i].v[j]);
            }
        build( 1,  n, 1, 0);
        int t;
        s1(t);
        //no te;
        while(t--){
        //    cout<<"jin"<<endl;
            for(int j = 0; j < dim; j++){
                s1(ob.v[j]);
            }
            s1(m);
            query( 1, 0);
            printf("the closest %d points are:\n", m);
            int top = 0;
            while(!que.empty()){
                sta[top++] = que.top().second;
                que.pop();
            } 
             while(top)
            {
                no tmp=sta[--top];
                printf("%d", tmp.v[0]);
                for(int i=1;i<dim;i++)
                    printf(" %d", tmp.v[i]);
                printf("\n");
            }
            
        }
    } 
    


    return 0;
}