【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维护就行了. --------------------------------- ...
随机推荐
- No.3__C#
起步的迷思 周二:今天起来,天上下着小雨,气温降低了许多.从上周的二十多度又回到了七八度的样子.多穿了一件衣服,顶着寒风就出门了,确实是有点冷.到了公司,已经八点四十几了.有些小疲倦,头晕晕沉沉的,不 ...
- MyBatis环境配置
<settings> <!-- 使全局的映射器启用或禁用缓存. --> <setting name="cacheEnabled" value=&quo ...
- 智能指针 ADO数据库连接
ADO库包含三个基本接口:_ConnectionPtr接口._CommandPtr接口和_RecordsetPtr接口._ConnectionPtr接口返回一个记录集或一个空指针.通常使用它来创建一个 ...
- projecteuler Problem 8 Largest product in a series
The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = ...
- 也说virtualbox下安装centos7
以前一直在VMware Workstation下安装虚拟机系统,这几天由于电脑被别人使用误升级为win10,而导致原来的LNMP不能使用,查找原因在于即使是最新的VM12.1.1也只是支持win8而已 ...
- Mybatis 插入null值报错
解决方法: 1.在settings中配置 <setting name="jdbcTypeForNull" value="OTHER"/> MyBat ...
- 4.openssl passwd
该伪命令用于生成加密的密码. [root@xuexi tmp]# whatis passwd ) - update user's authentication tokens ) - password ...
- Explode TArray
function Explode(const Separator, S: string; Limit: Integer = 0): TArray;var SepLen : Integer; F, P ...
- MySQL5.7.10免安装版配置
最新版的 Mysql 不提供图形界面的安装了, 下载下来是一个压缩包的形式, 那么我们如何来使用它呢, 让它为我们工作呢? 环境: mysql-5.7.10-winx64 + win7(64位) 一 ...
- ted be grateful
-------------------------------------------------------------- David Steindl-Rast: Want to be happy? ...