【kd-tree】bzoj3053 The Closest M Points
同p2626。由于K比较小,所以不必用堆。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef double db;
#define N 50001
#define INF 2147483647.0
#define KD 5//ά¶ÈÊý
int qp[KD];
int n,root,kd,K;
int dn;
struct Ans
{
int p[KD];
db d;
}ans[10];
int sqr(const int &x){return x*x;}
struct Node
{
int minn[KD],maxx[KD],p[KD];
int ch[2];
void Init()
{
for(int i=0;i<kd;++i)
minn[i]=maxx[i]=p[i];
}
db Dis()
{
db t=0;
for(int i=0;i<kd;++i)
{
t+=(db)sqr(max(0,minn[i]-qp[i]));
t+=(db)sqr(max(0,qp[i]-maxx[i]));
}
return sqrt(t);
}
}T[N<<1];
void Update(int rt)
{
for(int i=0;i<2;++i)
if(T[rt].ch[i])
for(int j=0;j<kd;++j)
{
T[rt].minn[j]=min(T[rt].minn[j],T[T[rt].ch[i]].minn[j]);
T[rt].maxx[j]=max(T[rt].maxx[j],T[T[rt].ch[i]].maxx[j]);
}
}
db Dis(int a[],int b[])
{
db t=0;
for(int i=0;i<kd;++i)
t+=(db)sqr(a[i]-b[i]);
return sqrt(t);
}
bool operator < (const Node &a,const Node &b){return a.p[dn]<b.p[dn];}
int Buildtree(int l=1,int r=n,int d=0)
{
dn=d;
int m=(l+r>>1);
nth_element(T+l,T+m,T+r+1);
T[m].Init();
if(l!=m) T[m].ch[0]=Buildtree(l,m-1,(d+1)%kd);
if(m!=r) T[m].ch[1]=Buildtree(m+1,r,(d+1)%kd);
Update(m);
return m;
}
void Query(int rt=root)
{
db t=Dis(T[rt].p,qp);
for(int i=0;i<K;++i)
if(t<ans[i].d)
{
for(int j=K-1;j>=i+1;--j)
ans[j]=ans[j-1];
ans[i].d=t;
memcpy(ans[i].p,T[rt].p,sizeof(T[rt].p));
break;
}
db dd[2];
for(int i=0;i<2;i++)
if(T[rt].ch[i])
dd[i]=T[T[rt].ch[i]].Dis();
else dd[i]=INF;
bool f=(dd[0]<=dd[1]);
if(dd[!f]<ans[K-1].d && T[rt].ch[!f]) Query(T[rt].ch[!f]);
if(dd[f]<ans[K-1].d && T[rt].ch[f]) Query(T[rt].ch[f]);
}
int q;
int main()
{
// freopen("bzoj3053.in","r",stdin);
// freopen("bzoj3053.out","w",stdout);
while(scanf("%d%d",&n,&kd)!=EOF)
{
for(int i=1;i<=n;++i)
for(int j=0;j<kd;++j)
scanf("%d",&T[i].p[j]);
Buildtree();
root=(1+n>>1);
scanf("%d",&q);
for(;q;--q)
{
for(int i=0;i<kd;++i)
scanf("%d",&qp[i]);
scanf("%d",&K);
for(int i=0;i<K;++i)
ans[i].d=INF;
Query();
printf("the closest %d points are:\n",K);
for(int i=0;i<K;++i)
{
for(int j=0;j<kd-1;++j)
printf("%d ",ans[i].p[j]);
printf("%d\n",ans[i].p[kd-1]);
}
}
for(int i=1;i<=n;++i)
T[i].ch[0]=T[i].ch[1]=0;
}
return 0;
}
【kd-tree】bzoj3053 The Closest M Points的更多相关文章
- 【BZOJ】3053: The Closest M Points(kdtree)
http://www.lydsy.com/JudgeOnline/problem.php?id=3053 本来是1a的QAQ.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!! ...
- BZOJ3053:The Closest M Points(K-D Teee)
Description The course of Software Design and Development Practice is objectionable. ZLC is facing a ...
- 【HDOJ】4347 The Closest M Points
居然是KD解. /* 4347 */ #include <iostream> #include <sstream> #include <string> #inclu ...
- BZOJ3053: The Closest M Points
题解: 我们可以事先在堆里放入插入m个inf然后不断的比较当前值与堆首元素的大小,如果小于的话进入. 估计函数也可以随便写写... query的时候貌似不用保留dir... return 0写在 wh ...
- 【Symmetric Tree】cpp
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
- 【Same Tree】cpp
题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
- 【LSGDOJ1834 Tree】树链剖分
题目描述 给定一个N个结点的无向树,树中的结点按照1...N编号,树中的边按照1...N − 1编号,每条边都赋予一个权值.你需要编写程序支持以下三种操作: 1. CHANGE i v:将i号边 ...
- 【BZOJ4154】Generating Synergy【kd树】
题意 给定一棵以1为根的有根树,初始所有节点颜色为1,每次将距离节点a不超过l的a的子节点染成c,或询问点a的颜色 分析 我们以dfs序为横坐标,深度为纵坐标,建kd树.我们每次更新,都是在kd树中更 ...
- 【BZOJ3489】A simple rmq problem【kd树】
题意 给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直接输出0.我会采取一些措施强制在线. 分析 预处理 ...
随机推荐
- Python绑定方法,未绑定方法,类方法,实例方法,静态方法
>>> class foo(): clssvar=[1,2] def __init__(self): self.instance=[1,2,3] def hehe(self): pr ...
- ThinkPHP 3.2.3心得
个人还是蛮喜欢tp的比其他的php框架轻(只接触过drupal.tp),而且上手容易(struts这种action的方式,对于java程序员来说).目录结构也比较简单易懂,提供的一些函数也比较实用.对 ...
- 让finder显示路径
在控制台输入 defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES 重启finder即可.
- (整理)IIS 7 503 "service unavailable" errors
原文地址:http://mvolo.com/where-did-my-iis7-server-go-troubleshooting-503-quotservice-unavailablequot-er ...
- shell基础
1.显示当前登录linux系统的用户 2.当用户登录到linux系统后,一旦出现提示符时,便可以输入操作命令了.命令可以分为如下两类: (1).bash内置的命令: (2).应用程序. 怎么知道哪些指 ...
- js调用java代码返回解决方案
版权声明:本文为楼主原创文章,未经楼主允许不得转载,如要转载请注明来源. 今天封装一个加密标签,遇到一个问题,我需要对页面上的数据调用java后台代码进行解密,而标签里只能通过js获取到数据,所以就遇 ...
- vc6 编译问题
Compiling...main.cppLinking...MSVCRT.lib(MSVCRT.dll) : error LNK2005: _malloc already defined in LIB ...
- oracle 认证方式
Oracle登录的时候有两种认证方式,一种是“操作系统认证”,一种是“口令文件认证”.1.当采取操作系统认证的时候,在本地用任何用户都可以以sysdba登陆:(默认方式)2.当采取口令文件认证的时候, ...
- 8天掌握EF的Code First开发系列之2 Code First开发系列之领域建模和管理实体关系
本文出自8天掌握EF的Code First开发系列,经过自己的实践整理出来. 本篇目录 理解Code First及其约定和配置 创建数据表结构 管理实体关系 三种继承模式 本章小结 本人的实验环境是V ...
- Windows程序设计(第五版)学习:第三章 窗口与消息
第三章 窗口与消息 1,windows窗口过程:应用程序所创建的每一个窗口都有一个与之关联的窗口过程,用于处理传递给窗口的消息. 2,窗口依据窗口类来创建.窗口类标识了用于处理传递给窗口的消息 ...