其实理论上cdq更优

核心是依次取x值、y值的mid作为当前节点,向两边递归建立二叉树,树上维护size:子树大小;mx[0/1]:子树内最大x/y;mn[0/1]:子树内最小x/y;d[0/1]:这个点的x/y;

建树的时候用到nth_element,用处是把第k个数放到k位置,并且把小于k的放在k前,大于k的放在k后(算是快排的一部分)

插入点的时候,像走二叉搜索树一样往下走(不过每走一步key值就要变,xy交替),然后走到空就把新点挂上去

查找的时候比较像退火,通过mnmx找到子树能更新答案的最小值预估,然后决定要不要往下走,并且一路走一路用真实值更新答案;如果左右儿子都能走,就先走估值小的那边

然后出现了一个问题,这个插入有可能把二叉树变成一条链,这个时候要用替罪羊树的思想,设一个阈值,如果左右儿子的size比例超过阈值就把这颗子树拍扁重构

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=500005;
const double alp=0.75;
int n,m,w,tot,rt,ans;
struct qwe
{
int a[2];
int& operator [] (int x)
{
return a[x];
}
bool operator < (const qwe &b) const
{
return a[w]<b.a[w];
}
}a[N];
struct KD
{
int s,ls,rs,mn[2],mx[2];
qwe d;
}t[N*5];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void minn(int &x,int y)
{
x>y?x=y:0;
}
void maxx(int &x,int y)
{
x<y?x=y:0;
}
void ud(int ro)
{
if(t[ro].ls)
for(int i=0;i<=1;i++)
minn(t[ro].mn[i],t[t[ro].ls].mn[i]),maxx(t[ro].mx[i],t[t[ro].ls].mx[i]);
if(t[ro].rs)
for(int i=0;i<=1;i++)
minn(t[ro].mn[i],t[t[ro].rs].mn[i]),maxx(t[ro].mx[i],t[t[ro].rs].mx[i]);
t[ro].s=t[t[ro].ls].s+t[t[ro].rs].s+1;
}
int build(int l,int r,int f)
{
if(l>r)
return 0;
w=f;
int mid=(l+r)>>1,ro=++tot;
nth_element(a+l,a+mid,a+r+1);
t[ro].s=1;
t[ro].d[0]=t[ro].mn[0]=t[ro].mx[0]=a[mid][0];
t[ro].d[1]=t[ro].mn[1]=t[ro].mx[1]=a[mid][1];
t[ro].ls=build(l,mid-1,f^1);
t[ro].rs=build(mid+1,r,f^1);
ud(ro);
return ro;
}
void pia(int &ro,int s)
{
if(t[ro].ls)
pia(t[ro].ls,s);
a[s+t[t[ro].ls].s+1]=t[ro].d;
if(t[ro].rs)
pia(t[ro].rs,s+t[t[ro].ls].s+1);
}
void ok(int &ro,int f)
{
if(alp*t[ro].s<max(t[t[ro].ls].s,t[t[ro].rs].s))
pia(ro,0),ro=build(1,t[ro].s,f);
}
void update(int &ro,int x,int y,int f)
{
if(!ro)
{
ro=++tot;
t[ro].s=1;
t[ro].mn[0]=t[ro].mx[0]=t[ro].d[0]=x;
t[ro].mn[1]=t[ro].mx[1]=t[ro].d[1]=y;
return;
}
if(t[ro].d[f]>=(f==0?x:y))
update(t[ro].ls,x,y,f^1);
else
update(t[ro].rs,x,y,f^1);
ud(ro);
ok(ro,f);
}
int dis(int ro,int x,int y)
{
return max(x-t[ro].mx[0],0)+max(t[ro].mn[0]-x,0)+max(y-t[ro].mx[1],0)+max(t[ro].mn[1]-y,0);
}
void ques(int ro,int x,int y)
{
ans=min(ans,abs(x-t[ro].d[0])+abs(y-t[ro].d[1]));
int dl=t[ro].ls?dis(t[ro].ls,x,y):1e9,dr=t[ro].rs?dis(t[ro].rs,x,y):1e9;
if(dl<dr)
{
if(dl<ans)
ques(t[ro].ls,x,y);
if(dr<ans)
ques(t[ro].rs,x,y);
}
else
{
if(dr<ans)
ques(t[ro].rs,x,y);
if(dl<ans)
ques(t[ro].ls,x,y);
}
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
a[i][0]=read(),a[i][1]=read();
rt=build(1,n,0);
while(m--)
{
int o=read(),x=read(),y=read();
if(o==1)
update(rt,x,y,0);
else
{
ans=1e9;
ques(rt,x,y);
printf("%d\n",ans);
}
}
return 0;
}

