#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】bzoj2716 [Violet 3]天使玩偶的更多相关文章

  1. bzoj2648SJY摆棋子&&bzoj2716[Violet 3]天使玩偶*

    bzoj2648SJY摆棋子 bzoj2716[Violet 3]天使玩偶 题意: 棋盘上有n个棋子,现在有m个操作,一种是加棋子,一种是查询离某个点最近的棋子.n,m≤500000. 题解: 先将已 ...

  2. [BZOJ2716] [Violet 3]天使玩偶(CDQ分治)

    [BZOJ2716] [Violet 3]天使玩偶(CDQ分治) 题面 Ayu 在七年前曾经收到过一个天使玩偶,当时她把它当作时间囊埋在了地下.而七年后 的今天,Ayu 却忘了她把天使玩偶埋在了哪里, ...

  3. bzoj2716: [Violet 3]天使玩偶

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  4. BZOJ2716 [Violet 3]天使玩偶 【CDQ分治】

    题目 输入格式 输出格式 输入样例 //样例太长就不贴了.... 输出样例 //见原题 提示 题解 我们将曼哈顿距离式子中的绝对值去掉,每次只考虑x,y比当前点小的更新答案. 为了使所有点都对答案进行 ...

  5. BZOJ2716: [Violet 3]天使玩偶(KD-Tree)

    Description Input Output Sample Input 100 100 81 23 27 16 52 58 44 24 25 95 34 2 96 25 8 14 97 50 97 ...

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

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

  7. SJY摆棋子&&[Violet 3]天使玩偶

    SJY摆棋子 https://www.lydsy.com/JudgeOnline/problem.php?id=2648 [Violet 3]天使玩偶 https://www.lydsy.com/Ju ...

  8. BZOJ 2716: [Violet 3]天使玩偶

    2716: [Violet 3]天使玩偶 Time Limit: 80 Sec  Memory Limit: 128 MBSubmit: 1473  Solved: 621[Submit][Statu ...

  9. BZOJ 2716: [Violet 3]天使玩偶( CDQ分治 + 树状数组 )

    先cdq分治, 然后要处理点对答案的贡献, 可以以询问点为中心分成4个区域, 然后去掉绝对值(4种情况讨论), 用BIT维护就行了. --------------------------------- ...

随机推荐

  1. 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  2. 自动化测试selenium+java 第二章

    1.设置等待时间 Thread.sleep(2000); (1000代表1s) 2.断言assertion: 验证应用程序的状态是否同所期望的一致. 常见的断言包括:验证页面内容,如标题是否为X或当前 ...

  3. Android新浪微博获取评论信息、发表评论、转发微博等

    首先前面一节中说过,获取用户的微博信息,这里简单介绍下获取微博的评论信息,以及对微博进行评论,转发微博等. OAuth认证,这里就不多说了, 我说名一下接口: 获取微博的评论列表接口: http:// ...

  4. JS,Jquery获取各种屏幕的宽度和高度

    Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.b ...

  5. libssh2 的集成与应用

    http://blog.csdn.net/wyc6668205/article/details/9179197 Xmanager Enterprise 4 putty 等工具都功能都是利用libssh ...

  6. 44. 普通对象建一个用户方法,提交时报:失败:建立业务逻辑对象失败:业务逻辑定义更新到数据库失败:ORA-00904: "DEFVERSION": 标识符无效

    LBBIZPROCESSDEFSLBHISTORYBIZPROCESSDEFSLBHISTORYMULTIWFDEFSDESIGNLBHISTORYWORKFLOWDEFSDESIGNLBMULTIW ...

  7. Linux课程实践一:Linux基础实践(基础操作)

    一.软件源维护 1. 基本操作 (1)查看源列表 sudo vim /etc/apt/sources.list deb:二进制软件安装包 deb-src:源码包 (2)备份软件源列表 sudo cp ...

  8. __new__

    [__new__] object.__new__(cls[, ...]) Called to create a new instance of class cls. 用于创建类对象cls的实例. __ ...

  9. js实现倒计时及时间对象

    JS实现倒计时效果代码如下: <!doctype html> <html> <head> <meta charset="utf-8"> ...

  10. 2015年可用的TRACKER服务器大全

    udp://tracker.openbittorrent.com:80/announceudp://tracker.publicbt.com:80/announcehttp://pubt.net:27 ...