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个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一 ...
随机推荐
- lelel-5
一.样式有几种引入方式?link和@import有什么区别? 样式有3种引入方式: 外部样式(外联式Linking):是将网页链接到外部样式表<link rel="stylesheet ...
- P4271 [USACO18FEB]New Barns
题目 P4271 [USACO18FEB]New Barns 做法 这题很长见识啊!! 知识点:两棵树\((A,B)\)联通后,新树的径端点为\(A\)的径端点与\(B\)的径端点的两点 不断加边,那 ...
- copy deepcopy辨析
copy deepcopy讲的是复制源对象的改变对copy出来的对象的影响: 我们寻常意义的复制就是深复制,即将被复制对象完全再复制一遍作为独立的新个体单独存在. 所以改变原有被复制对象不会对已经复制 ...
- 【iOS和HTML 5交互】iOS中加载html5调用html方法和修改html5内容
近期项目开发中用到了这方面的技术了,那我们一起来看看. 1.利用webView控件加载本地html5或者网络上html5 2.设置控制器为webView的代理,遵守协议 3.实现代理方法webView ...
- FTH: (7156): *** Fault tolerant heap shim applied to current process. This is usually due to previous crashes. ***
这两天在Qtcreator上编译程序的时候莫名其妙的出现了FTH: (7156): *** Fault tolerant heap shim applied to current process. T ...
- Variation calling and annotation
Resequencing 302 wild and cultivated accessions identifies genes related to domestication and improv ...
- mysql删除重复记录
Solution 1: Add Unique Index on your table: ALTER IGNORE TABLE `TableA` ADD UNIQUE INDEX (`member_id ...
- CocoaPods安装使用
$ gem sources --remove https://rubygems.org/ //等有反应之后再敲入以下命令 $ gem sources -a http://ruby.taobao.org ...
- 字符串匹配算法BF和KMP总结
背景 来看一道leetcode题目: Implement strStr(). Returns the index of the first occurrence of needle in haysta ...
- cdq分治入门and持续学习orz
感觉cdq分治是一个很有趣的算法 能将很多需要套数据结构的题通过离线来做 目前的一些微小的理解 在一般情况下 就像求三维偏序xyz 就可以先对x排序 然后分治 1 cdq_x(L,M) ; 2 提取出 ...