【题目分析】

直接x,y二维轮番划分,暴力即可。

套上替罪羊,打碎重构,对于时间复杂度有了保证。

写起来好麻烦,重构的技巧很棒!

【代码】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define maxn 200005
#define inf 0x3f3f3f3f
#define lim 0.7
#define L t[o].c[0]
#define R t[o].c[1]
#define mid (l+r>>1)
#define F(i,j,k) for (int i=j;i<=k;++i)

struct node{
    int d[2],c[2];
    int mx[2],mn[2],sum,v,siz,D;
    int& operator [] (int x){return d[x];}
}t[maxn],now;
int p[maxn];
int opt=0,D,rt=0,ans=0,tot=0,cnt;
inline bool cmp(int x,int y){return t[x][D]<t[y][D];}
void pushup(int k)
{
    for (int i=0;i<2;++i)
    {
        t[k].mn[i]=min(t[t[k].c[0]].mn[i],min(t[t[k].c[1]].mn[i],t[k].mn[i]));
        t[k].mx[i]=max(t[t[k].c[0]].mx[i],max(t[t[k].c[1]].mx[i],t[k].mx[i]));
    }
    t[k].sum=t[t[k].c[0]].sum+t[t[k].c[1]].sum+t[k].v;
    t[k].siz=t[t[k].c[0]].siz+t[t[k].c[1]].siz+1;
}
inline int build(int l,int r,int dir){
    D=dir;
    nth_element(p+l,p+mid,p+r+1,cmp);
    int o=p[mid];
    t[o].D=dir;
    F(i,0,1) t[o].mn[i]=t[o].mx[i]=t[o][i];
    t[o].sum=t[o].v;
    L=l<mid ? build(l,mid-1,dir^1) : 0;
    R=mid<r ? build(mid+1,r,dir^1) : 0;
    pushup(o);
    return o;
}
inline void dfs(int o){
    if (!o) return;
    dfs(L);
    p[++cnt]=o;
    dfs(R);
}
inline void rebuild(int &o){
    cnt=0;
    dfs(o);
    o=build(1,cnt,t[o].D);
}

void ins(int &o,int dir)
{
    if (!o)
    {
        o=++tot;
        t[o]=now;
        for (int i=0;i<2;++i) t[o].mn[i]=t[o].mx[i]=t[o].d[i];
        t[o].siz=1;
        t[o].D=dir;
        t[o].sum=t[o].v;
        return ;
    }
    if (now.d[dir]<t[o].d[dir])
    {
        ins(t[o].c[0],dir^1);
        pushup(o);
        if ((double)t[t[o].c[0]].siz>(double)t[o].siz*lim) rebuild(o);
    }
    else
    {
        ins(t[o].c[1],dir^1);
        pushup(o);
        if ((double)t[t[o].c[1]].siz>(double)t[o].siz*lim) rebuild(o);
    }
}

void print(int o){
    if (!o) return;
    printf("%d t[o].mn[0]=%d t[o].mn[1]=%d t[o].mx[0]=%d t[o].mx[1]=%d\n",o,t[o].mn[0],t[o].mn[1],t[o].mx[0],t[o].mx[1]);
    print(L);
    print(R);
}

int query(int o,int x1,int y1,int x2,int y2)
{
    if (!o) return 0;
    if (t[o].mn[0]>=x1 && t[o].mn[1]>=y1 && t[o].mx[0]<=x2 && t[o].mx[1]<=y2)
        return t[o].sum;
    else
    {
        int ret=0;
        if (t[o].d[0]>=x1&&t[o].d[0]<=x2&&t[o].d[1]>=y1&&t[o].d[1]<=y2)
            ret+=t[o].v;
        if (t[t[o].c[0]].mn[0]>x2||t[t[o].c[0]].mx[0]<x1||t[t[o].c[0]].mn[1]>y2||t[t[o].c[0]].mx[1]<y1);
            else ret+=query(t[o].c[0],x1,y1,x2,y2);
        if (t[t[o].c[1]].mn[0]>x2||t[t[o].c[1]].mx[0]<x1||t[t[o].c[1]].mn[1]>y2||t[t[o].c[1]].mx[1]<y1);
            else ret+=query(t[o].c[1],x1,y1,x2,y2);
        return ret;
    }
}

