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. mysql sql语句集锦

    1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份 ...

  2. C 碎片三 运算符与表达式

    一.算术运算符 算术运算符:+. -. *. /. %等 加:+ 减: - 乘: * 除: /     除数不能为0 模:%    参与模运算的数据不能为小数 二.赋值运算符 赋值运算符:= 作用: ...

  3. js之BOM和DOM

      今天我们来学习js中的一些基础的操作. 一.BOM对象 BOM(浏览器对象模型),可以对浏览器窗口进行访问和操作.使用 BOM,开发者可以移动窗口.改变状态栏中的文本以及执行其他与页面内容不直接相 ...

  4. 数据字典的设计--4.DOM对象的ajax应用

    需求:点击下拉选项框,选择一个数据类型,在表单中自动显示该类型下所有的数据项的名称,即数据库中同一keyword对应的所有不重复的ddlName.      1.在dictionaryIndex.js ...

  5. ARM实验1 —— 流水灯实验

    实验内容: 编写GPIO模块程序,实现对FS_4412平台的上的led2,led3,led4 ,led5,的流水灯实现. 实验目的: 熟悉开发环境的使用. 掌握Exynos 4412处理器GPIO功能 ...

  6. lnmp一键安装 nginx

    官网: https://lnmp.org/install.html 1.下载完整版:http://soft.vpser.net/lnmp/lnmp1.5-full.tar.gz文件大小:715MB M ...

  7. Aizu 2301 Sleeping Time(概率,剪枝)

    根据概率公式dfs即可,判断和区间[T-E,T+E]是否有交,控制层数. #include<bits/stdc++.h> using namespace std; int K,R,L; d ...

  8. CSS之常见文字样式整理

    常见文字样式 行高:line-height,当我i们将行高的大小设置成当前元素的高度时,可以实现当行文本在当前元素中垂直方向居中显示的效果 水平对齐方式:text-align:left|center| ...

  9. matlplotlib 为折线图填充渐变颜色

    概要   本篇记录绘图时填充颜色时的一些常用设置,主要用到了 imshow,fill 函数.   填充图实例   填充的效果图如下: 图 1:渐变色效果图 可根据下方给出的代码进行自定义. #!/us ...

  10. cuda中当元素个数超过线程个数时的处理案例

    项目打包下载 当向量元素超过线程个数时的情况 向量元素个数为(33 * 1024)/(128 * 128)=2.x倍 /* * Copyright 1993-2010 NVIDIA Corporati ...