码迷,mamicode.com
首页 > 其他好文 > 详细

kd-tree

时间:2019-10-28 21:06:39      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:tor   check   none   node   scan   closed   opened   hide   point   

技术图片
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10;
struct point{int x[2],val;}t[N],temp[N];
int mi[N][2],ma[N][2],sum[N],lson[N],rson[N],siz[N];
int st[N],top,ncnt,sort_dim,root;
bool operator<(point &a,point &b){return a.x[sort_dim]<b.x[sort_dim];};
int newnode(){return top?st[top--]:++ncnt;}
void up(int x)
{
    for(int i=0;i<=1;i++)
    {
        mi[x][i]=ma[x][i]=t[x].x[i];
        if(lson[x])mi[x][i]=min(mi[x][i],mi[lson[x]][i]),ma[x][i]=max(ma[x][i],ma[lson[x]][i]);
        if(rson[x])mi[x][i]=min(mi[x][i],mi[rson[x]][i]),ma[x][i]=max(ma[x][i],ma[rson[x]][i]);
    }
    sum[x]=sum[lson[x]]+sum[rson[x]]+t[x].val;
    siz[x]=siz[lson[x]]+siz[rson[x]]+1;
}
void build(int l,int r,int &pos,int dim)
{   
    if(l>r)return (void)(pos=0);
    int mid=l+r>>1;pos=newnode();
    sort_dim=dim;
    nth_element(temp+l,temp+mid,temp+r+1);
    t[pos]=temp[mid];
    build(l,mid-1,lson[pos],dim^1);
    build(mid+1,r,rson[pos],dim^1);
    up(pos);
}
void pia(int x,int cnt)
{
    if(lson[x])pia(lson[x],cnt);
    temp[siz[lson[x]]+cnt]=t[x];st[++top]=x;
    if(rson[x])pia(rson[x],cnt+siz[lson[x]]+1);
}
void check(int &x,int dim)
{
    if(siz[x]*0.75<siz[lson[x]]||siz[x]*0.75<siz[rson[x]])
    pia(x,1),build(1,siz[x],x,dim); 
}
void insert(int &x,point tmp,int dim)
{
    if(!x){x=newnode();lson[x]=rson[x]=0;t[x]=tmp;up(x);return ;}
    if(tmp.x[dim]<=t[x].x[dim])insert(lson[x],tmp,dim^1);
    else insert(rson[x],tmp,dim^1);
    up(x);check(x,dim);
}
inline int in(int x1,int y1,int x2,int y2,int X1,int Y1,int X2,int Y2)
{
    return x1>=X1&&x2<=X2&&y1>=Y1&&y2<=Y2;
}
inline int out(int x1,int y1,int x2,int y2,int X1,int Y1,int X2,int Y2)
{
    return X1>x2||X2<x1||Y1>y2||Y2<y1;
}
int query(int x,int x1,int y1,int x2,int y2)
{
    if(!x)return 0;
    int ans=0;
    if(in(mi[x][0],mi[x][1],ma[x][0],ma[x][1],x1,y1,x2,y2))return sum[x];
    if(out(mi[x][0],mi[x][1],ma[x][0],ma[x][1],x1,y1,x2,y2))return 0;
    if(in(t[x].x[0],t[x].x[1],t[x].x[0],t[x].x[1],x1,y1,x2,y2))ans+=t[x].val;
    ans+=query(lson[x],x1,y1,x2,y2)+query(rson[x],x1,y1,x2,y2);
    return ans;
}
int ans=0;
int main()
{
    int x1,x2,y1,y2,n,op;
    scanf("%d",&n);
    while(1)
    {
        scanf("%d",&op);
        if(op==3)break;
        else if(op==1)
        {
            int x,y,z;scanf("%d%d%d",&x,&y,&z);x^=ans;y^=ans;z^=ans;
            insert(root,(point){x,y,z},0);
        }
        else 
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);x1^=ans;x2^=ans;y1^=ans;y2^=ans;
            ans=query(root,x1,y1,x2,y2);
            printf("%d\n",ans);
        }
    }
    return 0;
}
kd-tree模板

 

kd-tree

标签:tor   check   none   node   scan   closed   opened   hide   point   

原文地址:https://www.cnblogs.com/bxd123/p/11755243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!