1. /*
  2. HDU 4819 Mosaic
  3. 题意:查询某个矩形内的最大最小值,
  4. 修改矩形内某点的值为该矩形(Mi+MA)/2;
  5. 二维线段树模板:
  6. 区间最值,单点更新。
  7. */
  8. #include<bits/stdc++.h>
  9. using namespace std;
  10. const int INF = 0x3f3f3f3f;
  11. const int MAXN = ;
  12. int N, Q;
  13. struct Nodey
  14. {
  15. int l, r;
  16. int Max, Min;
  17. };
  18. int locx[MAXN], locy[MAXN];
  19. struct Nodex
  20. {
  21. int l, r;
  22. Nodey sty[MAXN * ];
  23. void build(int i, int _l, int _r)
  24. {
  25. sty[i].l = _l;
  26. sty[i].r = _r;
  27. sty[i].Max = -INF;
  28. sty[i].Min = INF;
  29. if(_l == _r)
  30. {
  31. locy[_l] = i;
  32. return;
  33. }
  34. int mid = (_l + _r) / ;
  35. build(i << , _l, mid);
  36. build((i << ) | , mid + , _r);
  37. }
  38. int queryMin(int i, int _l, int _r)
  39. {
  40. if(sty[i].l == _l && sty[i].r == _r)
  41. return sty[i].Min;
  42. int mid = (sty[i].l + sty[i].r) / ;
  43. if(_r <= mid)
  44. return queryMin(i << , _l, _r);
  45. else if(_l > mid)
  46. return queryMin((i << ) | , _l, _r);
  47. else
  48. return min(queryMin(i << , _l, mid), queryMin((i << ) | , mid + , _r));
  49. }
  50. int queryMax(int i, int _l, int _r)
  51. {
  52. if(sty[i].l == _l && sty[i].r == _r)
  53. return sty[i].Max;
  54. int mid = (sty[i].l + sty[i].r) / ;
  55. if(_r <= mid)
  56. return queryMax(i << , _l, _r);
  57. else if(_l > mid)
  58. return queryMax((i << ) | , _l, _r);
  59. else
  60. return max(queryMax(i << , _l, mid), queryMax((i << ) | , mid + , _r));
  61. }
  62. } stx[MAXN * ];
  63. void build(int i, int l, int r)
  64. {
  65. stx[i].l = l;
  66. stx[i].r = r;
  67. stx[i].build(, , N);
  68. if(l == r)
  69. {
  70. locx[l] = i;
  71. return;
  72. }
  73. int mid = (l + r) / ;
  74. build(i << , l, mid);
  75. build((i << ) | , mid + , r);
  76. }
  77. //单点修改值
  78. void Modify(int x, int y, int val)
  79. {
  80. int tx = locx[x];
  81. int ty = locy[y];
  82. stx[tx].sty[ty].Min = stx[tx].sty[ty].Max = val;
  83. for(int i = tx; i; i >>= )
  84. for(int j = ty; j; j >>= )
  85. {
  86. if(i == tx && j == ty)continue;
  87. if(j == ty)
  88. {
  89. stx[i].sty[j].Min = min(stx[i << ].sty[j].Min, stx[(i << ) | ].sty[j].Min);
  90. stx[i].sty[j].Max = max(stx[i << ].sty[j].Max, stx[(i << ) | ].sty[j].Max);
  91. }
  92. else
  93. {
  94. stx[i].sty[j].Min = min(stx[i].sty[j << ].Min, stx[i].sty[(j << ) | ].Min);
  95. stx[i].sty[j].Max = max(stx[i].sty[j << ].Max, stx[i].sty[(j << ) | ].Max);
  96. }
  97. }
  98. }
  99.  
  100. int queryMin(int i, int x1, int x2, int y1, int y2)
  101. {
  102. if(stx[i].l == x1 && stx[i].r == x2)
  103. return stx[i].queryMin(, y1, y2);
  104. int mid = (stx[i].l + stx[i].r) / ;
  105. if(x2 <= mid)
  106. return queryMin(i << , x1, x2, y1, y2);
  107. else if(x1 > mid)
  108. return queryMin((i << ) | , x1, x2, y1, y2);
  109. else
  110. return min(queryMin(i << , x1, mid, y1, y2), queryMin((i << ) | , mid + , x2, y1, y2));
  111. }
  112. int queryMax(int i, int x1, int x2, int y1, int y2)
  113. {
  114. if(stx[i].l == x1 && stx[i].r == x2)
  115. return stx[i].queryMax(, y1, y2);
  116. int mid = (stx[i].l + stx[i].r) / ;
  117. if(x2 <= mid)
  118. return queryMax(i << , x1, x2, y1, y2);
  119. else if(x1 > mid)
  120. return queryMax((i << ) | , x1, x2, y1, y2);
  121. else
  122. return max(queryMax(i << , x1, mid, y1, y2), queryMax((i << ) | , mid + , x2, y1, y2));
  123. }
  124.  
  125. int main()
  126. {
  127. //freopen("in.txt","r",stdin);
  128. //freopen("out.txt","w",stdout);
  129. int T, ic = ;
  130. scanf("%d", &T);
  131. while(T--)
  132. {
  133. printf("Case #%d:\n", ++ic);
  134. scanf("%d", &N);
  135. build(, , N);
  136. for(int i = ; i <= N; i++)
  137. for(int j = ; j <= N; j++)
  138. {
  139. int a;
  140. scanf("%d", &a);
  141. Modify(i, j, a);
  142. }
  143. scanf("%d", &Q);
  144. while(Q--)
  145. {
  146. int x, y, L;
  147. scanf("%d%d%d", &x, &y, &L);
  148. int x1 = max(x - L / , );
  149. int x2 = min(x + L / , N);
  150. int y1 = max(y - L / , );
  151. int y2 = min(y + L / , N);
  152. //(x1,y1)左上角,(x2,y2)右下角
  153. int Max = queryMax(, x1, x2, y1, y2);
  154. int Min = queryMin(, x1, x2, y1, y2);
  155. int t = (Max + Min) / ;
  156. printf("%d\n", t);
  157. Modify(x, y, t);//单点修改
  158. }
  159. }
  160. return ;
  161. }

