原题地址
Kdtree第一题.
讲一下Kdtree:
Kdtree的用途是对高维向量(点)进行各种索引.
建树操作:轮流选择维度(资料上说要按最大方差来选维度,然而各种题解都是轮流选择维度…),用nth_element()对点进行划分,然后建树、递归,回溯时维护边界(边界意义:一个二维点的边界信息可以形象地理解为一个包含该点所在子树中的所有点围成的一个最小矩形,高维类比).
查找最近/远点的操作:有了边界,我们可以很方便地求出查询点与某子树所管辖空间的最近/最远距离(具体看代码),然后进行相应的递归、回溯(具体看代码).
研究表明,一次该操作的时间复杂度为
AC code:
#include
#include
using namespace std;
const int K=2;
const int N=1000010;
const int INF=1<<29;
int n,mx,mn,ans=INF,tot,flg;
struct Poi{
int d[K];
friend bool operator<(Poi x,Poi y){
return x.d[flg]struct nod{
Poi poi;
int mxd[K],mnd[K];
nod *ch[2];
}pool[N];
int getdis(Poi x,Poi y){
int sum=0;
for(int i=0;iabs(x.d[i]-y.d[i]);
return sum;
}
struct Kdtree{
nod *root;
Kdtree(){
build(&root,1,n,0);
};
void update(nod *p){
for(int i=0;imxd[i]=p->mnd[i]=p->poi.d[i];
if(p->ch[0]!=NULL){
p->mxd[i]=max(p->mxd[i],p->ch[0]->mxd[i]);
p->mnd[i]=min(p->mnd[i],p->ch[0]->mnd[i]);
}
if(p->ch[1]!=NULL){
p->mxd[i]=max(p->mxd[i],p->ch[1]->mxd[i]);
p->mnd[i]=min(p->mnd[i],p->ch[1]->mnd[i]);
}
}
}
void build(nod **p,int L,int R,int flag){
if(L>R) return ;
*p=&pool[tot++];
if(L==R){
(*p)->poi=a[L];
for(int i=0;imxd[i]=(*p)->mnd[i]=a[L].d[i];
return ;
}
int M=(L+R)>>1;
flg=flag;
nth_element(a+L,a+M,a+R+1);
(*p)->poi=a[M];
build(&(*p)->ch[0],L,M-1,(flag+1)%K);
build(&(*p)->ch[1],M+1,R,(flag+1)%K);
update(*p);
}
int maxdis(nod *p,Poi poi){
int sum=0;
for(int i=0;iabs(p->mxd[i]-poi.d[i]),abs(p->mnd[i]-poi.d[i]));
return sum;
}
int mindis(nod *p,Poi poi){
int sum=0;
for(int i=0;iif(poi.d[i]mnd[i]) sum+=p->mnd[i]-poi.d[i];
else if(poi.d[i]>p->mxd[i]) sum+=poi.d[i]-p->mxd[i];
}
return sum;
}
void getmax(nod *p,Poi poi){
int dis0=getdis(p->poi,poi),dis[2];
if(dis0>mx) mx=dis0;
if(p->ch[0]!=NULL) dis[0]=maxdis(p->ch[0],poi);
if(p->ch[1]!=NULL) dis[1]=maxdis(p->ch[1],poi);
bool t=dis[0]1];
if(p->ch[t]!=NULL&&dis[t]>mx) getmax(p->ch[t],poi);
t^=1;
if(p->ch[t]!=NULL&&dis[t]>mx) getmax(p->ch[t],poi);
}
void getmin(nod *p,Poi poi){
int dis0=getdis(p->poi,poi),dis[2];
if(dis0&&dis0if(p->ch[0]!=NULL) dis[0]=mindis(p->ch[0],poi);
if(p->ch[1]!=NULL) dis[1]=mindis(p->ch[1],poi);
bool t=dis[0]>dis[1];
if(p->ch[t]!=NULL&&dis[t]ch[t],poi);
t^=1;
if(p->ch[t]!=NULL&&dis[t]ch[t],poi);
}
};
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d%d",&a[i].d[0],&a[i].d[1]);
Kdtree T;
for(int i=1;i<=n;i++){
mx=0;mn=INF;
T.getmax(T.root,a[i]);
T.getmin(T.root,a[i]);
if(mx-mnprintf("%d\n",ans);
return 0;
}