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就过了。

/*其实代码差不多就是一个线段树的模板*/
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 510;
const int INF = 0x3f3f3f3f;
struct NODE
{
int Max,Min;
}node[maxn][maxn<<2];//第一维表示是用矩阵的第几行建立的线段树
int n,m,ans_Max,ans_Min; void updata(int c,int root)
{
node[c][root].Max = max(node[c][root<<1|1].Max,node[c][root<<1].Max);
node[c][root].Min = min(node[c][root<<1|1].Min,node[c][root<<1].Min);
} void build_tree(int c,int root,int l,int r)
{
if(l == r)
{
int temp;
scanf("%d",&temp);
node[c][root].Max = node[c][root].Min = temp;
return ;
}
int mid = (l+r)>>1;
build_tree(c,root<<1,l,mid);
build_tree(c,root<<1|1,mid+1,r);
updata(c,root);
} void init()
{
for(int i=1;i<=n;i++)
build_tree(i,1,1,n);
} void get_ans(int c,int root,int l,int r,int L,int R)
{
if(l == L && r == R)
{
ans_Max = max(ans_Max,node[c][root].Max);
ans_Min = min(ans_Min,node[c][root].Min);
return ;
}
int mid = (L+R)>>1;
if(mid >= r)
get_ans(c,root<<1,l,r,L,mid);
else if(mid < l)
get_ans(c,root<<1|1,l,r,mid+1,R);
else
{
get_ans(c,root<<1,l,mid,L,mid);
get_ans(c,root<<1|1,mid+1,r,mid+1,R);
}
} void change(int c,int va,int root,int pos,int l,int r)
{
if(l == r && l == pos)
{
node[c][root].Max = va;
node[c][root].Min = va;
return ;
}
int mid = (l+r)>>1;
if(mid >= pos)
change(c,va,root<<1,pos,l,mid);
else if(mid < pos)
change(c,va,root<<1|1,pos,mid+1,r);
updata(c,root);
} void query()
{
int m;
scanf("%d",&m);
while(m--)
{
char s[10];
scanf("%s",s);
if(s[0] == 'q')
{
ans_Max = -INF;
ans_Min = INF;
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
for(int i=x1;i<=x2;i++)
get_ans(i,1,y1,y2,1,n);
printf("%d %d\n",ans_Max,ans_Min);
}
if(s[0] == 'c')
{
int x,y,va;
scanf("%d%d%d",&x,&y,&va);
change(x,va,1,y,1,n);
}
}
} int main()
{
while(scanf("%d",&n) != EOF)
{
init();
query();
}
return 0;
}

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. linux下的idea的界面问题,错位以及各种...

    问题 ’ 方法 主题设置为GTK,多余的点会消失,而且字体也会好很多

  2. 提升Java代码质量(三)

    Item7:覆盖equals时需要遵守通用约定 在我们日常开发过程中,重写equals是比较常用的,但存在许多不合适的覆盖方式导致错误,最好的避免方法就是不去重写equals.但有时我们的业务又需要建 ...

  3. 关于IE和Firefox兼容性问题及解决办法

    1.//window.eventIE:有window.event对象FF:没有window.event对象.可以通过给函数的参数传递event对象.如onmousemove=doMouseMove(e ...

  4. Grafana 安装使用

    Grafana 安装使用 官方网址:https://grafana.com/ 官方文档:http://docs.grafana.org/ 安装 grafana 基于 RPM 的系统(CentOS,Fe ...

  5. Dll注入:Windows消息钩子注入

    SetWindowsHook() 是Windows消息处理机制的一个平台,应用程序可以在上面设置子程以监视指定窗口的某种消息,而且所监视的窗口可以是其他进程所创建的.当消息到达后,在目标窗口处理函数之 ...

  6. javascript面向对象继承和原型

    一.理解什么是对象:任何东西都可以是对象,对象就是一组无序属性的集合 对象具有属性和方法1.1 属性的类型属性内部又定义了两种属性:数据属性和访问器属性 (1)数据属性:有4个描述的行为 Config ...

  7. 两数相除赋值整数变量(T-SQL)

    MSSQL: DECLARE @_pagecount INT; ; SELECT @_pagecount; 结果为1 Mysql: BEGIN DECLARE _pagecount INT; ; SE ...

  8. UVA 1642 Magical GCD(gcd的性质,递推)

    分析:对于区间[i,j],枚举j. 固定j以后,剩下的要比较M_gcd(k,j) = gcd(ak,...,aj)*(j-k+1)的大小, i≤k≤j. 此时M_gcd(k,j)可以看成一个二元组(g ...

  9. json文件的读取

    在客户端读取后台的json文件,使用jquery的$.getJSON,读取后台文件内容. jQuery中的$.getJSON( )方法函数主要用来从服务器加载json编码的数据,它使用的是GET HT ...

  10. java常用 开源

    http://sourceforge.nethttp://code.google.com/hosting/http://www.open-open.com/code/tags/Javahttp://w ...