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陈老师!

  1. /**************************************************************
  2. Problem: 4066
  3. User: Tunix
  4. Language: C++
  5. Result: Accepted
  6. Time:28400 ms
  7. Memory:11444 kb
  8. ****************************************************************/
  9.  
  10. //BZOJ 4066
  11. #include<cstdio>
  12. #include<cstring>
  13. #include<cstdlib>
  14. #include<iostream>
  15. #include<algorithm>
  16. #define rep(i,n) for(int i=0;i<n;++i)
  17. #define F(i,j,n) for(int i=j;i<=n;++i)
  18. #define D(i,j,n) for(int i=j;i>=n;--i)
  19. #define pb push_back
  20. using namespace std;
  21. typedef long long LL;
  22. inline int getint(){
  23. int r=,v=; char ch=getchar();
  24. for(;!isdigit(ch);ch=getchar()) if (ch=='-') r=-;
  25. for(; isdigit(ch);ch=getchar()) v=v*-''+ch;
  26. return r*v;
  27. }
  28. const int N=,INF=1e9;
  29. /*******************template********************/
  30. int n,m,ans,D,cnt,p[N],tot,root;
  31. struct node{
  32. int d[],mn[],mx[],l,r,D,size,sum,v;
  33. int& operator [] (int x){return d[x];}
  34. }t[N],now;
  35. inline bool cmp(int x,int y){return t[x][D]<t[y][D];}
  36.  
  37. #define L t[o].l
  38. #define R t[o].r
  39. #define mid (l+r>>1)
  40. inline void Push_up(int o){
  41. F(i,,){
  42. t[o].mn[i]=min(t[o].mn[i],min(t[L].mn[i],t[R].mn[i]));
  43. t[o].mx[i]=max(t[o].mx[i],max(t[L].mx[i],t[R].mx[i]));
  44. }
  45. t[o].sum=t[L].sum+t[R].sum+t[o].v;
  46. t[o].size=t[L].size+t[R].size+;
  47. }
  48. inline int build(int l,int r,int dir){
  49. D=dir;
  50. nth_element(p+l,p+mid,p+r+,cmp);
  51. int o=p[mid];
  52. t[o].D=dir;
  53. F(i,,) t[o].mn[i]=t[o].mx[i]=t[o][i];
  54. t[o].sum=t[o].v;
  55. L=l<mid ? build(l,mid-,dir^) : ;
  56. R=mid<r ? build(mid+,r,dir^) : ;
  57. Push_up(o);
  58. return o;
  59. }
  60. inline void dfs(int o){
  61. if (!o) return;
  62. dfs(L);
  63. p[++cnt]=o;
  64. dfs(R);
  65. }
  66. inline void rebuild(int &o){
  67. cnt=;
  68. dfs(o);
  69. o=build(,cnt,t[o].D);
  70. }
  71. inline void Insert(int &o,int dir){
  72. if (!o){
  73. o=++tot; t[o]=now;
  74. F(i,,) t[o].mn[i]=t[o].mx[i]=t[o][i];
  75. t[o].D=dir;
  76. t[o].size=;
  77. t[o].sum=t[o].v;
  78. return;
  79. }
  80. if (now[dir]<t[o][dir]){
  81. Insert(L,dir^);
  82. Push_up(o);
  83. if ((double)t[L].size>(double)t[o].size*0.7) rebuild(o);
  84. }
  85. else{
  86. Insert(R,dir^);
  87. Push_up(o);
  88. if ((double)t[R].size>(double)t[o].size*0.7) rebuild(o);
  89. }
  90. }
  91. int query(int o,int x1,int y1,int x2,int y2){
  92. if (!o) return ;
  93. if (t[o].mn[]>=x1 && t[o].mn[]>=y1 && t[o].mx[]<=x2 && t[o].mx[]<=y2)
  94. return t[o].sum;
  95. else{
  96. int ans=;
  97. if (t[o][]>=x1 && t[o][]<=x2 && t[o][]>=y1 && t[o][]<=y2) ans+=t[o].v;
  98. if (t[L].mn[]>x2 || t[L].mx[]<x1 || t[L].mn[]>y2 || t[L].mx[]<y1) ;
  99. else ans+=query(L,x1,y1,x2,y2);
  100. if (t[R].mn[]>x2 || t[R].mx[]<x1 || t[R].mn[]>y2 || t[R].mx[]<y1) ;
  101. else ans+=query(R,x1,y1,x2,y2);
  102. return ans;
  103. }
  104. }
  105. void print(int o){
  106. if (!o) return;
  107. 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[]);
  108. print(L);
  109. print(R);
  110. }
  111. int main(){
  112. #ifndef ONLINE_JUDGE
  113. freopen("4066.in","r",stdin);
  114. freopen("4066.out","w",stdout);
  115. #endif
  116. F(i,,) t[].mn[i]=INF,t[].mx[i]=-INF;
  117. t[].size=t[].sum=t[].v=;
  118.  
  119. n=getint(); int cmd;
  120. while(scanf("%d",&cmd)!=EOF && cmd<){
  121. if (cmd==){
  122. now[]=getint()^ans,now[]=getint()^ans,now.v=getint()^ans;
  123. Insert(root,);
  124. }else{
  125. int x1=getint()^ans,y1=getint()^ans,x2=getint()^ans,y2=getint()^ans;
  126. printf("%d\n",ans=query(root,x1,y1,x2,y2));
  127. }
  128. }
  129. return ;
  130. }

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. Django实战(19):自定义many-to-many关系,实现Atom订阅

    记得有人跟我说过,rails的has_many :through是一个”亮点“,在Django看来,该功能简直不值一提.rails中的many-to-many关联中,还需要你手工创建关联表(写 mig ...

  2. tp5总结(四)

    数据库 1.数据库配置 1-1.配置文件配置[http://ww:7070/tp5-3/public/] 1-2.Db::connect配置[数组和字符串方式][http://ww:7070/tp5- ...

  3. 交换机高级特性MUX VLAN

    MUX VLAN 基本概念 lMUX VLAN(Multiplex VLAN)提供了一种通过VLAN进行网络资源控制的机制. 例如,在企业网络中,企业员工和企业客户可以访问企业的服务器. 对于企业来说 ...

  4. 安装部署VMware vSphere 5.5文档 (6-4) 安装配置DB数据库

    部署VMware vSphere 5.5 实施文档 ########################################################################## ...

  5. Ubuntu 16.04 64位 tftp服务器搭建

    TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂.开销不大的文件传输服务 ...

  6. python opencv3 grabcut前景检测

    git:https://github.com/linyi0604/Computer-Vision import numpy as np import cv2 import matplotlib.pyp ...

  7. event使用说明和DHTML参数属性

    event 对象 代表事件状态,如事件发生的元素,键盘状态,鼠标位置和鼠标按钮状态. DHTML元素属性列表 属性 描述 abstract 使用 event 对象获取高级流重定向器(ASX)文件中项目 ...

  8. 快速排序之C++实现

    快速排序之C++实现 一趟快速排序的算法是: 1)设置两个变量i.j,排序开始的时候:i=0,j=N-1: 2)以第一个数组元素作为关键数据,赋值给key,即key=A[0]: 3)从j开始向前搜索, ...

  9. CF1027F Session in BSU

    link 花絮: 这场看起来打得还不错的样子……(别问我是用哪个号打的). 然后听说这题的思想被出了好多次,女生赛也出过,quailty算法,然而当时没反应过来,而且时间不多啦. 题意: 有n个人,每 ...

  10. PHP 操作MySQL时mysql_connect( )和Mysqli( )的两种报错机制

    刚开始使用PHP连接MySQL数据库的时候,如果数据库连接不成功或者,对MySQL数据库进行增删改查等操作的时候,SQL语句存在错误,而在执行PHP文件的时候,浏览器并不会抛出错误的原因,一般是空白显 ...