bzoj2648/2716 kdtree
SJY摆棋子
Time Limit: 20 Sec Memory Limit: 128 MB
Submit: 5199 Solved: 1813
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 1
2 3
2 1 2
1 3 3
2 4 2
Sample Output
2
HINT
kdtree可以过
Source
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstdio> #define inf 1000000007
#define N 500007
#define ll long long
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if (ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,m,rt,F;
struct Node
{
int d[],mn[],mx[],l,r;
int& operator[](int x)
{
return d[x];
}
Node(int x=,int y=)//无代入的话x,y为0,代入即为代入值。
{
l=,r=;
d[]=x,d[]=y;
}
}p[N];
bool operator<(Node x,Node y)
{
return x[F]<y[F];
}
inline int dis(Node x,Node y)
{
return abs(x[]-y[])+abs(x[]-y[]);
}
struct kdtree
{
int ans;
Node tr[N*],T;
void update(int p)
{
Node l=tr[tr[p].l],r=tr[tr[p].r];
for (int i=;i<;i++)
{
if (tr[p].l)tr[p].mn[i]=min(tr[p].mn[i],l.mn[i]),tr[p].mx[i]=max(tr[p].mx[i],l.mx[i]);
if (tr[p].r)tr[p].mn[i]=min(tr[p].mn[i],r.mn[i]),tr[p].mx[i]=max(tr[p].mx[i],r.mx[i]);
}
}
int build(int l,int r,int now)
{
F=now;int mid=(l+r)>>;
nth_element(p+l,p+mid,p+r+);
tr[mid]=p[mid];
for (int i=;i<;i++)
tr[mid].mn[i]=tr[mid].mx[i]=tr[mid][i];
if (l<mid)tr[mid].l=build(l,mid-,now^);
if (r>mid)tr[mid].r=build(mid+,r,now^);
update(mid);
return mid;
}
void ins(int p,int now)
{
if (T[now]>=tr[p][now])
{
if(tr[p].r)ins(tr[p].r,now^);
else
{
tr[p].r=++n,tr[n]=T;
for (int i=;i<;i++)
tr[n].mn[i]=tr[n].mx[i]=tr[n][i];
}
}
else
{
if (tr[p].l)ins(tr[p].l,now^);
else
{
tr[p].l=++n,tr[n]=T;
for (int i=;i<;i++)
tr[n].mn[i]=tr[n].mx[i]=tr[n][i];
}
}
update(p);
}
int get(int k,Node p)
{
int tmp=;
for (int i=;i<;i++)
tmp+=max(,tr[k].mn[i]-p[i]);
for (int i=;i<=;i++)
tmp+=max(,p[i]-tr[k].mx[i]);
return tmp;
}
void query(int p,int now)
{
int d,dl=inf,dr=inf;
d=dis(tr[p],T);
ans=min(ans,d);
if (tr[p].l)dl=get(tr[p].l,T);
if (tr[p].r)dr=get(tr[p].r,T);
if (dl<dr)
{
if (dl<ans)query(tr[p].l,now^);
if (dr<ans)query(tr[p].r,now^);
}
else
{
if (dr<ans)query(tr[p].r,now^);
if (dl<ans)query(tr[p].l,now^);
}
}
int query(Node p)
{
ans=inf,T=p,query(rt,);
return ans;
}
void ins(Node p)
{
T=p;
ins(rt,);
}
}kd;
int main()
{
freopen("fzy.in","r",stdin);
freopen("fzy.out","w",stdout); n=read(),m=read();
for (int i=;i<=n;i++)
p[i][]=read(),p[i][]=read();
rt=kd.build(,n,);//后者0,代表维的循环
while(m--)
{
int flag=read(),x=read(),y=read();
if (flag==) kd.ins(Node(x,y));
else printf("%d\n",kd.query(Node(x,y)));
}
}
bzoj2648/2716 kdtree的更多相关文章
- 【BZOJ-2648&2716】SJY摆棋子&天使玩偶 KD Tree
2648: SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2459 Solved: 834[Submit][Status][Discu ...
- [bzoj2648/2716]SJY摆棋子_KD-Tree
SJY摆旗子 bzoj-2648 题目大意:平面上有n个黑子.有m个操作,可以下一颗白子,查询与曼哈顿距离下最近黑子之间的曼哈顿距离,或者下一颗黑子. 注释:$1\le n,m\le 5\cdot 1 ...
- [bzoj2648/2716]SJY摆棋子
平面上有n个点,要求支持插入一个点和查询一个点的最近点距离 n,m<=500000 用kdtree实现,但是复杂度貌似没法保证.....(莫名加了替罪羊重建更慢了...) #include< ...
- BZOJ2648/2716:SJY摆棋子/[Violet]天使玩偶(K-D Tree)
Description 这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一个白色棋子,如果是白色棋子,他会找出距离这个白色棋子最近的黑色棋子. ...
- BZOJ 2716 [Violet 3]天使玩偶 ——KD-Tree
[题目分析] KD-Tree的例题.同BZOJ2648. [代码] #include <cstdio> #include <cstring> #include <cstd ...
- 【BZOJ】2648: SJY摆棋子 & 2716: [Violet 3]天使玩偶(kdtree)
http://www.lydsy.com/JudgeOnline/problem.php?id=2716 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- BZOJ2648 SJY摆棋子(KD-Tree)
板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- bzoj 2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 --kdtree
2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 Time Limit: 20 Sec Memory Limit: 128 MB Description 这天,S ...
- 【BZOJ2648】SJY摆棋子(KD-Tree)
[BZOJ2648]SJY摆棋子(KD-Tree) 题面 BZOJ Description 这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一 ...
随机推荐
- ftpclient 遇到的一些问题
1. FTPFile[] files=ftpClient.listFiles(ftpDirectory); 没有数据 public static boolean ftpLogin(String ser ...
- Spring 配置定时器(注解+xml)方式—整理
一.注解方式 1. 在Spring的配置文件ApplicationContext.xml,首先添加命名空间 xmlns:task="http://www.springframework.or ...
- (三)mybatis之对Hibernate初了解
前言:为什么会写Hibernate呢?因为HIbernate跟Mybatis一样,是以ORM模型为核心思想的,但是这两者有相似的地方也有差异的地方.通过这两种框架的比对,可以对mybatis有着更深的 ...
- Windows Dos命令下查看端口号,杀死端口
PS:本文以 Redis 默认端口 6379 为例 1,首先查询该端口的 pid,使用命令 [netstat -ano | findstr 端口号] F:\Program Files\Redi ...
- UVA - 658 It's not a Bug, it's a Feature! (隐式图的最短路,位运算)
隐式的图搜索,存不下边,所以只有枚举转移就行了,因为bug的存在状态可以用二进制表示,转移的时候判断合法可以用位运算优化, 二进制pre[i][0]表示可以出现的bug,那么u&pre[i][ ...
- Linux中fork函数的例子
- WPF知识点全攻略10- 路由事件
路由事件是WPF不得不提,不得不会系列又一 先来看一下他的定义: 功能定义:路由事件是一种可以针对元素树中的多个侦听器(而不是仅针对引发该事件的对象)调用处理程序的事件. 实现定义:路由事件是一个 C ...
- 浏览器输入一个url到整个页面显示出来经历了哪些过程?
https://cloud.tencent.com/developer/article/1396399 https://www.cnblogs.com/haonanZhang/p/6362233.ht ...
- ubuntu 18.* 重启网卡
systemctl UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded active waiting A ...
- ios之UIButoon
第一.UIButton的定义 UIButton *button=[[UIButton buttonWithType:(UIButtonType); 能够定义的button类型有以下6种, typede ...