#KD-Tree,替罪羊树#洛谷 6224 [BJWC2014]数据
题目
平面上有 \(N\) 个点。需要实现以下三种操作:
在点集里添加一个点;
给出一个点,查询它到点集里所有点的曼哈顿距离的最小值;
给出一个点,查询它到点集里所有点的曼哈顿距离的最大值。
分析
用KD-Tree实现,维护区间横纵坐标最小值和最大值,
由于需要在点集中添加点,可能会导致K-D Tree树高不平衡,
那么直接用替罪羊树拍扁重建即可
代码
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <queue>
#define rr register
using namespace std;
const int N=200011;
typedef long long lll;
const double alp=0.75;
int ran,root,n,m,ans;
inline signed iut(){
rr int ans=0,f=1; rr char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
return ans*f;
}
inline void print(int ans){
if (ans>9) print(ans/10);
putchar(ans%10+48);
}
inline signed min(int a,int b){return a<b?a:b;}
inline signed max(int a,int b){return a>b?a:b;}
struct rec{
int p[2];
bool operator <(const rec &t)const{
return p[ran]<t.p[ran];
}
};
inline signed Abs(int x){return x<0?-x:x;}
struct KD_Tree{
int mn[N][2],mx[N][2],son[N][2],siz[N],stac[N],TOP,tot; rec pt[N],p[N];
inline void pup(int now){
for (rr int i=0;i<2;++i){
mn[now][i]=mx[now][i]=p[now].p[i];
if (son[now][0]){
mn[now][i]=min(mn[now][i],mn[son[now][0]][i]);
mx[now][i]=max(mx[now][i],mx[son[now][0]][i]);
}
if (son[now][1]){
mn[now][i]=min(mn[now][i],mn[son[now][1]][i]);
mx[now][i]=max(mx[now][i],mx[son[now][1]][i]);
}
}
siz[now]=siz[son[now][0]]+siz[son[now][1]]+1;
}
inline bool balance(int now){return alp*siz[now]>=(max(siz[son[now][0]],siz[son[now][1]]));}
inline void recycle(int now){
if (son[now][0]) recycle(son[now][0]);
stac[++TOP]=now,pt[TOP]=p[now];
if (son[now][1]) recycle(son[now][1]);
}
inline signed build(int l,int r,int Ran){
if (l>r) return 0;
rr int mid=(l+r)>>1,now=stac[mid];
ran=Ran,nth_element(pt+l,pt+mid,pt+1+r),p[now]=pt[mid];
son[now][0]=build(l,mid-1,Ran^1);
son[now][1]=build(mid+1,r,Ran^1);
pup(now);
return now;
}
inline void rebuild(int &now,int Ran){
TOP=0,recycle(now);
now=build(1,TOP,Ran);
}
inline void Insert(int &now,rec W,int Ran){
if (!now) now=++tot,p[now]=W;
else{
if (W.p[Ran]<=p[now].p[Ran]) Insert(son[now][0],W,Ran^1);
else Insert(son[now][1],W,Ran^1);
}
pup(now);
if (!balance(now)) rebuild(now,Ran);
}
inline signed calcmn(int t,int x){
rr int ans=0;
for (rr int i=0;i<2;++i) ans+=max(mn[t][i]-p[x].p[i],0)+max(p[x].p[i]-mx[t][i],0);
return ans;
}
inline signed calcmx(int t,int x){
return max(Abs(p[x].p[0]-mn[t][0]),Abs(p[x].p[0]-mx[t][0]))+max(Abs(p[x].p[1]-mn[t][1]),Abs(p[x].p[1]-mx[t][1]));
}
inline void querymn(int now,int x){
rr int t=Abs(p[x].p[0]-p[now].p[0])+Abs(p[x].p[1]-p[now].p[1]);
if (ans>t) ans=t;
rr int c0=calcmn(son[now][0],x),c1=calcmn(son[now][1],x);
if (son[now][0]&&son[now][1]){
if (c0<c1&&c0<ans){
querymn(son[now][0],x);
if (c1<ans) querymn(son[now][1],x);
}else if (c1<ans){
querymn(son[now][1],x);
if (c0<ans) querymn(son[now][0],x);
}
}else if (son[now][0]){
if (c0<ans) querymn(son[now][0],x);
}else if (son[now][1]){
if (c1<ans) querymn(son[now][1],x);
}
}
inline void querymx(int now,int x){
rr int t=Abs(p[x].p[0]-p[now].p[0])+Abs(p[x].p[1]-p[now].p[1]);
if (ans<t) ans=t;
rr int c0=calcmx(son[now][0],x),c1=calcmx(son[now][1],x);
if (son[now][0]&&son[now][1]){
if (c0>c1&&c0>ans){
querymx(son[now][0],x);
if (c1>ans) querymx(son[now][1],x);
}else if (c1>ans){
querymx(son[now][1],x);
if (c0>ans) querymx(son[now][0],x);
}
}else if (son[now][0]){
if (c0>ans) querymx(son[now][0],x);
}else if (son[now][1]){
if (c1>ans) querymx(son[now][1],x);
}
}
}Tre;
signed main(){
n=iut();
for (rr int i=1;i<=n;++i) Tre.Insert(root,(rec){iut(),iut()},0);
for (rr int m=iut();m;--m){
rr int opt=iut(),x,y;
Tre.p[Tre.tot+1].p[0]=x=iut();
Tre.p[Tre.tot+1].p[1]=y=iut();
if (opt==0) Tre.Insert(root,(rec){x,y},0);
else if (opt==1) ans=0x3f3f3f3f,Tre.querymn(root,Tre.tot+1),print(ans),putchar(10);
else ans=-0x3f3f3f3f,Tre.querymx(root,Tre.tot+1),print(ans),putchar(10);
}
return 0;
}
#KD-Tree,替罪羊树#洛谷 6224 [BJWC2014]数据的更多相关文章
- AC日记——校门外的树 洛谷 P1047
题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,……,L,都种 ...
- 线段树 洛谷P3932 浮游大陆的68号岛
P3932 浮游大陆的68号岛 题目描述 妖精仓库里生活着黄金妖精们,她们过着快乐,却随时准备着迎接死亡的生活. 换用更高尚的说法,是随时准备着为这个无药可救的世界献身. 然而孩子们的生活却总是无忧无 ...
- [线段树]洛谷P5278 算术天才⑨与等差数列
题目描述 算术天才⑨非常喜欢和等差数列玩耍. 有一天,他给了你一个长度为n的序列,其中第i个数为a[i]. 他想考考你,每次他会给出询问l,r,k,问区间[l,r]内的数从小到大排序后能否形成公差为k ...
- 带修主席树 洛谷2617 支持单点更新以及区间kth大查询
题目链接:https://www.luogu.com.cn/problem/P2617 参考博客:https://blog.csdn.net/dreaming__ldx/article/details ...
- 区间连续长度的线段树——洛谷P2894 [USACO08FEB]酒店Hotel
https://www.luogu.org/problem/P2894 #include<cstdio> #include<iostream> using namespace ...
- Solution -「APIO/CTSC 2007」「洛谷 P3620」数据备份
\(\mathcal{Description}\) Link. 给定升序序列 \(\{x_n\}\) 以及整数 \(k\),在 \(\{x_n\}\) 中选出恰 \(k\) 对 \((x_i, ...
- BZOJ 2648: SJY摆棋子(K-D Tree)
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 6051 Solved: 2113[Submit][Status][Discuss] Descript ...
- 洛谷P2633 Count on a tree(主席树上树)
题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...
- [洛谷P1198/BZOJ1012][JSOI2008] 最大数 - 树状数组/线段树?
其实已经学了树状数组和线段树,然而懒得做题,所以至今没写多少博客 Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数 ...
- 洛谷P2617 Dynamic Rankings (主席树)
洛谷P2617 Dynamic Rankings 题目描述 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a ...
随机推荐
- 树莓派开发笔记(十六):树莓派4B+安装mariadb数据库(mysql开源分支)并测试基本操作
前言 树莓派使用数据库时,优先选择sqlite数据库,但是sqlite是文件数据库同时仅针对于单用户的情况,考虑到多用户的情况,在树莓派上部署安装mariadb数据库服务(mysql的开源分支), ...
- 【Azure 应用服务】使用Docker Compose创建App Service遇见"Linux Version is too long. It cannot be more than 4000 characters"错误
问题描述 使用Docker Compose方式合并多个镜像(Images)文件,然后部署到App Service中,结果失败.报错 Linux Version 太长,不能超过4000个字符. 错误消息 ...
- Task Manager 的设计简述
讲解 Task Manager 之前,在这里先介绍一些 Task Manager 会使用到的概念术语. 图数据库 Nebula Graph 中,存在一些长期在后台运行的任务,我们称之为 Job.存储层 ...
- 使用 maven 的 `wagon-maven-plugin`插件 快速部署 到不同的 环境
profile 在pom文件中配置 开发和测试环境的 profile信息, <profiles> <profile> <!-- 开发环境 --> <id> ...
- 在Linux下开启指定端口号
1.查看某个端口是否已开启,如果提示no表示未开启 #8888表示要查询的端口号firewall-cmd --query-port=8888/tcp 2.永久开启端口号,提示 success 表示成功 ...
- XAF Blazor TabbedMdi
开源项目地址:https://gitee.com/easyxaf/blazor-tabbed-mdi 前言 XAF在WinForm中采用了多文档界面(MDI),但在Blazor中却没有,在官网中也有人 ...
- Zabbix 7.0编译部署教程
Zabbix7.0 alpha版本.beta版本已经陆续发布,Zabbix7.0 LTS版本发布时间也越来越近.据了解,新的版本在性能提升.架构优化等新功能方面有非常亮眼的表现,不少小伙伴对此也已经跃 ...
- Java中枚举配合switch语句用法-2022新项目
一.业务场景 项目开发中经常会遇到多条件判断的情况,如果判断条件少的话使用if/elseif/else还比较好处理,如果判断条件多的话,则在使用这种语句就不太合适. 如果是自定义的一些内容,比如不同的 ...
- obsidian 日记本倒序汇总 获取标题显示 插件dataviewjs list
obsidian 日记本倒序汇总 获取标题显示 插件dataviewjs list // dataviewjs function removeDuplicate(arr) { return arr.f ...
- ida使用入门指北
静态分析 快捷键 操作 作用 空格键 在反汇编窗口中,进行列表视图与图形视图之间的切换 TAB 在反汇编窗口中,进行汇编指令与伪代码之间的切换 Esc 和 Ctrl+Enter 翻页,返回前一页面 G ...