KD-Tree


  KD-Tree的进阶姿势戳这里 http://zyfzyf.is-programmer.com/posts/92431.html

  为啥有种线段树&平衡树的即视感……(树形结构的相似性?)

  每次插入之后,判断下如果某个子树的size>父亲size*0.7,那么重构一下……(替罪羊树的即视感)

  查询的时候,如果当前点表示的坐标范围被查询范围完全包含,则直接返回sum;

  否则:当前点若在范围内则更新答案,左子树若不全在范围外则递归进入查询,右子树同理(线段树的即视感)

  

TLE:rebuild的时候,我没清当前点的min和max就直接build子树&Push_up(o)了……活该跪……

UPD:2015-05-23 17:20:26

  今天看论文的时候,发现这一做法在陈老师的论文中已经讲出来了……orz,无限ym陈老师!

 /**************************************************************
Problem: 4066
User: Tunix
Language: C++
Result: Accepted
Time:28400 ms
Memory:11444 kb
****************************************************************/ //BZOJ 4066
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
#define pb push_back
using namespace std;
typedef long long LL;
inline int getint(){
int r=,v=; char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if (ch=='-') r=-;
for(; isdigit(ch);ch=getchar()) v=v*-''+ch;
return r*v;
}
const int N=,INF=1e9;
/*******************template********************/
int n,m,ans,D,cnt,p[N],tot,root;
struct node{
int d[],mn[],mx[],l,r,D,size,sum,v;
int& operator [] (int x){return d[x];}
}t[N],now;
inline bool cmp(int x,int y){return t[x][D]<t[y][D];} #define L t[o].l
#define R t[o].r
#define mid (l+r>>1)
inline void Push_up(int o){
F(i,,){
t[o].mn[i]=min(t[o].mn[i],min(t[L].mn[i],t[R].mn[i]));
t[o].mx[i]=max(t[o].mx[i],max(t[L].mx[i],t[R].mx[i]));
}
t[o].sum=t[L].sum+t[R].sum+t[o].v;
t[o].size=t[L].size+t[R].size+;
}
inline int build(int l,int r,int dir){
D=dir;
nth_element(p+l,p+mid,p+r+,cmp);
int o=p[mid];
t[o].D=dir;
F(i,,) t[o].mn[i]=t[o].mx[i]=t[o][i];
t[o].sum=t[o].v;
L=l<mid ? build(l,mid-,dir^) : ;
R=mid<r ? build(mid+,r,dir^) : ;
Push_up(o);
return o;
}
inline void dfs(int o){
if (!o) return;
dfs(L);
p[++cnt]=o;
dfs(R);
}
inline void rebuild(int &o){
cnt=;
dfs(o);
o=build(,cnt,t[o].D);
}
inline void Insert(int &o,int dir){
if (!o){
o=++tot; t[o]=now;
F(i,,) t[o].mn[i]=t[o].mx[i]=t[o][i];
t[o].D=dir;
t[o].size=;
t[o].sum=t[o].v;
return;
}
if (now[dir]<t[o][dir]){
Insert(L,dir^);
Push_up(o);
if ((double)t[L].size>(double)t[o].size*0.7) rebuild(o);
}
else{
Insert(R,dir^);
Push_up(o);
if ((double)t[R].size>(double)t[o].size*0.7) rebuild(o);
}
}
int query(int o,int x1,int y1,int x2,int y2){
if (!o) return ;
if (t[o].mn[]>=x1 && t[o].mn[]>=y1 && t[o].mx[]<=x2 && t[o].mx[]<=y2)
return t[o].sum;
else{
int ans=;
if (t[o][]>=x1 && t[o][]<=x2 && t[o][]>=y1 && t[o][]<=y2) ans+=t[o].v;
if (t[L].mn[]>x2 || t[L].mx[]<x1 || t[L].mn[]>y2 || t[L].mx[]<y1) ;
else ans+=query(L,x1,y1,x2,y2);
if (t[R].mn[]>x2 || t[R].mx[]<x1 || t[R].mn[]>y2 || t[R].mx[]<y1) ;
else ans+=query(R,x1,y1,x2,y2);
return ans;
}
}
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[],t[o].mn[],t[o].mx[],t[o].mx[]);
print(L);
print(R);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("4066.in","r",stdin);
freopen("4066.out","w",stdout);
#endif
F(i,,) t[].mn[i]=INF,t[].mx[i]=-INF;
t[].size=t[].sum=t[].v=; n=getint(); int cmd;
while(scanf("%d",&cmd)!=EOF && cmd<){
if (cmd==){
now[]=getint()^ans,now[]=getint()^ans,now.v=getint()^ans;
Insert(root,);
}else{
int x1=getint()^ans,y1=getint()^ans,x2=getint()^ans,y2=getint()^ans;
printf("%d\n",ans=query(root,x1,y1,x2,y2));
}
}
return ;
}

4066: 简单题

Time Limit: 50 Sec  Memory Limit: 20 MB
Submit: 297  Solved: 99
[Submit][Status][Discuss]

