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]:这 ...
随机推荐
- js函数的caller属性
funcName.caller : 返回一个对函数的引用, 该函数调用了当前函数 function test() { if (test.caller) { var a = test.caller.to ...
- Unicode Category
Lu(letter,uppercase):大写字母. Ll(letter.lowercase):小写字母. Lt(letter,titlecase):词首字母大写的字母. Lm(letter,modi ...
- linux c编程:信号(三) sigprocmask和sigpending函数
信号源为目标进程产生了一个信号,然后由内核来决定是否要将该信号传递给目标进程.从信号产生到传递给目标进程的流程图如下图所示: 进程可以阻塞信号的传递.当信号源为目标进程产生了一个信号之后,内核会执行依 ...
- java基础入门之九九乘法表
/* 自学java 九九乘法表 Power by Stuart Date: 2015.4.23 */public class Math { public static void main (Strin ...
- 网络的分层协议总结(转发:https://www.cnblogs.com/Zhang-wj/p/5907534.html)
网络的分层协议总结 OSI七层模型OSI 中的层 功能 TCP/IP ...
- Intel Quick Sync Video Encoder
本篇记录Intel E3 1275处理器集成显卡的硬编码预研过程. 步骤如下: (1)环境搭建 (2)demo编译,测试 (3)研究demo源码,Media SDK API使用 (4)编写so动态库封 ...
- this.Hide()与this.Visible、Application.Exit与this.Close()区别
首先明确this.Hide()和this.Visible作用完全一样,都是隐藏当前窗体,使其不可见:Application.Exit与this.Close()虽然都有关闭当前应用的作用,但作用不同. ...
- sublime text的pylinter插件设置pylint_rc后提示错误
sublime text插件pylinter提示错误 Warning: option include-ids is deprecated and ignored. 错误本身是Python的错误,这说明 ...
- 暑假集训第一周比赛G题
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83146#problem/G G - 向 Crawling in process... C ...
- 大话设计模式--中介者模式 Mediator --C++实现实例
1. 中介者模式: 用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立改变他们之间的交互. Mediator的出现减少了各个Colleague的耦 ...