从杨哥哪里偷的板子, 存一下。

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 5e4 + ;
int n,k,idx;
struct Node{
int f[];
bool operator <(const Node&rhs)const{
return f[idx]<rhs.f[idx];
}
}a[N];
priority_queue<pair<double,Node> >q;
struct kdtree{
bool vis[N<<];
Node data[N<<];
void Build(int l, int r, int rt, int dep){
if(l > r)return ;
vis[rt] = ;
vis[rt<<] = vis[rt<<|] = ;
idx = dep%k;
int m = l+r >> ;
nth_element(a+l, a+m, a+r+);
data[rt] = a[m];
Build(l, m-, rt<<, dep+);
Build(m+, r, rt<<|, dep+);
}
void Query(Node p, int m, int rt, int dep){
if(!vis[rt]) return ;
pair<double,Node>cur(,data[rt]);
for(int i = ; i < k; ++i)
cur.fi += (cur.se.f[i] - p.f[i]) * (cur.se.f[i] - p.f[i]);
int dim = dep % k;
bool flag = ;
int x = rt << ,y = rt << | ;
if(p.f[dim] >= data[rt].f[dim]) swap(x,y);
if(vis[x]) Query(p, m, x, dep+);
if(q.size() < m) q.push(cur),flag = ;
else{
if(cur.fi<q.top().fi){
q.pop();
q.push(cur);
}
if((p.f[dim]-data[rt].f[dim])*(p.f[dim]-data[rt].f[dim])<q.top().fi)
flag=;
}
if(vis[y] && flag) Query(p,m,y,dep+);
}
}kd;
int main(){
while(scanf("%d%d",&n,&k)!=EOF){
for(int i = ;i < n; ++i)
for(int j = ; j < k; ++j)
scanf("%d", &a[i].f[j]);
kd.Build(,n-,,);
int t;
scanf("%d", &t);
while(t--){
Node p;
for(int i = ;i < k; ++i)
scanf("%d", &p.f[i]);
int m;
scanf("%d",&m);
while(!q.empty())
q.pop();
kd.Query(p,m,,);
printf("the closest %d points are:\n", m);
Node ans[];
for(int i = ; !q.empty(); ++i){
ans[i] = q.top().se;
q.pop();
}
for(int i = m - ; i >= ; --i)
for(int j = ; j < k; ++j)
printf("%d%c", ans[i].f[j], " \n"[j==k-]);
}
}
return ;
}

最新文章

  1. pom.xml详解
  2. 在aspx怎么引用public string getPicurl(string picurl)?
  3. nrf51822-配对绑定实现过程
  4. MyEclipse8.5集成Tomcat7
  5. UIKit框架之UIlabel
  6. 多线程查询FTP Server上的文件
  7. CLRS:build_max_heap(strorage in array)
  8. SBM is Not Sale And Run Company
  9. jquery 消息提醒插件 toastmessage
  10. Eclipse插件安装总结
  11. echarts.js(图表插件)2.0版会导致 ZeroClipboard.js(复制插件)失效,3.0版未知。
  12. gulp和webpack初探
  13. idea intellij 快捷键(ubuntu版本)
  14. elasticsearch 修改内存
  15. 线程:ThreadLocal实现线程范围内共享变量
  16. iOS项目中常见的文件
  17. 学习笔记TF049:TensorFlow 模型存储加载、队列线程、加载数据、自定义操作
  18. iOS 网络监听、判断
  19. chrome浏览器被reimage pair 劫持怎么处理
  20. Python 3安装MySQLdb

热门文章

  1. Lexical or preprocessor &#39;XXX/XXX.h&#39; issue file not found
  2. 什么是https?http升级为https需要什么?
  3. Windows 纠错
  4. 如何创建Github创库
  5. javaweb基础整理随笔------jstl与el表达式
  6. react学习(一)--JSX简介
  7. Opengl_入门学习分享和记录_00
  8. 深入浅出Apriori关联分析算法(一)
  9. java后端开发面经 数据库相关
  10. Hive 系列(一)—— Hive 简介及核心概念