Census

Time Limit: 8 sec

Description

This year, there have been many problems with population calculations, since in some cities, there are many emigrants, or the population growth is very high. Every year the ACM (for Association for Counting Members) conducts a census in each region. The country is divided into N^2 regions, consisting of an N x N grid of regions. Your task is to find the least, and the greatest population in some set of regions. Since in a single year there is no significant change in the populations, the ACM modifies the population counts by some number of inhabitants.

Input

In the first line you will find N (0 <= N <= 500), in following the N lines you will be given N numbers, which represent, the initial population of city C [i, j]. In the following line is the number Q (Q <= 40000), followed by Q lines with queries:

There are two possible queries:

  • “x1 y1 x2 y2” which represent the coordinates of the upper left and lower right of where you must calculate the maximum and minimum change in population.

  • “x y v” indicating a change of the population of city C [x, y] by value v.

Output

For each query, “x1 y1 x2 y2” print in a single line the greatest and least amount of current population. Separated each output by a space.

Notice: There is only a single test case.

Sample Input

5 5

1 2 3 4 5

0 9 2 1 3

0 2 3 4 1

0 1 2 4 5

8 5 3 1 4

4

q 1 1 2 3

c 2 3 10

q 1 1 5 5

q 1 2 2 2

Sample Output

9 0

10 0

9 2


解题心得:

  1. 题意很简单,就是给你一个矩阵,多次询问,每次询问一个子矩阵,输出子矩阵里面的最大值和最小值,也可以改变矩阵中某个点的值。
  2. 一看就是一个线段树,不过是一个矩阵,但是也很简单啊,把线段树的每一行拆出来建一个树,询问的时候就一个树一个树的找,时间给你8s,其实300ms就过了。

  1. /*其实代码差不多就是一个线段树的模板*/
  2. #include<stdio.h>
  3. #include<algorithm>
  4. #include<cstring>
  5. using namespace std;
  6. const int maxn = 510;
  7. const int INF = 0x3f3f3f3f;
  8. struct NODE
  9. {
  10. int Max,Min;
  11. }node[maxn][maxn<<2];//第一维表示是用矩阵的第几行建立的线段树
  12. int n,m,ans_Max,ans_Min;
  13. void updata(int c,int root)
  14. {
  15. node[c][root].Max = max(node[c][root<<1|1].Max,node[c][root<<1].Max);
  16. node[c][root].Min = min(node[c][root<<1|1].Min,node[c][root<<1].Min);
  17. }
  18. void build_tree(int c,int root,int l,int r)
  19. {
  20. if(l == r)
  21. {
  22. int temp;
  23. scanf("%d",&temp);
  24. node[c][root].Max = node[c][root].Min = temp;
  25. return ;
  26. }
  27. int mid = (l+r)>>1;
  28. build_tree(c,root<<1,l,mid);
  29. build_tree(c,root<<1|1,mid+1,r);
  30. updata(c,root);
  31. }
  32. void init()
  33. {
  34. for(int i=1;i<=n;i++)
  35. build_tree(i,1,1,n);
  36. }
  37. void get_ans(int c,int root,int l,int r,int L,int R)
  38. {
  39. if(l == L && r == R)
  40. {
  41. ans_Max = max(ans_Max,node[c][root].Max);
  42. ans_Min = min(ans_Min,node[c][root].Min);
  43. return ;
  44. }
  45. int mid = (L+R)>>1;
  46. if(mid >= r)
  47. get_ans(c,root<<1,l,r,L,mid);
  48. else if(mid < l)
  49. get_ans(c,root<<1|1,l,r,mid+1,R);
  50. else
  51. {
  52. get_ans(c,root<<1,l,mid,L,mid);
  53. get_ans(c,root<<1|1,mid+1,r,mid+1,R);
  54. }
  55. }
  56. void change(int c,int va,int root,int pos,int l,int r)
  57. {
  58. if(l == r && l == pos)
  59. {
  60. node[c][root].Max = va;
  61. node[c][root].Min = va;
  62. return ;
  63. }
  64. int mid = (l+r)>>1;
  65. if(mid >= pos)
  66. change(c,va,root<<1,pos,l,mid);
  67. else if(mid < pos)
  68. change(c,va,root<<1|1,pos,mid+1,r);
  69. updata(c,root);
  70. }
  71. void query()
  72. {
  73. int m;
  74. scanf("%d",&m);
  75. while(m--)
  76. {
  77. char s[10];
  78. scanf("%s",s);
  79. if(s[0] == 'q')
  80. {
  81. ans_Max = -INF;
  82. ans_Min = INF;
  83. int x1,y1,x2,y2;
  84. scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
  85. for(int i=x1;i<=x2;i++)
  86. get_ans(i,1,y1,y2,1,n);
  87. printf("%d %d\n",ans_Max,ans_Min);
  88. }
  89. if(s[0] == 'c')
  90. {
  91. int x,y,va;
  92. scanf("%d%d%d",&x,&y,&va);
  93. change(x,va,1,y,1,n);
  94. }
  95. }
  96. }
  97. int main()
  98. {
  99. while(scanf("%d",&n) != EOF)
  100. {
  101. init();
  102. query();
  103. }
  104. return 0;
  105. }

