题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648

学习资料:https://blog.csdn.net/zhl30041839/article/details/9277807

https://www.cnblogs.com/galaxies/p/kdtree.html

模仿代码:https://blog.csdn.net/lych_cys/article/details/50809141

那个求点到块的距离的方法挺妙的。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define mid ((l+r)>>1)
using namespace std;
int const xn=5e5+,inf=1e9;
int n,m,rt,cnt,dm,c[xn<<][],ans;
struct N{int mn[],mx[],p[];}a[xn],t[xn<<];
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=ret*+ch-'',ch=getchar();
return f?ret:-ret;
}
int Min(int x,int y){return x<y?x:y;}
int Max(int x,int y){return x<y?y:x;}
int Abs(int x){return x>?x:-x;}
bool cmp(N a,N b){return a.p[dm]<b.p[dm];}
void turn(int x,N y){for(int i=;i<;i++)t[x].mn[i]=t[x].mx[i]=t[x].p[i]=y.p[i];}
void pushup(int x)
{
int ls=c[x][],rs=c[x][];
for(int i=;i<;i++)
{
if(ls)t[x].mn[i]=Min(t[x].mn[i],t[ls].mn[i]),t[x].mx[i]=Max(t[x].mx[i],t[ls].mx[i]);
if(rs)t[x].mn[i]=Min(t[x].mn[i],t[rs].mn[i]),t[x].mx[i]=Max(t[x].mx[i],t[rs].mx[i]);
}
}
void build(int &x,int l,int r,int nw)
{
x=++cnt; dm=nw;
nth_element(a+l,a+mid,a+r+,cmp);
turn(x,a[mid]);
if(mid>l)build(c[x][],l,mid-,nw^);
if(mid<r)build(c[x][],mid+,r,nw^);
pushup(x);
}
void ins(int x,N v,int nw)
{
if(v.p[nw]<=t[x].p[nw])
{
if(c[x][])ins(c[x][],v,nw^);
else c[x][]=++cnt,turn(c[x][],v);
}
else
{
if(c[x][])ins(c[x][],v,nw^);
else c[x][]=++cnt,turn(c[x][],v);
}
pushup(x);
}
int dis(int x,N y)
{
int ret=;
for(int i=;i<;i++)
ret+=Max(,t[x].mn[i]-y.p[i]),ret+=Max(,y.p[i]-t[x].mx[i]);
return ret;
}
int query(int x,N v)
{
ans=min(ans,Abs(t[x].p[]-v.p[])+Abs(t[x].p[]-v.p[]));
int d1=(c[x][]?dis(c[x][],v):inf),d2=(c[x][]?dis(c[x][],v):inf);
if(d1<=d2){if(d1<ans)query(c[x][],v); if(d2<ans)query(c[x][],v);}
else {if(d2<ans)query(c[x][],v); if(d1<ans)query(c[x][],v);}
}
int main()
{
n=rd(); m=rd(); N tmp;
for(int i=;i<=n;i++)a[i].p[]=rd(),a[i].p[]=rd();
build(rt,,n,);
for(int i=,op;i<=m;i++)
{
op=rd(); tmp.p[]=rd(); tmp.p[]=rd();
if(op==)ins(rt,tmp,);
else ans=inf,query(rt,tmp),printf("%d\n",ans);
}
return ;
}

bzoj 2648 SJY摆棋子 —— K-D树的更多相关文章

  1. BZOJ 2648 SJY摆棋子(KD树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2716 [题目大意] 给出一些点,同时不断插入点和询问某点离插入点最近距离 [题解] 我 ...

  2. BZOJ 2648: SJY摆棋子

    2648: SJY摆棋子 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2968  Solved: 1011[Submit][Status][Disc ...

  3. BZOJ 2648: SJY摆棋子 kdtree

    2648: SJY摆棋子 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2648 Description 这天,SJY显得无聊.在家自己玩 ...

  4. bzoj 2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 --kdtree

    2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 Time Limit: 20 Sec  Memory Limit: 128 MB Description 这天,S ...

  5. bzoj 2648 SJY摆棋子 kd树

    题目链接 初始的时候有一些棋子, 然后给两种操作, 一种是往上面放棋子. 另一种是给出一个棋子的位置, 问你离它最近的棋子的曼哈顿距离是多少. 写了指针版本的kd树, 感觉这个版本很好理解. #inc ...

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

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

  7. BZOJ 2648 SJY摆棋子(KD Tree)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2648 题意: 思路: KDtree模板题. 参考自http://www.cnblogs.com/ra ...

  8. bzoj 2648 SJY摆棋子——KDtree

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 第一道KDtree! 学习资料:https://blog.csdn.net/zhl30 ...

  9. bzoj 2648: SJY摆棋子【KD-tree】

    其实理论上cdq更优 核心是依次取x值.y值的mid作为当前节点,向两边递归建立二叉树,树上维护size:子树大小:mx[0/1]:子树内最大x/y:mn[0/1]:子树内最小x/y:d[0/1]:这 ...

随机推荐

  1. ArcGIS API for JavaScript Bookmarks(书签)

    说明:本篇博文介绍的是ArcGIS API for JavaScript中的 Bookmarks(书签) ,书签的作用是,把地图放大到一个地方 添加书签,书签名称可以和地图名称一直,单击标签 地图会定 ...

  2. React-Native 安装改变镜像

    3.安装完node后建议设置npm镜像以加速后面的过程,否则后面插件安装巨慢 npm config set registry https://registry.npm.taobao.org --glo ...

  3. phpstorm+xdebug, 实现断点调试: xdebug如何配置

    [XDebug] xdebug.profiler_output_dir="D:\phpStudy\tmp\xdebug" xdebug.trace_output_dir=" ...

  4. 【整理学习Hadoop】Hadoop学习基础之一:服务器集群技术

            服务器集群就是指将很多服务器集中起来一起进行同一种服务,在客户端看来就像是只有一个服务器.集群可以利用多个计算机进行并行计算从而获得很高的计算速度,也可以用多个计算机做备份,从而使得任 ...

  5. 转 Oracle 11g服务详细介绍

    转自http://www.cnblogs.com/userWuLiang/archive/2013/04/13/3017900.html Oracle 11g服务详细介绍及哪些服务是必须开启的? 成功 ...

  6. 深入理解利用new创建对象的执行过程以Person p=new Person("张三",20);为例

    代码如下: class Person { private String name="haha"; private int age; private static String co ...

  7. [原创]Scala学习:函数的定义

    方式一:标准的定义函数 def 函数名(参数1: 参数类型,参数2: 参数类型): 返回值类型 = { 函数体 } 例子 def max(x: Int,y: Int): Int ={ if(x > ...

  8. python3 mysql 多表查询

    python3 mysql 多表查询 一.准备表 创建二张表: company.employee company.department #建表 create table department( id ...

  9. 吴恩达机器学习笔记(四) —— BP神经网络

    主要内容: 一.模型简介 二.一些变量所代表的含义 三.代价函数 四.Forward Propagation 五.Back Propagation 六.算法流程 待解决问题: 视频中通过指出:当特征变 ...

  10. 【转】HTTP方法

    [转]HTTP方法 掌握HTTP虽然不是必须的,但是如果你知道它的工作原理,那么在学习JSP开发中的某些知识就可以易如反掌了. 一,HTTP协议详解之URL篇 http(超文本传输协议)是一个基于请求 ...