#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. [CF442C] Artem and Array (贪心+单调栈优化)

    题目链接:http://codeforces.com/problemset/problem/442/C 题目大意:一个数列,有n个元素.你可以做n-2次操作,每次操作去除一个数字,并且得到这个数字两边 ...

  2. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  3. 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  4. 8. String to Integer (atoi)

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  5. UUID生成

    >>>Arch Linux # uuidgen From: http://os.51cto.com/art/200709/56613.htm >>>Debian j ...

  6. SQL笔记-第四章,数据的检索

    一.select的简单用法 1.简单的数据检索 SELECT * FROM T_Employee; 2.检索出需要的列 SELECT FNumber,FName,FAge FROM T_Employe ...

  7. 【Python】 sorted函数

    我们需要对List.Dict进行排序,Python提供了两个方法对给定的List L进行排序,方法1.用List的成员函数sort进行排序,在本地进行排序,不返回副本方法2.用built-in函数so ...

  8. regular expressions

  9. oracle rownum paging issues

    rownum是oracle预处理字段,默认标序是1,只有记录集已经满足条件后才会进行后续编号.由于第一条记录rownum默认是1,而你的条件是rownum>=6 对第一条记录比较它的rownum ...

  10. 使用VS,获取SQL SERVER 的链接字符串

    在VS中→工具→链接到数据库→(选择数据源,这里要链接的是SQL server)Microsoft SQL Server→服务器名字(不知道的可以在登陆SQL server的时候,把服务器名字复制过来 ...