UVA:11297-Census(二维线段树)的更多相关文章

  1. UVa 11297 Census (二维线段树)

    题意:给定上一个二维矩阵,有两种操作 第一种是修改 c x y val 把(x, y) 改成 val 第二种是查询 q x1 y1 x2 y2 查询这个矩形内的最大值和最小值. 析:二维线段树裸板. ...

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

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

  3. UVA 11297 Census(二维线段树)

    Description This year, there have been many problems with population calculations, since in some cit ...

  4. UVA 11297 线段树套线段树(二维线段树)

    题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要  不同的处理方式,非叶子形成的 ...

  5. POJ2155 Matrix二维线段树经典题

    题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...

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

    之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...

  7. poj 2155:Matrix(二维线段树,矩阵取反,好题)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17880   Accepted: 6709 Descripti ...

  8. poj 1195:Mobile phones(二维线段树,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14391   Accepted: 6685 De ...

  9. POJ 2155 Matrix (二维线段树)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17226   Accepted: 6461 Descripti ...

  10. HDU 4819 Mosaic (二维线段树)

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

随机推荐

  1. spring batch 读取多个文件数据导入数据库

    项目的目录结构 需要读取文件的的数据格式 applicatonContext.xml的配置 <?xml version="1.0" encoding="UTF-8& ...

  2. Javascript Number

    Number 对象 Number对象是原始值的包装对象 创建Number对象的语法: var myNum = new Number(value): var myNum = Number(value): ...

  3. 运用CSS3媒体查询判断iPhoneX、iPhoneXR、iPhoneXS MAX及横竖屏

    //iphoneX.iphoneXs @media only screen and (device-width: 375px) and (device-height: 812px) and (-web ...

  4. URL最大长度问题

    在http协议中,其实并没有对url长度作出限制,往往url的最大长度和用户浏览器和Web服务器有关,不一样的浏览器,能接受的最大长度往往是不一样的,当然,不一样的Web服务器能够处理的最大长度的UR ...

  5. uLua学习之创建游戏对象(二)

    前言 上节,刚刚说到创建一个“HelloWorld”程序,大家想必都对uLua有所了解了,现在我们一步步地深入学习.在有关uLua的介绍中(在这里),我们可以发现它使用的框架是Lua + LuaJIT ...

  6. tcpick

    tcpick 是一款基于文本的嗅探器,能追踪,重组和重排tcp流.

  7. sqlserver锁表、解锁、查看销表

    锁定数据库的一个表 代码如下 复制代码 SELECT * FROM table WITH (HOLDLOCK) 注意: 锁定数据库的一个表的区别 代码如下 复制代码 SELECT * FROM tab ...

  8. git记录

    2017-3-30:git常用命令:1.$ git init:初始化git仓库2.$ git add *.c:跟踪文件3.$ git commit -m 'initial project versio ...

  9. C#环形缓冲区(队列)完全实现

    公司项目中经常设计到串口通信,TCP通信,而且大多都是实时的大数据的传输,然后大家都知道协议通讯肯定涉及到什么,封包.拆包.粘包.校验--什么鬼的概念一大堆,说简单点儿就是要一个高效率可复用的缓存区. ...

  10. [动态规划] uestc oj A - 男神的礼物

    A - 男神的礼物 Time Limit: 3000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Lweb学长 ...