BZOJ2648:SJY摆棋子
浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html
题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=2648
\(K-D\) \(Tree\)最近点查询裸题,注意先把所有点建好再一个个激活。
时间复杂度:\(O(nlogn)\)
空间复杂度:\(O(n)\)
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=5e5+5,inf=2e9;
int n,m,pps,ans,node[maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
struct query {
int opt,x,y;
}q[maxn];
struct kd_tree {
int cnt,root;
int fa[maxn<<1];
struct point {
int c[2],mn[2],mx[2],id,ls,rs;
point() {}
point(int _x,int _y,int _id) {
c[0]=_x,c[1]=_y,id=_id;
if(id<=n)mn[0]=mx[0]=c[0],mn[1]=mx[1]=c[1];
else mn[0]=mn[1]=inf,mx[0]=mx[1]=-inf;
ls=rs=0;
}
bool operator<(const point &a)const {
return c[pps]<a.c[pps];
}
}p[maxn<<1];
void update(int u) {
int ls=p[u].ls,rs=p[u].rs;
for(int i=0;i<2;i++) {
int mn=min(p[ls].mn[i],p[rs].mn[i]);
p[u].mn[i]=min(p[u].mn[i],mn);
int mx=max(p[ls].mx[i],p[rs].mx[i]);
p[u].mx[i]=max(p[u].mx[i],mx);
}
}
int build(int l,int r,int cmp) {
int mid=(l+r)>>1,u=mid;pps=cmp;
nth_element(p+l,p+mid,p+r+1);
if(p[u].id>n)node[p[u].id-n]=u;
if(l<mid)fa[p[u].ls=build(l,mid-1,cmp^1)]=u;
if(r>mid)fa[p[u].rs=build(mid+1,r,cmp^1)]=u;
update(u);
return u;
}
void prepare() {
p[0]=point(0,0,2e9);
for(int i=1;i<=n;i++) {
int x=read(),y=read();
p[i]=point(x,y,i);
}
for(int i=1;i<=m;i++) {
int opt=read(),x=read(),y=read();
q[i].opt=opt,q[i].x=x,q[i].y=y;
if(opt==1)p[++cnt]=point(x,y,n+i);
}
root=build(1,cnt,0);
}
void arouse(int u) {
p[u].mn[0]=p[u].mx[0]=p[u].c[0];
p[u].mn[1]=p[u].mx[1]=p[u].c[1];
while(fa[u])update(u),u=fa[u];
update(u);
}
int dis(int id,int x,int y) {
int res=0;
if(x<p[id].mn[0])res+=p[id].mn[0]-x;
if(x>p[id].mx[0])res+=x-p[id].mx[0];
if(y<p[id].mn[1])res+=p[id].mn[1]-y;
if(y>p[id].mx[1])res+=y-p[id].mx[1];
return res;
}
void query(int u,int x,int y,int id) {
if(p[u].id<id)ans=min(ans,abs(x-p[u].c[0])+abs(y-p[u].c[1]));
int dl=p[u].ls?dis(p[u].ls,x,y):inf;
int dr=p[u].rs?dis(p[u].rs,x,y):inf;
if(dl<dr) {
if(dl<ans)query(p[u].ls,x,y,id);
if(dr<ans)query(p[u].rs,x,y,id);
}
else {
if(dr<ans)query(p[u].rs,x,y,id);
if(dl<ans)query(p[u].ls,x,y,id);
}
}
}T;
int main() {
T.cnt=n=read(),m=read();
T.prepare();
for(int i=1;i<=m;i++)
if(q[i].opt==1)T.arouse(node[i]);
else {
ans=2e9;
T.query(T.root,q[i].x,q[i].y,n+i);
printf("%d\n",ans);
}
return 0;
}
BZOJ2648:SJY摆棋子的更多相关文章
- BZOJ2648: SJY摆棋子&&2716: [Violet 3]天使玩偶
BZOJ2648: SJY摆棋子 BZOJ2716: [Violet 3]天使玩偶 BZOJ氪金无极限... 其实这两道是同一题. 附上2648的题面: Description 这天,SJY显得无聊. ...
- [BZOJ2648] SJY摆棋子 kd-tree
2648: SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 5421 Solved: 1910[Submit][Status][Disc ...
- Bzoj2648 SJY摆棋子
Time Limit: 20 Sec Memory Limit: 128 MB Submit: 3128 Solved: 1067 Description 这天,SJY显得无聊.在家自己玩.在一个 ...
- BZOJ2648 SJY摆棋子(KD-Tree)
板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- 【kd-tree】bzoj2648 SJY摆棋子
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define ...
- 2019.01.14 bzoj2648: SJY摆棋子(kd-tree)
传送门 kd−treekd-treekd−tree模板题. 题意简述:支持在平面上插入一个点,求对于一个点的最近点对. 思路:cdqcdqcdq是一种很不错的分治方法 只是好像码量有点窒息 所以我用了 ...
- KDTree(Bzoj2648: SJY摆棋子)
题面 传送门 KDTree 大概就是一个分割\(k\)维空间的数据结构,二叉树 建立:每层选取一维为关键字,把中间的点拿出来,递归左右,有个\(STL\)函数nth_element可以用一下 维护:维 ...
- [bzoj2648]SJY摆棋子(带插入kd-tree)
解题关键:带插入kdtree模板题. #include<iostream> #include<cstdio> #include<cstring> #include& ...
- luogu4169 [Violet]天使玩偶/SJY摆棋子 / bzoj2648 SJY摆棋子 k-d tree
k-d tree + 重构的思想,就能卡过luogu和bzoj啦orz #include <algorithm> #include <iostream> #include &l ...
- 【BZOJ2648】SJY摆棋子(KD-Tree)
[BZOJ2648]SJY摆棋子(KD-Tree) 题面 BZOJ Description 这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一 ...
随机推荐
- 防止iframe被别的网站引用
try{ top.location.hostname; if (top.location.hostname != window.location.hostname) { top.location.hr ...
- uboot无法引导uImage错误及其解决方法
先编译友善提供的linux内核: make ARCH=arm mini2440_defconfigmake CROSS_COMPILE=arm-linux- uImage 在arch/arm/boot ...
- 【转】linux驱动开发
转自:http://www.cnblogs.com/heat-man/articles/4174899.html 首先理一理驱动/内核/应用程序的一些概念,以前总没有具体的去关注过! 我们的pc直观来 ...
- Cgroups控制cpu,内存,io示例【转】
本文转载自:https://www.cnblogs.com/yanghuahui/p/3751826.html 百度私有PaaS云就是使用轻量的cgoups做的应用之间的隔离,以下是关于百度架构师许立 ...
- java写出图形界面
1. 做出简单的窗体 package javaGUI; import java.awt.BorderLayout; import java.awt.Color; import javax.swing. ...
- springmvc异常处理(非注解与注解)
1.异常 程序中的异常一般分为两类:预期异常,运行时异常.前者是我们可预知的,我们一般通过捕获和抛出方式处理这些异常.后者主要通过代码规范.测试等手段来减少异常的发生.一般,我们在系统的DAO.Ser ...
- Python之单例模式总结
一.单例模式 a.单例模式分为四种:文件,类,基于__new__方法实现单例模式,基于metaclass方式实现 b.类实现如下: class Sigletion(objects): import t ...
- JavaScript文件下载 兼容所有浏览器 不可跨域
前端文件下载 兼容所有浏览器 download.js文件下载,几乎支持所有类型下载,详细内容参考官网 http://danml.com/download.html 引入文件 <script sr ...
- EDID真实数据块,请参考标准文档仔细核对
数据格式的详细说明:http://en.wikipedia.org/wiki/Extended_display_identification_data 下面是一个例子:
- this license has been cancelled
是因为IDEA注册码的问题, 解决方案: 修改此路径的hosts文件:C:\Windows\System32\drivers\etc\hosts 在其最后一行加入:“0.0.0.0 account.j ...