学习了一下kd-tree的基本写法 http://blog.csdn.net/jiangshibiao/article/details/34144829 配合 http://www.bilibili.com/video/av7039143/ 食用

不过这个博客的里面那道2648的代码是错的 应该加上一句if(n)build 否则应该在插入第一个黑点的时候建树

kd-tree的写法还是很简单的 如果学习过线段树 应该很容易就学会了模板了

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<math.h>
#include<queue>
#include<string>
using namespace std;
#define L long long
const int INF = 999999999 ;
int n , m , root , cmp_d ;
int x, y ;
struct node{
int d[2] , Max[2] , Min[2] ;
int l, r ;
}tr[1000050];
bool cmp(node a , node b){
return a.d[cmp_d] < b.d[cmp_d] || a.d[cmp_d] == b.d[cmp_d] && a.d[cmp_d^1] < b.d[cmp_d^1];
}
void up(int p,int k){
if(tr[k].Max[0] > tr[p].Max[0]) tr[p].Max[0] = tr[k].Max[0] ;
if(tr[k].Max[1] > tr[p].Max[1]) tr[p].Max[1] = tr[k].Max[1] ;
if(tr[k].Min[0] < tr[p].Min[0]) tr[p].Min[0] = tr[k].Min[0] ;
if(tr[k].Min[1] < tr[p].Min[1]) tr[p].Min[1] = tr[k].Min[1] ;
}
int build(int l,int r, int D){
int mid = (l+r) >> 1;
cmp_d = D;
nth_element(tr+l+1,tr+mid+1,tr+r+1,cmp) ;
tr[mid].Max[0] = tr[mid].Min[0] = tr[mid].d[0];
tr[mid].Max[1] = tr[mid].Min[1] = tr[mid].d[1];
if(l!=mid)
tr[mid].l = build(l,mid-1,D^1) ;
else tr[mid].l = 0;
if(r!=mid)
tr[mid].r = build(mid+1,r,D^1);
else tr[mid].r = 0;
if(tr[mid].l)up(mid,tr[mid].l);
if(tr[mid].r)up(mid,tr[mid].r);
return mid ;
}
void inse(int k){
int p = root ;
int D = 0 ;
while(true){
up(p,k);
if(tr[k].d[D] <= tr[p].d[D]){
if(!tr[p].l){
tr[p].l = k ;
return;
}
p = tr[p].l ;
}
else {
if(!tr[p].r){
tr[p].r = k ;
return;
}
p = tr[p].r ;
}
D ^= 1;
}
}
int ans ;
int getdis(int p,int x,int y){
int res = 0;
if(x > tr[p].Max[0])res += x - tr[p].Max[0];
if(x < tr[p].Min[0])res += tr[p].Min[0] - x;
if(y > tr[p].Max[1])res += y - tr[p].Max[1];
if(y < tr[p].Min[1])res += tr[p].Min[1] - y;
return res ;
}
void ask(int p){
int d0 = abs(x - tr[p].d[0]) + abs(y - tr[p].d[1]) ;
if(d0<ans)ans = d0 ;
int dl , dr ;
if(tr[p].l)dl=getdis(tr[p].l,x,y) ;else dl = INF ;
if(tr[p].r)dr=getdis(tr[p].r,x,y) ;else dr = INF ;
if(dl < dr){
if(dl < ans)ask(tr[p].l) ;
if(dr < ans)ask(tr[p].r) ;
}
else {
if(dr < ans)ask(tr[p].r) ;
if(dl < ans)ask(tr[p].l) ;
}
} int main(){
scanf("%d%d",&n,&m);
for(int i = 1; i <= n ; i ++ ){
scanf("%d%d",&tr[i].d[0] , &tr[i].d[1]);
}
if(n)
root = build(1,n,0) ;
for(int i = 1; i <= m ; i ++ ){
int q;
scanf("%d%d%d",&q,&x,&y);
if(q == 1){
n ++ ;
tr[n].d[0]=tr[n].Max[0]=tr[n].Min[0]=x;
tr[n].d[1]=tr[n].Max[1]=tr[n].Min[1]=y;
if(n>1)
inse(n);
else {
root = build(1,n,0);
}
}
else {
ans = INF;
ask(root);
printf("%d\n",ans);
}
}
}

  

