BZOJ 2648 kd-tree模板
学习了一下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模板的更多相关文章
- k-d tree模板练习
1. [BZOJ]1941: [Sdoi2010]Hide and Seek 题目大意:给出n个二维平面上的点,一个点的权值是它到其他点的最长距离减最短距离,距离为曼哈顿距离,求最小权值.(n< ...
- BZOJ 2648 / 2716 K-D Tree 模板题
#include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...
- BZOJ - 2648 KD树 最近点查询
省赛后躺尸几天又回来更新了,内容是说好的KD树.. 具体操作从代码中感受一下 感觉已经把KD树尽量封装好了(虽然全局的D看着极不顺眼) 需要注意的是估值函数的判断条件 #include<bits ...
- 【BZOJ-2648&2716】SJY摆棋子&天使玩偶 KD Tree
2648: SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2459 Solved: 834[Submit][Status][Discu ...
- kd-tree注解 & bzoj 2648 & 2716 & 3053 解决问题的方法
[KD-TREE简介]于SYC1999大神"迷住"下一个.我开始接触这样的算法. 首先.这个概念大概能去百度百科.详细的实施.我在看RZZ的代码长大的. 我们能够想象在平面上有N个 ...
- BZOJ2648/2716:SJY摆棋子/[Violet]天使玩偶(K-D Tree)
Description 这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一个白色棋子,如果是白色棋子,他会找出距离这个白色棋子最近的黑色棋子. ...
- BZOJ 2648: SJY摆棋子(K-D Tree)
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 6051 Solved: 2113[Submit][Status][Discuss] Descript ...
- [模板] K-D Tree
K-D Tree K-D Tree可以看作二叉搜索树的高维推广, 它的第 \(k\) 层以所有点的第 \(k\) 维作为关键字对点做出划分. 为了保证划分均匀, 可以以第 \(k\) 维排名在中间的节 ...
- BZOJ 3489: A simple rmq problem(K-D Tree)
Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 2579 Solved: 888[Submit][Status][Discuss] Descripti ...
- BZOJ 3053: The Closest M Points(K-D Tree)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1235 Solved: 418[Submit][Status][Discuss] Descripti ...
随机推荐
- SQLCE数据工具(Flyhoward Ltd SDF Viewer)
SDF Viewer sdf数据库创建编辑查看 官方下载地址 http://www.flyhoward.com/Download_SDF_Viewer.aspx 用户名:www.cr173.com注 ...
- abap 基本知识
sap gui 安装教程:http://www.itpub.net/forum.php?mod=viewthread&tid=2090890 1:abap 基本数据类型: i(整型),f(浮点 ...
- Scala中的数组和集合操作
package test /* 1.在scala集合中,Iterable是共同的Trait,Iterable要求继承者实现一些共同的方法,例如元素的遍历 * 2.Array是scala基础的数据结构, ...
- POJ:1182 食物链(带权并查集)
http://poj.org/problem?id=1182 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1 ...
- 更改docker服务网段分配地址
docker安装完毕后,会自动生成一个网卡名为docker0的网桥,如果其默认分配的网段地址和已有地址段冲突,可按如下步骤修改. 查看默认地址段如下 docker0: flags=4099<UP ...
- Groovy介绍
关于 Groovy 这一节将学习 Groovy 的基础知识:它是什么,它与 Java 语言和 JVM 的关系,以及编写 Groovy 代码的一些要点. 一.什么是 Groovy? Groovy 是 J ...
- [LeetCode] 197. Rising Temperature_Easy tag: SQL
Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...
- Leetcode: Longest Consecutive Sequence && Summary: Iterator用法以及ConcurrentModificationException错误说明
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- java常用功能
1.复制文件 private void fileChannelCopy(File source, File target) throws IOException { FileInputStream f ...
- Hive 大数据倾斜总结
在做Shuffle阶段的优化过程中,遇 到了数据倾斜的问题,造成了对一些情况下优化效果不明显.主要是因为在Job完成后的所得到的Counters是整个Job的总和,优化是基于这些 Counters得出 ...