zoukankan      html  css  js  c++  java
  • kd-tree

    #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
    ",ans);
            }
        }
        return 0;
    }
    kd-tree模板
  • 相关阅读:
    模态框+Tab切换显示Json/Xml格式,提交Json/Xml字符串到后台
    jeDate时间插件
    ECharts柱状图+BootstrapTable联动
    ES6新增的一些常用特性
    Array数组遍历的几种方法以及Object对象的遍历
    Arguments 对象
    数组去重几种方法
    原型链图解
    切换镜像小工具
    AppID
  • 原文地址:https://www.cnblogs.com/bxd123/p/11755243.html
Copyright © 2011-2022 走看看