#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define N 500001
#define INF 2147483647
#define KD 2//ά¶ÈÊý
int qp[KD],disn;
int n,root;
bool dn;
struct Node
{
int minn[KD],maxx[KD],p[KD];
int ch[2];
void Init()
{
for(int i=0;i<KD;++i)
minn[i]=maxx[i]=p[i];
}
int Dis()
{
int res=0;
for(int i=0;i<KD;++i)
{
res+=max(0,minn[i]-qp[i]);
res+=max(0,qp[i]-maxx[i]);
}
return res;
}
}T[N<<1];
void Update(int rt)
{
for(int i=0;i<2;++i)
if(T[rt].ch[i])
for(int j=0;j<KD;++j)
{
T[rt].minn[j]=min(T[rt].minn[j],T[T[rt].ch[i]].minn[j]);
T[rt].maxx[j]=max(T[rt].maxx[j],T[T[rt].ch[i]].maxx[j]);
}
}
int Abs(const int &x)
{
return x<0 ? (-x) : x;
}
int Dis(int a[],int b[])
{
return Abs(a[0]-b[0])+Abs(a[1]-b[1]);
}
bool operator < (const Node &a,const Node &b)
{
return a.p[dn]!=b.p[dn] ? a.p[dn]<b.p[dn] : a.p[dn^1]<b.p[dn^1];
}
int Buildtree(int l=1,int r=n,bool d=0)
{
dn=d;
int m=(l+r>>1);
nth_element(T+l,T+m,T+r+1);
T[m].Init();
if(l!=m) T[m].ch[0]=Buildtree(l,m-1,d^1);
if(m!=r) T[m].ch[1]=Buildtree(m+1,r,d^1);
Update(m);
return m;
}
void Query(int rt=root)
{
disn=min(disn,Dis(T[rt].p,qp));
int dd[2];
for(int i=0;i<2;i++)
if(T[rt].ch[i])
dd[i]=T[T[rt].ch[i]].Dis();
else dd[i]=INF;
bool f=(dd[0]<=dd[1]);
if(dd[!f]<disn) Query(T[rt].ch[!f]);
if(dd[f]<disn) Query(T[rt].ch[f]);
}
void Insert(int rt=root,bool d=0)
{
bool f=(T[n].p[d]>T[rt].p[d]);
if(T[rt].ch[f])
Insert(T[rt].ch[f],d^1);
else
T[rt].ch[f]=n;
Update(rt);
}
int q;
int main()
{
// freopen("bzoj2716.in","r",stdin);
// freopen("bzoj2716.out","w",stdout);
int op,x,y;
scanf("%d%d",&n,&q);
for(int i=1;i<=n;++i)
scanf("%d%d",&T[i].p[0],&T[i].p[1]);
root=(1+n>>1);
Buildtree();
for(int i=1;i<=q;++i)
{
scanf("%d",&op);
if(op==1)
{
++n;
scanf("%d%d",&T[n].p[0],&T[n].p[1]);
T[n].Init();
Insert();
}
else
{
scanf("%d%d",&qp[0],&qp[1]);
disn=INF;
Query();
printf("%d\n",disn);
}
}
return 0;
}

【kd-tree】bzoj2648 SJY摆棋子的更多相关文章

  1. BZOJ2648: SJY摆棋子&&2716: [Violet 3]天使玩偶

    BZOJ2648: SJY摆棋子 BZOJ2716: [Violet 3]天使玩偶 BZOJ氪金无极限... 其实这两道是同一题. 附上2648的题面: Description 这天,SJY显得无聊. ...

  2. [BZOJ2648] SJY摆棋子 kd-tree

    2648: SJY摆棋子 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 5421  Solved: 1910[Submit][Status][Disc ...

  3. luogu4169 [Violet]天使玩偶/SJY摆棋子 / bzoj2648 SJY摆棋子 k-d tree

    k-d tree + 重构的思想,就能卡过luogu和bzoj啦orz #include <algorithm> #include <iostream> #include &l ...

  4. Bzoj2648 SJY摆棋子

    Time Limit: 20 Sec  Memory Limit: 128 MB Submit: 3128  Solved: 1067 Description 这天,SJY显得无聊.在家自己玩.在一个 ...

  5. 2019.01.14 bzoj2648: SJY摆棋子(kd-tree)

    传送门 kd−treekd-treekd−tree模板题. 题意简述:支持在平面上插入一个点,求对于一个点的最近点对. 思路:cdqcdqcdq是一种很不错的分治方法 只是好像码量有点窒息 所以我用了 ...

  6. BZOJ2648 SJY摆棋子(KD-Tree)

    板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...

  7. [bzoj2648]SJY摆棋子(带插入kd-tree)

    解题关键:带插入kdtree模板题. #include<iostream> #include<cstdio> #include<cstring> #include& ...

  8. KDTree(Bzoj2648: SJY摆棋子)

    题面 传送门 KDTree 大概就是一个分割\(k\)维空间的数据结构,二叉树 建立:每层选取一维为关键字,把中间的点拿出来,递归左右,有个\(STL\)函数nth_element可以用一下 维护:维 ...

  9. 【BZOJ-2648&2716】SJY摆棋子&天使玩偶 KD Tree

    2648: SJY摆棋子 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2459  Solved: 834[Submit][Status][Discu ...

随机推荐

  1. NSIS打包(二)第一个安装包

    基本介绍完之后,我们大概了解了NSIS中有哪些组件,现在我们通过一个简单的demo来学习如何使用这些组件. 1.环境配置 NSIS的环境非常简单,只有一个安装包,安装后即可使用. 官网:http:// ...

  2. EBS R12重启后无法进入登录页面

    应用启动正常,但无法进入登录页面: The webpage cannot be found HTTP 404 ... No known changes had been made and the Mi ...

  3. 2016-06-08:Windows中的bat脚本

    涉及循环嵌套,启用变量延时,算术运算 @echo off setlocal enabledelayedexpansion %路径以及文件名等变量设置% set x264_exe=E:\demo\c++ ...

  4. UBUNTU 13.04 install Grive

    sudo apt-get install software-properties-common sudo apt-get install python-software-properties sudo ...

  5. 微信 网页授权获取用户基本信息(OAuth 2.0)

    // 相关设置 $APPID = ""; $AppSecret = ""; $html = ""; // 拼接 URL // 跳转该连接 获 ...

  6. Python自动化 【第二篇】:Python基础-列表、元组、字典

    本节内容 模块初识 .pyc简介 数据类型初识 数据运算 列表.元组操作 字符串操作 字典操作 集合操作 字符编码与转码 一.模块初识 Python的强大之处在于他有非常丰富和强大的标准库和第三方库, ...

  7. iOS 解析json串

    NSString *json = @"[{\"name\":\"a1\",\"items\":[{\"x1\" ...

  8. Redis的主从同步复制

    先来看一下Redis的主从同步复制的原理: 在Slave启动并连接到Master之后,它将主动发送一条SYNC命令.此后Master将启动后台存盘进程,同时收集所有接收到的用于修改数据集的命令,在后台 ...

  9. .net中自定义过滤器对Response内容进行处理

    原文:http://www.cnblogs.com/zgqys1980/archive/2008/09/02/1281895.html 代码DEMO:http://files.cnblogs.com/ ...

  10. ViewPager + Fragment实现滑动标签页

    http://blog.csdn.net/lizhenmingdirk/article/details/13631813; tab与frg的区别: http://www.cnblogs.com/tia ...