bzoj 2648 SJY摆棋子 —— K-D树
题目: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树的更多相关文章
- BZOJ 2648 SJY摆棋子(KD树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2716 [题目大意] 给出一些点,同时不断插入点和询问某点离插入点最近距离 [题解] 我 ...
- BZOJ 2648: SJY摆棋子
2648: SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2968 Solved: 1011[Submit][Status][Disc ...
- BZOJ 2648: SJY摆棋子 kdtree
2648: SJY摆棋子 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2648 Description 这天,SJY显得无聊.在家自己玩 ...
- bzoj 2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 --kdtree
2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 Time Limit: 20 Sec Memory Limit: 128 MB Description 这天,S ...
- bzoj 2648 SJY摆棋子 kd树
题目链接 初始的时候有一些棋子, 然后给两种操作, 一种是往上面放棋子. 另一种是给出一个棋子的位置, 问你离它最近的棋子的曼哈顿距离是多少. 写了指针版本的kd树, 感觉这个版本很好理解. #inc ...
- BZOJ 2648: SJY摆棋子(K-D Tree)
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 6051 Solved: 2113[Submit][Status][Discuss] Descript ...
- BZOJ 2648 SJY摆棋子(KD Tree)
http://www.lydsy.com/JudgeOnline/problem.php?id=2648 题意: 思路: KDtree模板题. 参考自http://www.cnblogs.com/ra ...
- bzoj 2648 SJY摆棋子——KDtree
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 第一道KDtree! 学习资料:https://blog.csdn.net/zhl30 ...
- bzoj 2648: SJY摆棋子【KD-tree】
其实理论上cdq更优 核心是依次取x值.y值的mid作为当前节点,向两边递归建立二叉树,树上维护size:子树大小:mx[0/1]:子树内最大x/y:mn[0/1]:子树内最小x/y:d[0/1]:这 ...
随机推荐
- hive深入使用
Hive表的创建和数据类型 https://cwiki.apache.org/confluence/display/Hive/Home 管理表和外部的区别 # 管理表 1. 内部表也称之为MANAGE ...
- elasticsearch从入门到出门-06-剖析Elasticsearch的基础分布式架构
这个图来自中华石杉:
- 使用weka训练一个分类器
1 训练集数据 1.1 csv格式 5.1,3.5,1.4,0.2,Iris-setosa 4.9,3.0,1.4,0.2,Iris-setosa 4.7,3.2,1.3,0.2,Iris-setos ...
- 我的Android进阶之旅------>启动Activity的标准Action和标准Category
Android内部提供了大量标准的Action和Category常量. 除了参考本文外,您还可以参考了以下链接: http://developer.android.com/reference/andr ...
- Linux c编程:线程属性
前面介绍了pthread_create函数,并且当时的例子中,传入的参数都是空指针,而不是指向pthread_attr_t结构的指针.可以使用pthread_attr_t结构修改线程默认属性,并把这些 ...
- Python——Numpy的random子库
NumPy的random子库 np.random.* np.random.rand() np.random.randn() np.random.randint() import numpy as np ...
- zend 和 esftp插件开发大型PHP项目,ZEND最常用快捷键
先说一下如何安装zend的esftp插件,下载插件esftp-1.1.1.zip,下载地址http://sourceforge.net/projects/esftp/ 或者 http://yun.ba ...
- Spring笔记:IOC基础
Spring笔记:IOC基础 引入IOC 在Java基础中,我们往往使用常见关键字来完成服务对象的创建.举个例子我们有很多U盘,有金士顿的(KingstonUSBDisk)的.闪迪的(SanUSBDi ...
- 模仿jquery框架源码 -成熟---选择器
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- Python 不可变对象
str是不变对象,而list是可变对象. 对于可变对象,比如list,对list进行操作,list内部的内容是会变化的,比如: >>> a = ['c', 'b', 'a'] > ...