题目

平面上有 \(N\) 个点。需要实现以下三种操作:

  1. 在点集里添加一个点;

  2. 给出一个点,查询它到点集里所有点的曼哈顿距离的最小值;

  3. 给出一个点,查询它到点集里所有点的曼哈顿距离的最大值。


分析

用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]数据的更多相关文章

  1. AC日记——校门外的树 洛谷 P1047

    题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,……,L,都种 ...

  2. 线段树 洛谷P3932 浮游大陆的68号岛

    P3932 浮游大陆的68号岛 题目描述 妖精仓库里生活着黄金妖精们,她们过着快乐,却随时准备着迎接死亡的生活. 换用更高尚的说法,是随时准备着为这个无药可救的世界献身. 然而孩子们的生活却总是无忧无 ...

  3. [线段树]洛谷P5278 算术天才⑨与等差数列

    题目描述 算术天才⑨非常喜欢和等差数列玩耍. 有一天,他给了你一个长度为n的序列,其中第i个数为a[i]. 他想考考你,每次他会给出询问l,r,k,问区间[l,r]内的数从小到大排序后能否形成公差为k ...

  4. 带修主席树 洛谷2617 支持单点更新以及区间kth大查询

    题目链接:https://www.luogu.com.cn/problem/P2617 参考博客:https://blog.csdn.net/dreaming__ldx/article/details ...

  5. 区间连续长度的线段树——洛谷P2894 [USACO08FEB]酒店Hotel

    https://www.luogu.org/problem/P2894 #include<cstdio> #include<iostream> using namespace ...

  6. Solution -「APIO/CTSC 2007」「洛谷 P3620」数据备份

    \(\mathcal{Description}\)   Link.   给定升序序列 \(\{x_n\}\) 以及整数 \(k\),在 \(\{x_n\}\) 中选出恰 \(k\) 对 \((x_i, ...

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

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

  8. 洛谷P2633 Count on a tree(主席树上树)

    题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...

  9. [洛谷P1198/BZOJ1012][JSOI2008] 最大数 - 树状数组/线段树?

    其实已经学了树状数组和线段树,然而懒得做题,所以至今没写多少博客 Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数 ...

  10. 洛谷P2617 Dynamic Rankings (主席树)

    洛谷P2617 Dynamic Rankings 题目描述 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a ...

随机推荐

  1. Fpga开发笔记(二):高云FPGA发开发软件Gowin和高云fpga基本开发过程

    前言   本篇安装高云的开发软件Gowin,并且描述了一个基于高云fpga的程序的开发环境和完整的下载运行过程.   Gowin软件 概述   Gowin 软件是广东高云半导体股份有限公司的 FPGA ...

  2. Java JVM——3.运行时数据区概述及线程

    运行时数据区概述 在JVM 中的位置 内部划分 当我们通过前面的:类的加载 → 验证 → 准备 → 解析 → 初始化 这几个阶段完成后,执行引擎就会对我们的类进行使用,同时执行引擎将会使用到我们的运行 ...

  3. Ubuntu18.04搭建Vue开发环境

    更新软件源列表 sudo apt update nodejs安装 sudo apt install nodejs nodejs -v #查看版本 npm安装 sudo apt install npm ...

  4. Java 线程安全问题 使用同步机制讲单例模式中的懒汉式改写为线程安全的

    1 package bytezero.deadlock; 2 3 /** 4 * 使用同步机制讲单例模式中的懒汉式改写为线程安全的 5 * 6 * 7 * 8 * 9 * @author Byteze ...

  5. Java 如何自定义异常类

    1 package com.bytezero.throwable; 2 3 /** 4 * 5 * @Description 如何自定义异常类 6 * @author Bytezero·zhengle ...

  6. JavaScript 最新动态:2024 年新功能

    前言 随着 Web 技术的日新月异,JavaScript 也在不断地吸收新的特性和技术,以满足日益复杂和多样化的开发需求.在 2024 年,JavaScript 迎来了一系列令人瞩目的新功能,这些功能 ...

  7. 如何避免MYSQL主从延迟带来的读写问题?

    在MYSQL 部署架构选型上,许多公司都会用到主从读写分离的架构,如下是一个一主一从的架构,主库master负责写入,从库slave进行读取. 但是既然是读写分离,必然会面临这样一个问题,当在主库上进 ...

  8.  liunx上安装django ,启动uwsgi ,语音播报python实现过程

    由于需要做一个语音播报实现,用到的技术是python  ,需要事先搭环境,安装uwsgi  djagno环境,以下内容为百度上找到的好一点的内容,确实照着做成功了,转载一下,下次更好找资料 liunx ...

  9. 遥感图像镶嵌拼接:ENVI的Pixel Based Mosaicking工具操作方法

      本文介绍基于ENVI软件,利用"Pixel Based Mosaicking"工具实现栅格遥感影像镶嵌拼接的方法.   首先需要说明的是,本文需要镶嵌的遥感影像并不含地理参考信 ...

  10. [学习笔记]PostgreSQL数据库的安装和配置

    安装 安装源 yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat ...