【kd-tree】bzoj2716 [Violet 3]天使玩偶
#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]天使玩偶的更多相关文章
- bzoj2648SJY摆棋子&&bzoj2716[Violet 3]天使玩偶*
bzoj2648SJY摆棋子 bzoj2716[Violet 3]天使玩偶 题意: 棋盘上有n个棋子,现在有m个操作,一种是加棋子,一种是查询离某个点最近的棋子.n,m≤500000. 题解: 先将已 ...
- [BZOJ2716] [Violet 3]天使玩偶(CDQ分治)
[BZOJ2716] [Violet 3]天使玩偶(CDQ分治) 题面 Ayu 在七年前曾经收到过一个天使玩偶,当时她把它当作时间囊埋在了地下.而七年后 的今天,Ayu 却忘了她把天使玩偶埋在了哪里, ...
- bzoj2716: [Violet 3]天使玩偶
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- BZOJ2716 [Violet 3]天使玩偶 【CDQ分治】
题目 输入格式 输出格式 输入样例 //样例太长就不贴了.... 输出样例 //见原题 提示 题解 我们将曼哈顿距离式子中的绝对值去掉,每次只考虑x,y比当前点小的更新答案. 为了使所有点都对答案进行 ...
- 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 ...
- BZOJ2648: SJY摆棋子&&2716: [Violet 3]天使玩偶
BZOJ2648: SJY摆棋子 BZOJ2716: [Violet 3]天使玩偶 BZOJ氪金无极限... 其实这两道是同一题. 附上2648的题面: Description 这天,SJY显得无聊. ...
- SJY摆棋子&&[Violet 3]天使玩偶
SJY摆棋子 https://www.lydsy.com/JudgeOnline/problem.php?id=2648 [Violet 3]天使玩偶 https://www.lydsy.com/Ju ...
- BZOJ 2716: [Violet 3]天使玩偶
2716: [Violet 3]天使玩偶 Time Limit: 80 Sec Memory Limit: 128 MBSubmit: 1473 Solved: 621[Submit][Statu ...
- BZOJ 2716: [Violet 3]天使玩偶( CDQ分治 + 树状数组 )
先cdq分治, 然后要处理点对答案的贡献, 可以以询问点为中心分成4个区域, 然后去掉绝对值(4种情况讨论), 用BIT维护就行了. --------------------------------- ...
随机推荐
- PHP For Windows/php-5.6.11-Win32-VC11-x64启动脚本
启动php-cgi.bat @echo off E: cd E:\php-5.6.11-Win32-VC11-x64 php-cgi.exe -b 127.0.0.1:9000 -c php.ini- ...
- E:in-range伪类选择器与E:out-of-range伪类选择器
E:in-range伪类选择器用来指定当元素的有效值被限定在一段范围之内(通常通过min属性值与max属性值来限定),且实际输入值在该范围内时使用的样式.E:out-of-range伪类选择器用来指定 ...
- 用自己的ID在appstore中更新app-黑苹果之路
由于之前套用了别人的镜像,在appstore中更新XCode时总要输别人id的密码,id还不能改.网上有的说要把XCode删掉,然后再用自己的ID更新,找到另外一个方法,更简单: 1.打开引用程序目录 ...
- appium入门
前语:学习需要总结,或许有些知识自己存在偏差,但是能总结出来就会更加加深所学知识 1. 环境变量配置 必备软件安装: jdk1.6.0 android-sdk python:2.7(3.6 ...
- UnixBench测试
安装: 1. UnixBench from version 5.1 on has both system and graphics tests. If you want to use the g ...
- findstr 命令
body { font-family: Bitstream Vera Sans Mono; font-size: 11pt; line-height: 1.5; } html, body { colo ...
- redis 安装使用
在 centos 7.2 系统上,安装使用redis.了解学习redis功能及特性. 版本: 3.2.4 1.安装: # yum install redis 2.配置: /etc/logrotate. ...
- C#DataGridView中的常用技巧
0(最基本的技巧). 获取某列中的某行(某单元格)中的内容 this.currentposition = this.dataGridView1.BindingContext [this.dataG ...
- win 7 IIS 配置
http://jingyan.baidu.com/article/219f4bf723bcb2de442d38ed.html win7旗舰版系统 点击开始→控制面板,然后再点击程序,勿点击卸载程序,否 ...
- ted be grateful
-------------------------------------------------------------- David Steindl-Rast: Want to be happy? ...