Description

你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作:

命令

参数限制

内容

1 x y A

1<=x,y<=N,A是正整数

将格子x,y里的数字加上A

2 x1 y1 x2 y2

1<=x1<= x2<=N

1<=y1<= y2<=N

输出x1 y1 x2 y2这个矩形内的数字和

3

终止程序

Input

输入文件第一行一个正整数N。
接下来每行一个操作。每条命令除第一个数字之外,
均要异或上一次输出的答案last_ans,初始时last_ans=0。

Output

对于每个2操作,输出一个对应的答案。

Sample Input

4
1 2 3 3
2 1 1 3 3
1 1 1 1
2 1 1 0 7
3

Sample Output

3
5

HINT

数据规模和约定
1<=N<=500000,操作数不超过200000个,内存限制20M,保证答案在int范围内并且解码之后数据仍合法。
样例解释见OJ2683

Source

[Submit][Status][Discuss]

【BZOJ】【4066】简单题(强制在线)的更多相关文章

  1. bzoj 4066: 简单题 kd-tree

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

  2. BZOJ 4066 简单题(KD树)

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

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

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

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

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

  5. BZOJ 4066 简单题 ——KD-Tree套替罪羊树

    [题目分析] 直接x,y二维轮番划分,暴力即可. 套上替罪羊,打碎重构,对于时间复杂度有了保证. 写起来好麻烦,重构的技巧很棒! [代码] #include <cstdio> #inclu ...

  6. bzoj 4066: 简单题

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

  7. BZOJ 2683: 简单题

    2683: 简单题 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 913  Solved: 379[Submit][Status][Discuss] ...

  8. BZOJ 3687: 简单题 bitset

    3687: 简单题 Time Limit: 10 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 小呆开始研究集合论了,他 ...

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

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

  10. BZOJ 2683: 简单题(CDQ分治 + 树状数组)

    BZOJ2683: 简单题(CDQ分治 + 树状数组) 题意: 你有一个\(N*N\)的棋盘,每个格子内有一个整数,初始时的时候全部为\(0\),现在需要维护两种操作: 命令 参数限制 内容 \(1\ ...

随机推荐

  1. LoadRunner中的IP欺骗的设置以及误区

    LoadRunner中的IP欺骗的设置以及误区 最近在忙着部署web性能测试的环境后,对IP欺骗进行设置,特地做个笔记,给自己的学习历程留下点足迹. 一. 什么是IP欺骗? 做什么事首先要问个为什么, ...

  2. OneDrive 开机启动设置失效如何处理?

    问题现象: 『设置里勾选了开机启动onedrive,但是重启电脑勾选就没了,重新勾选没用,一重启就没了』 win10的onedrive无法开机启动 - Microsoft Community 处理方式 ...

  3. 大数据技术之_13_Azkaban学习_Azkaban(阿兹卡班)介绍 + Azkaban 安装部署 + Azkaban 实战

    一 概述1.1 为什么需要工作流调度系统1.2 常见工作流调度系统1.3 各种调度工具特性对比1.4 Azkaban 与 Oozie 对比二 Azkaban(阿兹卡班) 介绍三 Azkaban 安装部 ...

  4. 网络图片嗅探工具driftnet

    网络图片嗅探工具driftnet   图片是网络数据传输的重要内容.Kali Linux内置了一款专用工具drifnet.该工具可以支持实时嗅探和离线嗅探.它可以从数据流中提取JPEG和GIF这两种网 ...

  5. luoguP4279 [SHOI2008]小约翰的游戏 Anti-SG 博弈论

    这就是一个Anti SG问题 当整个游戏的\(sg = 0\)时,如果不存在单一游戏局面\(sg > 1\),那么先手必胜 当整个游戏的\(sg \neq 0\)时,如果至少存在一个单一游戏局面 ...

  6. [POI2015]Trzy wieże

    [POI2015]Trzy wieże 题目大意: 给定一个长度为\(n(n\le10^6)\)的仅包含'B'.'C'.'S'三种字符的字符串,请找到最长的一段连续子串,使得在这一段内出现过的所有字符 ...

  7. BZOJ3473 字符串 广义后缀自动机

    今天主攻了下SAM 好多东西以前都没理解到 对于这道题 我们建一个自动机存所有串 每个穿last从1开始 对于自动机上每个点额外记一个cnt 表示能匹配到这个点的不同串个数 建完对每个串在自动机上匹配 ...

  8. VC6配置sqlite数据库

    SQLite官方下载只提供给我们一个sqlite3.dll跟一个sqlite3.def文件,并没有提供用于VC++6.0的lib文件,可以利用sqlite3.def文件生成,步骤如下: 1.下载DLL ...

  9. Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索

    Codeforces Round #297 (Div. 2)E. Anya and Cubes Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx  ...

  10. C#设计模式泛型注入

    TSFac注入方式: 泛型接口工厂: public class SFac<TInterface, TClass> where TInterface : class where TClass ...