int main()
{
    for (int i=0;i<2;++i) t[rt].mx[i]=-inf,t[rt].mn[i]=inf;
    t[rt].siz=t[rt].sum=t[rt].v=0;
    scanf("%*d");
    while (scanf("%d",&opt)!=EOF&&opt!=3)
    {
        if (opt==1)
        {
            scanf("%d%d%d",&now.d[0],&now.d[1],&now.v);
            now.d[0]^=ans; now.d[1]^=ans; now.v^=ans;
            ins(rt,1);
        }
        else{
            int x1,y1,x2,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            x1^=ans;y1^=ans;x2^=ans;y2^=ans;
            printf("%d\n",ans=query(rt,x1,y1,x2,y2));
        }
    }
}

  

BZOJ 4066 简单题 ——KD-Tree套替罪羊树的更多相关文章

  1. bzoj 4066: 简单题 K-D树

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=4066 题解 我们把每次的修改操作都当作二维平面上多了一个权值点 对于每组询问可以看做求一 ...

  2. bzoj 4066: 简单题 kd-tree

    4066: 简单题 Time Limit: 50 Sec  Memory Limit: 20 MBSubmit: 234  Solved: 82[Submit][Status][Discuss] De ...

  3. bzoj 4066 简单题——KDtree(带重构)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4066 带部分重构的KDtree.就是那个替罪羊树思想的. 写了对拍,调了半天,发现忘了 re ...

  4. BZOJ4066:简单题(K-D Tree)

    Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作:   命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 ...

  5. P4148 简单题 k-d tree

    思路:\(k-d\ tree\) 提交:2次 错因:整棵树重构时的严重错误:没有维护父子关系(之前写的是假重构所以没有维护父子关系) 题解: 遇到一个新的点就插进去,如果之前出现过就把权值加上. 代码 ...

  6. BZOJ 4066 简单题(KD树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4066 [题目大意] 要求维护矩阵内格子加点和矩阵查询 [题解] 往KD树上加权值点,支 ...

  7. bzoj 4066 & bzoj 2683 简单题 —— K-D树(含重构)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4066 https://www.lydsy.com/JudgeOnline/problem.p ...

  8. bzoj 4066: 简单题

    #include<cstdio> #include<iostream> #include<cstdlib> #include<algorithm> #d ...

  9. 【CJOJ2433】陌上花开 树状数组套替罪羊树

    [CJOJ2433]陌上花开 树状数组套替罪羊树 蛤?bzoj?没权限QAQ 蛤?CDQ?看了好久没看懂QwQ 好吧我会拿cdq再写一遍的 为啥我感觉这东西比cdq好写 只好拿树状数组套替罪羊树水水了 ...

随机推荐

  1. Android 只开启一个Activity实例

    在一个Activity中,多次调用startActivity()来启动另一个Activity,要想只生成一个Activity实例,方法有两种. 方法一:设置起动模式 一个Activity有四种启动模式 ...

  2. Selector

    原文: https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Sele ...

  3. windows安装rabbitmq

    官网下载windows安装版本:http://www.rabbitmq.com/install-windows.html ,安装文件rabbitmq-server-3.6.5.exe 前提:安装erl ...

  4. Spring配置文件集成Hibernate配置文件

      Spring对hibernate配置文件hibernate.cfg.xml的集成,来取代hibernate.cfg.xml的配置.  spring对hibernate配置文件hibernate.c ...

  5. C# DBHelper 第二版

    1. [代码][C#]代码     跳至 [1] [全屏预览] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

  6. SEO之title优化

    作者:andyrat,联系方式:andyrat@qq.com

  7. 我的C语言进化史

    Hello, world! 这三年就看看我的C语言还有JAVAscript进化史吧.更厉害的sunmarvell,我等你

  8. mysql使用load导入csv文件所遇到的问题及解决方法

    使用navicat的客户端插入csv的数据文件,有一种非常简单的方式,即使用导入向导,直接根据数据匹配即可. 使用load的方式. 由于本项目中插入数据表量大而且格式统一,故首先使用创建字段creat ...

  9. python Unicode 编码解码

    1 #将Unicode转换成普通的Python字符串:"编码(encode)" 2 unicodestring = u"Hello world" 3 utf8s ...

  10. Tomcat使用详解

    Tomcat简介 官网:http://tomcat.apache.org/ Tomcat GitHub 地址:https://github.com/apache/tomcat Tomcat是Apach ...