UVA:11297-Census(二维线段树)
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
解题心得:
- 题意很简单,就是给你一个矩阵,多次询问,每次询问一个子矩阵,输出子矩阵里面的最大值和最小值,也可以改变矩阵中某个点的值。
- 一看就是一个线段树,不过是一个矩阵,但是也很简单啊,把线段树的每一行拆出来建一个树,询问的时候就一个树一个树的找,时间给你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(二维线段树)的更多相关文章
- UVa 11297 Census (二维线段树)
题意:给定上一个二维矩阵,有两种操作 第一种是修改 c x y val 把(x, y) 改成 val 第二种是查询 q x1 y1 x2 y2 查询这个矩形内的最大值和最小值. 析:二维线段树裸板. ...
- UVA 11297 Census ——二维线段树
[题目分析] 二维线段树模板题目. 简直就是无比的暴力.时间复杂度为两个log. 标记的更新方式比较奇特,空间复杂度为N^2. 模板题目. [代码] #include <cstdio> # ...
- UVA 11297 Census(二维线段树)
Description This year, there have been many problems with population calculations, since in some cit ...
- UVA 11297 线段树套线段树(二维线段树)
题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要 不同的处理方式,非叶子形成的 ...
- POJ2155 Matrix二维线段树经典题
题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...
- HDU 1823 Luck and Love(二维线段树)
之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...
- poj 2155:Matrix(二维线段树,矩阵取反,好题)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17880 Accepted: 6709 Descripti ...
- poj 1195:Mobile phones(二维线段树,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14391 Accepted: 6685 De ...
- POJ 2155 Matrix (二维线段树)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17226 Accepted: 6461 Descripti ...
- HDU 4819 Mosaic (二维线段树)
Mosaic Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total S ...
随机推荐
- PowerShell 操作 OFFICE
UiPath操作Office软件的方式,这里说一下用PowerShell调用Office的COM组件的方式 老生常谈~每个程序员都要至少掌握一门脚本编程语言... EXCEL: $excel = Ne ...
- Struts2 源码分析-----Hello world
今天第一天学习struts2,没学过怎么办,那当然是helloworld.感觉嘛,学习的基本流程都差不多,就是helloworld,开发环境,然后就是逐个按照知识点打demo,打着打着你就会发现str ...
- hibernate课程 初探单表映射3-3 对象类型
本节简介: 1 简介对象类型(重点是音视频blob类型) 2 demo(对图片的写入数据库与读取) 1 简介对象类型 映射类型 java类型 标准sql类型 mysql类型 oracle类型 bina ...
- 微信小程序:从本地相册选择图片或使用相机拍照。
wx.chooseImage(OBJECT) 从本地相册选择图片或使用相机拍照. OBJECT参数说明: 示例代码: wx.chooseImage({ count: 1, // 默认9 sizeTyp ...
- Cocos2d-x v3.1 Hello world程序(四)
Cocos2d-x v3.1 Hello world程序(四) 在上一篇文章中我们我们已经使用Cocos-Console工具生成了工程,本机生成的目录为:"D:\CocosProject\T ...
- 用rem实现h5页面的编写
一 静态页面的布局 将这段代码加到script中 (function(doc, win) { var docEl = doc.documentElement, resizeEvt = 'orienta ...
- JavaScript:理解Promise方法
什么是promise? Promise的核心思想是代表异步操作的一个结果,并且promise具有三个状态(pending初始状态,fulfilled成功状态,rejected失败状态).我们可以理解为 ...
- Java后台工程师的3次面试
第一次面试 我面的是一个中小公司,在BOSS直聘上面找的,去之前看了看关于Java的一些基础知识,在牛客网上面看的,也做了一下牛客网的题目.然后跟HR约了一个时间就去面试了.因为第一次面试,一点经验都 ...
- Oracle表连接学习笔记
目录 一.表连接类型 1.1 内连接 1.2 外连接 二.表连接方法 2.1 表连接方法分类 2.2 表连接方法特性区别 @ 一.表连接类型 表连接类型可以分为:内连接.外连接,在看<收获,不止 ...
- komodo-edit
sudo add-apt-repository ppa:mystic-mirage/komodo-edit sudo apt-get update sudo apt-get install komod ...