bzoj 2648: SJY摆棋子【KD-tree】的更多相关文章

  1. BZOJ 2648: SJY摆棋子(K-D Tree)

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 6051  Solved: 2113[Submit][Status][Discuss] Descript ...

  2. bzoj 2648 SJY摆棋子 kd树

    题目链接 初始的时候有一些棋子, 然后给两种操作, 一种是往上面放棋子. 另一种是给出一个棋子的位置, 问你离它最近的棋子的曼哈顿距离是多少. 写了指针版本的kd树, 感觉这个版本很好理解. #inc ...

  3. bzoj 2648 SJY摆棋子 —— K-D树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 学习资料:https://blog.csdn.net/zhl30041839/arti ...

  4. BZOJ 2648: SJY摆棋子

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

  5. BZOJ 2648: SJY摆棋子 kdtree

    2648: SJY摆棋子 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2648 Description 这天,SJY显得无聊.在家自己玩 ...

  6. bzoj 2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 --kdtree

    2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 Time Limit: 20 Sec  Memory Limit: 128 MB Description 这天,S ...

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

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

  8. BZOJ 2648 SJY摆棋子(KD Tree)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2648 题意: 思路: KDtree模板题. 参考自http://www.cnblogs.com/ra ...

  9. BZOJ 2648 SJY摆棋子(KD树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2716 [题目大意] 给出一些点,同时不断插入点和询问某点离插入点最近距离 [题解] 我 ...

  10. bzoj 2648 SJY摆棋子——KDtree

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 第一道KDtree! 学习资料:https://blog.csdn.net/zhl30 ...

随机推荐

  1. 天津政府应急系统之GIS一张图(arcgis api for flex)解说(二)鹰眼模块

    解说GIS功能模块实现之前,先大概说一下flexviewer的核心配置文件config.xml,系统额GIS功能widget菜单布局.系统的样式.地图资源等等都是在这里配置的,这里对flexviewe ...

  2. POJ 3928 Ping pong 树状数组模板题

    開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> ...

  3. 133. Clone Graph (3 solutions)——无向无环图复制

    Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...

  4. Spring官方文档翻译——15.1 介绍Spring Web MVC框架

    Part V. The Web 文档的这一部分介绍了Spring框架对展现层的支持(尤其是基于web的展现层) Spring拥有自己的web框架--Spring Web MVC.在前两章中会有介绍. ...

  5. zabbix基于SNMP 协议监控路由器

    zabbix基于SNMP 协议监控路由器 步骤 步骤超级方便. 1. 路由器上开启snmp 2. 确保外网能訪问到 3. 用snmpwalk測试 4. 加入zabbix主机,SNMP interfac ...

  6. Android版DesiredCapabilities参数配置

    前言 每一个App测试都应指定是在什么平台下,那个设备中运行那个App,而在Appium中主要是通过DesiredCapabilities来配置的. DesiredCapabilities的作用,负责 ...

  7. quilt

    1 什么是quilt quilt是一个patch管理工具,特别适合于对多个patch进行管理. quilt是基于gnu patch和diff的. 2 使用quilt创建一个patch 第一步,quil ...

  8. MapReduce ChainMapper/ChainReducer

    The ChainMapper class allows to use multiple Mapper classes within a single Map task.  The ChainRedu ...

  9. 如何用redis做到限制,一个手机号,1分钟内最多发一条,一天内最多10条

    需要两个缓存 key名称 phone-busy,缓存1分钟 key名称 phone-send-count,缓存1天,每成功发送一条+1 发送的时候流程如下: 判断phone-busy是否存在,存在直接 ...

  10. 初探linux子系统集之led子系统(一)【转】

    本文转载自:http://blog.csdn.net/eastmoon502136/article/details/37569789 就像学编程第一个范例helloworld一样,学嵌入式,单片机.f ...