KDTree 板子
2024-08-25 17:04:03
从杨哥哪里偷的板子, 存一下。
#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 ;
}
最新文章
- pom.xml详解
- 在aspx怎么引用public string getPicurl(string picurl)?
- nrf51822-配对绑定实现过程
- MyEclipse8.5集成Tomcat7
- UIKit框架之UIlabel
- 多线程查询FTP Server上的文件
- CLRS:build_max_heap(strorage in array)
- SBM is Not Sale And Run Company
- jquery 消息提醒插件 toastmessage
- Eclipse插件安装总结
- echarts.js(图表插件)2.0版会导致 ZeroClipboard.js(复制插件)失效,3.0版未知。
- gulp和webpack初探
- idea intellij 快捷键(ubuntu版本)
- elasticsearch 修改内存
- 线程:ThreadLocal实现线程范围内共享变量
- iOS项目中常见的文件
- 学习笔记TF049:TensorFlow 模型存储加载、队列线程、加载数据、自定义操作
- iOS 网络监听、判断
- chrome浏览器被reimage pair 劫持怎么处理
- Python 3安装MySQLdb