BZOJ 2648 kd-tree模板的更多相关文章

  1. k-d tree模板练习

    1. [BZOJ]1941: [Sdoi2010]Hide and Seek 题目大意:给出n个二维平面上的点,一个点的权值是它到其他点的最长距离减最短距离,距离为曼哈顿距离,求最小权值.(n< ...

  2. BZOJ 2648 / 2716 K-D Tree 模板题

    #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...

  3. BZOJ - 2648 KD树 最近点查询

    省赛后躺尸几天又回来更新了,内容是说好的KD树.. 具体操作从代码中感受一下 感觉已经把KD树尽量封装好了(虽然全局的D看着极不顺眼) 需要注意的是估值函数的判断条件 #include<bits ...

  4. 【BZOJ-2648&2716】SJY摆棋子&天使玩偶 KD Tree

    2648: SJY摆棋子 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2459  Solved: 834[Submit][Status][Discu ...

  5. kd-tree注解 &amp; bzoj 2648 &amp; 2716 &amp; 3053 解决问题的方法

    [KD-TREE简介]于SYC1999大神"迷住"下一个.我开始接触这样的算法. 首先.这个概念大概能去百度百科.详细的实施.我在看RZZ的代码长大的. 我们能够想象在平面上有N个 ...

  6. BZOJ2648/2716:SJY摆棋子/[Violet]天使玩偶(K-D Tree)

    Description 这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一个白色棋子,如果是白色棋子,他会找出距离这个白色棋子最近的黑色棋子. ...

  7. BZOJ 2648: SJY摆棋子(K-D Tree)

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 6051  Solved: 2113[Submit][Status][Discuss] Descript ...

  8. [模板] K-D Tree

    K-D Tree K-D Tree可以看作二叉搜索树的高维推广, 它的第 \(k\) 层以所有点的第 \(k\) 维作为关键字对点做出划分. 为了保证划分均匀, 可以以第 \(k\) 维排名在中间的节 ...

  9. BZOJ 3489: A simple rmq problem(K-D Tree)

    Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 2579  Solved: 888[Submit][Status][Discuss] Descripti ...

  10. BZOJ 3053: The Closest M Points(K-D Tree)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1235  Solved: 418[Submit][Status][Discuss] Descripti ...

随机推荐

  1. 浅谈远程登录时,ssh的加密原理

    SSH:Secure Shell,是一种网络安全协议,主要用于登录远程计算机的加密过程. 登录方式主要有两种: 1.基于用户密码的登录方式:   加密原理:   当服务器知道用户请求登录时,服务器会把 ...

  2. Directed Graph Loop detection and if not have, path to print all path.

    这里总结针对一个并不一定所有点都连通的general directed graph, 去判断graph里面是否有loop存在, 收到启发是因为做了[LeetCode] 207 Course Sched ...

  3. Are you looking forward to this 11s Black Stingray

    The Derek Jeter Air Jordan 11 Navy Suede has quietly dropped a number of various colorways over the ...

  4. js实现轮播图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. java反射之根据全类名创建对象

    现在的需求是根据类的全名.来创建对象 package 中介者设计模式; import java.util.Date; public class CreateObject { public static ...

  6. JQuery Form AjaxSubmit(options)在Asp.net中的应用注意事项

    所需引用的JS: 在http://www.malsup.com/jquery/form/#download 下载:http://malsup.github.com/jquery.form.js 在ht ...

  7. mysql参数配置文件

    (1)参数配置文件中的内容以键值对形式存在. (2)如何查看键值对?show variables like '%name%';或者查看information_schema库下的global_varia ...

  8. CAScrollLayer

    CAScrollLayer 对于一个未转换的图层,它的bounds和它的frame是一样的,frame属性是由bounds属性自动计算而出的,所以更改任意一个值都会更新其他值. 但是如果你只想显示一个 ...

  9. SoapUI 使用变量

    登录问题不好解决, 只能临时用cookie来执行 1.变量定义 2.引用变量 3.调用Header

  10. Linux命令: ls -l显示文件和目录的详细资料

    ls -l 显示文件和目录的详细资料