UVA 11297 Census(二维线段树)
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.
The 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.
The 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.
题目大意:一个n*m的矩阵上有些数,单点修改,区域查询。
思路:二维线段树裸题。抄个代码体验一下。
代码(502MS):
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; const int INF = 0x7fffffff;
const int MAXN = ;
struct IntervaTree2D {
int Max[MAXN][MAXN], Min[MAXN][MAXN], n, m;
int xo, xleaf, x1, y1, x2, y2, x, y, v, vmax, vmin; void query1D(int o, int L, int R) {
if(y1 <= L && R <= y2) {
vmax = max(vmax, Max[xo][o]); vmin = min(vmin, Min[xo][o]);
}
else {
int M = (L + R) >> ;
if(y1 <= M) query1D(o * , L, M);
if(M < y2) query1D(o * + , M + , R);
}
} void query2D(int o, int L, int R) {
if(x1 <= L && R <= x2) {xo = o; query1D(, , m);}
else {
int M = (L + R) >> ;
if(x1 <= M) query2D(o * , L, M);
if(M < x2) query2D(o * + , M + , R);
}
} void modify1D(int o, int L, int R) {
if(L == R) {
if(xleaf) {Max[xo][o] = Min[xo][o] = v; return ;}
Max[xo][o] = max(Max[xo * ][o], Max[xo * + ][o]);
Min[xo][o] = min(Min[xo * ][o], Min[xo * + ][o]);
}
else {
int M = (L + R) >> ;
if(y <= M) modify1D(o * , L, M);
else modify1D(o * + , M + , R);
Max[xo][o] = max(Max[xo][o * ], Max[xo][o * + ]);
Min[xo][o] = min(Min[xo][o * ], Min[xo][o * + ]);
}
} void modify2D(int o, int L, int R) {
if(L == R) {xo = o; xleaf = ; modify1D(, , m);}
else {
int M = (L + R) / ;
if(x <= M) modify2D(o * , L, M);
else modify2D(o * + , M + , R);
xo = o; xleaf = ; modify1D(, , m);
}
} void query() {vmax = -INF; vmin = INF; query2D(, , n);}
void modify() {modify2D(, , n);}
} t; int main() {
int n, m, Q;
char op[];
scanf("%d%d", &n, &m);
t.n = n; t.m = m;
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j) {
scanf("%d", &t.v);
t.x = i, t.y = j;
t.modify();
}
scanf("%d", &Q);
while(Q--) {
scanf("%s", op);
if(*op == 'q') {
scanf("%d%d%d%d", &t.x1, &t.y1, &t.x2, &t.y2);
t.query();
printf("%d %d\n", t.vmax, t.vmin);
} else {
scanf("%d%d%d", &t.x, &t.y, &t.v);
t.modify();
}
}
return ;
}
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 线段树套线段树(二维线段树)
题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要 不同的处理方式,非叶子形成的 ...
- 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 ...
随机推荐
- mobienet, shufflenet
参考github上各位大神的代码 mobilenet和shufflenet,实现起来感觉还是各种问题. mobilenet目前使用的代码来自这里:https://github.com/BVLC/caf ...
- 23.POI导出
POI导出 XSSFWorkbook 对应Excel2007版本及以上 HSSFWorkbook 对应Excel2003版本 还要注意一点,不要用Swagger-ui测试导出的表格,这样的表格文件都是 ...
- MySQL存储引擎与索引
引言: MySQL存储引擎主要分为 InnoDB 存储引擎与 MyISAM 存储引擎.都采用B+数的存储结构. 应用场景: InnoDB适合:(1)可靠性要求比较高,要求事务:(2)大量 insert ...
- 经验之谈—控制器的view的显示
经验之谈—控制器的view的显示 开发中,我们经常需要将一个控制器的view添加到另一个控制器的view上,这种效果是我们期望看到的,但是里边隐藏着一些细节,不注意的话,可能会达不到我们想到的效果. ...
- 02-第一个iOS程序-开发步骤
打开Xcode 选择项目模板 Single View Application是最适合初学者的模板 设置项目属性 运行程序 不管怎样,先运行第一个iOS程序看看效果先(用快捷键Command + R 也 ...
- spring入门学习感悟
1:ioc:控制反转 控制权的转移,应用程序本身不负责依赖对象的创建和维护,而是有外部容器负责创建和维护的(获取依赖对象的过程被反转了) 2:di:依赖注入,它是一种控制反转的一种实现方法,ioc容器 ...
- NEC 工程师规范
工程师规范 - 开发准备 了解产品和设计 参加需求.交互.视觉会议,了解产品设计和项目成员. 了解产品面向的设备和平台. 了解产品对兼容性的要求以及是否采用响应式设计等. 了解产品要使用的技术(WEB ...
- zepto 基础知识(4)
61.prev prev() 类型:collection prev(selector) 类型:collection 获取对相集合中每一个元素的钱一个兄弟节点,通过选择器来进行过滤 62.prev pr ...
- FBI树
题目描述 我们可以把由"0"和"1"组成的字符串分为三类:全"0"串称为B串,全"1"串称为I串,既含"0&q ...
- 分布式网上商城项目-dubbo搭建与初次使用错误
1.Spring-service启动失败 严重: Exception sending context initialized event to listener instance of class o ...