hdu 4819 二维线段树模板的更多相关文章

  1. HDU 4819 二维线段树

    13年长春现场赛的G题,赤裸裸的二维线段树,单点更新,区间查询 不过我是第一次写二维的,一开始写T了,原因是我没有好好利用行段,说白一点,还是相当于枚举行,然后对列进行线段树,那要你写二维线段树干嘛 ...

  2. Mosaic HDU 4819 二维线段树入门题

    Mosaic Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total S ...

  3. hdu1823(二维线段树模板题)

    hdu1823 题意 单点更新,求二维区间最值. 分析 二维线段树模板题. 二维线段树实际上就是树套树,即每个结点都要再建一颗线段树,维护对应的信息. 一般一维线段树是切割某一可变区间直到满足所要查询 ...

  4. HDU 4819 Mosaic (二维线段树&区间最值)题解

    思路: 二维线段树模板题,马克一下,以后当模板用 代码: #include<cstdio> #include<cmath> #include<cstring> #i ...

  5. HDU1832 二维线段树求最值(模板)

    Luck and Love Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. Luck and Love(二维线段树)

    Luck and Love Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. [hdu1823]Luck and Love(二维线段树)

    解题关键:二维线段树模板题(单点修改.查询max) #include<cstdio> #include<cstring> #include<algorithm> # ...

  8. UVA 11297 Census ——二维线段树

    [题目分析] 二维线段树模板题目. 简直就是无比的暴力.时间复杂度为两个log. 标记的更新方式比较奇特,空间复杂度为N^2. 模板题目. [代码] #include <cstdio> # ...

  9. HDU 4819 Mosaic(13年长春现场 二维线段树)

    HDU 4819 Mosaic 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4819 题意:给定一个n*n的矩阵,每次给定一个子矩阵区域(x,y,l) ...

随机推荐

  1. JS获取列表索引值

    html部分 <ul id="test"> <li>111</li> <li>222</li> <li>33 ...

  2. System中关于Property的方法

    System类在java.lang包中,所有方法都是静态的,里边有很多对系统的属性和控制方法 System类有三个成员变量:out-标准输出流(默认是控制台),in-标准输入流(默认是键盘),err- ...

  3. Master of Phi (欧拉函数 + 积性函数的性质 + 狄利克雷卷积)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6265 题目大意:首先T是测试组数,n代表当前这个数的因子的种类,然后接下来的p和q,代表当前这个数的因 ...

  4. MySQL删除数据几种情况以及是否释放磁盘空间【转】

    MySQL删除数据几种情况以及是否释放磁盘空间: 1.drop table table_name 立刻释放磁盘空间 ,不管是 Innodb和MyISAM ; 2.truncate table tabl ...

  5. 42.Trapping Rain Water---dp,stack,两指针

    题目链接:https://leetcode.com/problems/trapping-rain-water/description/ 题目大意:与84题做比较,在直方图中计算其蓄水能力.例子如下: ...

  6. java在图片上写字

  7. linux下C语言编程,include的默认搜索路径

    C语言编程时,发现细节的魅力很大.较为详细了看了一下关于include的知识,发现了几点新知: 1.include<头文件名>和include"头文件名" 如:incl ...

  8. ie6下png图片背景色处理

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  9. HDU 4614 Vases and Flowers(线段树+记录区间始末点或乱搞)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题目大意:有n个空花瓶,有两种操作: 操作①:给出两个数字A,B,表示从第A个花瓶开始插花,插B ...

  10. csu 1552(米勒拉宾素数测试+二分图匹配)

    1552: Friends Time Limit: 3 Sec  Memory Limit: 256 MBSubmit: 723  Solved: 198[Submit][Status][Web Bo ...