BZOJ2716 KD-Tree
好久没写博客了 回去赶了好久文化课 颓欲见长
突然翻到fc爷的KD-Tree板子 来切了到裸题
对于一开始的数据我们可以先预处理 具体的排序方式见板子 其实就是我们对每次选定的一块选一个维度来排序啦 这里算了下方差 选最大的那一维来分下去
#include<bits/stdc++.h>
#define bug(x) cout<<(#x)<<" "<<(x)<<endl
#define ll long long
/*
char *TT,*mo,but[(1<<15)+2];
#define getchar() ((TT==mo&&(mo=(TT=but)+fread(but,1,1<<15,stdin),TT==mo))?-1:*TT++)//*/
using namespace std;
const int K=2,N=1e6+5;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct P{
int x[K];
}a[N];
struct T{
int x[K],min[K],max[K],split;
T *ls,*rs;
}pool[N],*num=pool,*rt;
int cmp_k,ans,n,m,sta,p[K];
bool cmp(P a,P b){
return a.x[cmp_k]<b.x[cmp_k];
}
void update(T *o,T *x){
if(!x) return;
for(int i=0;i<K;i++) o->min[i]=min(o->min[i],x->min[i]),o->max[i]=max(o->max[i],x->max[i]);
}
void build(T *&o,int l,int r){
if(l>r) {
o=0;return;
}
o=num++;
int d;double mx=-1;
for(int i=0;i<K;i++){
double ave=0,sum=0;
for(int j=l;j<=r;j++) ave+=a[j].x[i];ave/=r-l+1;
for(int j=l;j<=r;j++) sum+=(a[j].x[i]-ave)*(a[j].x[i]-ave);
if(sum>mx) mx=sum,d=i;
}
o->split=cmp_k=d;
int mid=(l+r)>>1;
nth_element(a+l,a+mid+1,a+r+1,cmp);
for(int i=0;i<K;i++) o->x[i]=o->min[i]=o->max[i]=a[mid].x[i];
build(o->ls,l,mid-1),update(o,o->ls);
build(o->rs,mid+1,r),update(o,o->rs);
}
void insert(T *&o,int kd=0){
if(!o){
o=num++;
o->split=(kd+1)%K;
for(int i=0;i<K;i++) o->x[i]=o->min[i]=o->max[i]=p[i];
return;
}
for(int i=0;i<K;i++) o->min[i]=min(o->min[i],p[i]),o->max[i]=max(o->max[i],p[i]);
int d=o->split;
if(p[d]<o->x[d]) insert(o->ls,d);
else insert(o->rs,d);
}
void upd(int x){
ans=min(ans,x);
}
int dist(int *x,int *y){
int ret=0;
for(int i=0;i<K;i++) ret+=abs(x[i]-y[i]);
return ret;
}
int dist(int *x,int *y,int *z){
int ret=0;
for(int i=0;i<K;i++)
if(y[i]>x[i])ret+=y[i]-x[i];
else if(x[i]>z[i])ret+=x[i]-z[i];
return ret;
}
void ask(T *o){
if(!o||dist(p,o->min,o->max)>=ans) return;
upd(dist(o->x,p));
int d=o->split;
if(p[d]<o->x[d]) ask(o->ls),ask(o->rs);
else ask(o->rs),ask(o->ls);
}
int main(){
#ifdef Devil_Gary
freopen("in.txt","r",stdin);
#endif
n=read(),m=read();
for(int i=1;i<=n;i++) for(int j=0;j<K;j++) a[i].x[j]=read();
build(rt,1,n);
while(m--){
sta=read();
for(int i=0;i<K;i++) p[i]=read();
if(sta==2) ans=1<<30,ask(rt),printf("%d\n",ans);
else insert(rt);
}
return 0;
}
BZOJ2716 KD-Tree的更多相关文章
- k-d tree 学习笔记
以下是一些奇怪的链接有兴趣的可以看看: https://blog.sengxian.com/algorithms/k-dimensional-tree http://zgjkt.blog.uoj.ac ...
- K-D tree入门
久仰K-D tree大名已久,终于在合适的时候遇见了合适的水题入了坑入了门 K-D tree是什么 K-D tree是什么? 按名字上翻译来就是K维的树,就是一个用来维护K维空间的点的平衡二叉树 K- ...
- AOJ DSL_2_C Range Search (kD Tree)
Range Search (kD Tree) The range search problem consists of a set of attributed records S to determi ...
- 【BZOJ-2648&2716】SJY摆棋子&天使玩偶 KD Tree
2648: SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2459 Solved: 834[Submit][Status][Discu ...
- K-D Tree
这篇随笔是对Wikipedia上k-d tree词条的摘录, 我认为解释得相当生动详细, 是一篇不可多得的好文. Overview A \(k\)-d tree (short for \(k\)-di ...
- K-D Tree题目泛做(CXJ第二轮)
题目1: BZOJ 2716 题目大意:给出N个二维平面上的点,M个操作,分为插入一个新点和询问到一个点最近点的Manhatan距离是多少. 算法讨论: K-D Tree 裸题,有插入操作. #inc ...
- k-d Tree in TripAdvisor
Today, TripAdvisor held a tech talk in Columbia University. The topic is about k-d Tree implemented ...
- k-d tree算法
k-d树(k-dimensional树的简称),是一种分割k维数据空间的数据结构.主要应用于多维空间关键数据的搜索(如:范围搜索和最近邻搜索). 应用背景 SIFT算法中做特征点匹配的时候就会利用到k ...
- k-d tree模板练习
1. [BZOJ]1941: [Sdoi2010]Hide and Seek 题目大意:给出n个二维平面上的点,一个点的权值是它到其他点的最长距离减最短距离,距离为曼哈顿距离,求最小权值.(n< ...
- [模板] K-D Tree
K-D Tree K-D Tree可以看作二叉搜索树的高维推广, 它的第 \(k\) 层以所有点的第 \(k\) 维作为关键字对点做出划分. 为了保证划分均匀, 可以以第 \(k\) 维排名在中间的节 ...
随机推荐
- Cesium entity click
var url = 'http://202.107.245.51:81/user/dev/api/v2/sql?rows_per_page=40&page=0&sort_order=a ...
- 使用dork脚本来查询Google
使用dork脚本来查询Google 了解Google Hacking数据库的第一步是了解所有典型的Google运算,就像机器级编程工程师必须了解计算机操作代码一样. 这些Google运算是Google ...
- 关于bcb调用动态库,contains invalid OMF record, type 0x21 (possibly COFF)问题
今天用C++Builder6.0 调用三方lib文件时,编译的时候出现如下错误: “contains invalid OMF record, type 0x21 (possibly COFF)” 才知 ...
- http://code52.org/DownmarkerWPF/
http://code52.org/DownmarkerWPF/ http://kb.cnblogs.com/page/132209/
- ExtJs的Reader
ExtJs的Reader Reader : 主要用于将proxy数据代理读取的数据按照不同的规则进行解析,讲解析好的数据保存到Modle中 结构图 Ext.data.reader.Reader 读取器 ...
- Bootstrap FileInput 多图上传插件 文档属性说明
Bootstrap FileInput 多图上传插件 原文链接:http://blog.csdn.net/misterwho/article/details/72886248?utm_source ...
- docker使用Dockerfile构建ssh容器
一.使用Dockerfile构建centos 1.创建 Dockerfile mkdir centos # 创建一个目录存放之后的Dockerfile,目录名无所谓 cd centos # 进入目录 ...
- wpf 在Popup内的TextBox 输入法 不能切换
切换输入法 输入不了中文 [DllImport("User32.dll")] public static extern IntPtr SetFocus(IntPtr hWnd); ...
- Writing a Kernel in C++
*:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...
- CCF CSP 201312-4 有趣的数
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201312-4 有趣的数 问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0 ...