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个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一 ...
随机推荐
- CSS3手风琴下拉菜单
在线演示 本地下载
- CSS3手风琴菜单 可同时折叠多个菜单
在线演示 本地下载
- Python 集合set概念和操作
# 集合 # 概念 # 无序的, 不可随机访问的, 不可重复的元素集合 # 与数学中集合的概念类似,可对其进行交.并.差.补等逻辑运算 # 分为可变集合和非可变集合 # set # 为可变集合 # 增 ...
- win10打不开菜单且点击通知栏无反应的解决方法
1.在键盘上按下win+R键,或在开始菜单图标上点击右键选择"运行" 2.输入powershell,按下“确定”运行 3.在窗口里输入或复制粘贴以下命令,注意只有一行: Get-A ...
- shell 读取文件的每一行内容并输出
(1)#!/bin/bash while read linedo echo $linedone < file (2)#!/bin/bash cat file | while read l ...
- nohup后台运行jar与关闭
nohup 用途:LINUX命令用法,不挂断地运行命令. 语法:nohup Command [ Arg ... ] [ & ] 描述:nohup 命令运行由 Command 参数和任何相关 ...
- 智能穿戴设备移动APP端与外设数据传输协议
S1 Communication Layer specifications 1. Purpose of This Document ...
- myEclipse 2014 破解教程
因为经常在不同电脑里安装配置下载myEclipse,所以干脆记录下来,一直找度娘也是很麻烦的. 此教程仅对myEclipse2014 有效. 破解工具:https://pan.baidu.com/s/ ...
- BZOJ3669 [Noi2014]魔法森林(SPFA+动态加边)
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- RPM和yum相关
写在前面: 在这里可以知道rpm和yum的基本用法,找到更新本地yum源.搭建yum源的方法以及yum很琐碎的东西,包括yum源的优先级.用yum来安装或卸载CentOS图形界面包以及保存yum下载的 ...