bzoj 2648: SJY摆棋子【KD-tree】
其实理论上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】的更多相关文章
- BZOJ 2648: SJY摆棋子(K-D Tree)
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 6051 Solved: 2113[Submit][Status][Discuss] Descript ...
- bzoj 2648 SJY摆棋子 kd树
题目链接 初始的时候有一些棋子, 然后给两种操作, 一种是往上面放棋子. 另一种是给出一个棋子的位置, 问你离它最近的棋子的曼哈顿距离是多少. 写了指针版本的kd树, 感觉这个版本很好理解. #inc ...
- bzoj 2648 SJY摆棋子 —— K-D树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 学习资料:https://blog.csdn.net/zhl30041839/arti ...
- BZOJ 2648: SJY摆棋子
2648: SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2968 Solved: 1011[Submit][Status][Disc ...
- BZOJ 2648: SJY摆棋子 kdtree
2648: SJY摆棋子 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2648 Description 这天,SJY显得无聊.在家自己玩 ...
- bzoj 2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 --kdtree
2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 Time Limit: 20 Sec Memory Limit: 128 MB Description 这天,S ...
- luogu4169 [Violet]天使玩偶/SJY摆棋子 / bzoj2648 SJY摆棋子 k-d tree
k-d tree + 重构的思想,就能卡过luogu和bzoj啦orz #include <algorithm> #include <iostream> #include &l ...
- BZOJ 2648 SJY摆棋子(KD Tree)
http://www.lydsy.com/JudgeOnline/problem.php?id=2648 题意: 思路: KDtree模板题. 参考自http://www.cnblogs.com/ra ...
- BZOJ 2648 SJY摆棋子(KD树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2716 [题目大意] 给出一些点,同时不断插入点和询问某点离插入点最近距离 [题解] 我 ...
- bzoj 2648 SJY摆棋子——KDtree
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 第一道KDtree! 学习资料:https://blog.csdn.net/zhl30 ...
随机推荐
- 一场由股票提醒助手插件引发的血案——浅入浅出 jquery autocomplete
我没有学过前端,所以这篇文章注定要班门弄斧了. 通常,须要用到什么技术什么语言时,我才去学,学了也不一定掌握,就是记不住!所以如今明确了.学习的时候,亦或是攻克难点的时候,一定要记录下来.并不一定非要 ...
- 组件接口(API)设计指南[2]-类接口(class interface)
*返回文件夹阅读其它章节: http://blog.csdn.net/cuibo1123/article/details/39894477 类接口(class interface) 你能够參考MGTi ...
- LeetCode_3Sum Closest
一.题目 3Sum Closest Total Accepted: 32191 Total Submissions: 119262My Submissions Given an array S of ...
- C# does not contain a constructor that takes no parameter
C# 中子类要重用父类的构造函数时, 一般会在子类构造函数后面调用 : base(paratype, para). 如果父类有一个參数个数为1的构造函数, 没有 0 參构造函数. 子类想要重用这个构造 ...
- object-c iOS 教程 git for mac
本文转载至 http://blog.csdn.net/u011728347/article/details/10035191 http://rypress.com/tutorials/object ...
- 使用TASM编译COFF格式和连接
看到网络上流传的一份Drocon的mercury的代码程序源码使用TASM32编译使用MASM32来连接...关键的地方就在这里为什么要使用TASM编译...正常情况下TASM连接出来的程序代码体积远 ...
- ALVtree 显示BOM结构
REPORT z_barry_alv_tree1_bom MESSAGE-ID oo. TABLES: stpox.INCLUDE <icon>. CLASS: cl_gui_col ...
- 搭建双系统后没有windows的引导程序
因为安装linux系统前没有安装引导程序,导致安装了linux系统后进入linux系统没有windows的引导程序,网上找了很多解决办法,也不能说是不好使,只是作为新手小白来说有点难以理解,最后无意中 ...
- 对 block 内存管理的一些看法
首先交代一下retain cycle ,和 产生retain cycle后我们应该怎么处理. 1.retain cycle在block中是极易产生,block就是一段可以灵活使用的代码,你可以把它当做 ...
- springboot 项目 docker化部署
最近公司项目需要docker化,项目所使用的框架是springboot,linux环境.第一次接触docker化方面的技术.做的时候,所接触的新知识比较多,留下此文,以便以后用到的时候快速入手. 修改 ...