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 ...
随机推荐
- Timer控制开始、停止例子【转】
public partial class Form1 : Form { static public bool flag; public Form1() ...
- 2019年我的nodejs项目选型
选型项目比较激进.发现基于 go 语言的工具变多了.
- lambda匿名函数,sorted排序,filter()筛选,map()映射
一丶匿名函数 语法: 函数名 = lambda参数:返回值 # 普通的正常的函数 def func(n): return n * n ret = func(9) print(ret) # 匿名函数 a ...
- ArrayList与Vector区别
ArrayList与Vector区别表 ArrayList Vector 1.实现原理:采用动态对象数组实现,默认构造方法创建了一个空数组 1.实现原理:采用动态数组对象实现,默认构造方法创建了一个大 ...
- 【Java/Android性能优 7】Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类
本文转自:http://www.trinea.cn/android/android-common-lib/ 介绍总结的一些android公共库,包含缓存(图片缓存.预取缓存.网络缓存).公共View( ...
- mysqlbinlog 查看执行的sql (row模式)
记录一下:当bin-log的模式设置为 row时 不仅日志长得快 并且查看执行的sql时 也稍微麻烦一点:1.干扰语句多:2生成sql的编码需要解码. binlog_format=row 直接mysq ...
- Jsoup查找dom元素
package com.open1111.jsoup; import org.apache.http.HttpEntity;import org.apache.http.client.methods. ...
- StackOverflow之旅<2>------{HashMap和Hashtable的区别}
问题 在Java中HashMap和Hashtable的区别? 哪一个对于多线程应用程序更好? 回答 Hashtable是同步的,加了synchronized锁,而HashMap不是.没有加synchr ...
- POJ-1459 Power Network---最大流
题目链接: https://cn.vjudge.net/problem/POJ-1459 题目大意: 简单的说下题意(按输入输出来讲,前面的描述一堆的rubbish,还用来误导人),给你n个点,其中有 ...
- java设计模式——单例模式(二)
破坏单例模式 上一章节,介绍了单例模式的几种方式,这次来学习一波我们创建的单例模式是否安全,能不能破坏.换句话说,也就是在程序运行中,不止有一个实例. 一. 序列化,反序列化破坏 以饿汉式的单例模式 ...