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 ...
随机推荐
- ImportError: No module named lmdb
why? 具体原因没有查清楚.安装caffe时,按照要求安装了包,caffe用的好好的,而且我也用打好包的lmdb跑了程序了.可我今天想看一下我的打包数据是不是漏掉数据了,直接开个python窗口,i ...
- JavaScript函数的方法
在一个对象中绑定函数,称为这个对象的方法. 在JavaScript中,对象的定义是: var xiaoming = { name:'小明'; birth:1990; }; 但是,如果我们给xiaomi ...
- JavaScript js调用堆栈(二)
本文主要介绍JavaScript的内存空间 var a = 20; var b = 'abc'; var c = true; var d = { m: 20 } 首先需要对栈(stack),堆(hea ...
- Oracle 行转列两种方法
1.新建一个名为TEST表 create table TEST( STUDENT varchar2(20), COURSE varchar2(20), SCORE number); INSERT IN ...
- Linux性能监控工具 gtop
给大家介绍一款性能监控工具,个人对比界面比top美观,常用指标比较清晰毕竟top上的指标不是每个人都能熟悉,也不是所有指标参数都需要看,对于新手也不便查找,好了说的再多先上图大家参观一下. 1.安装需 ...
- bootstrap模态框传值操作
1.bootstrap模态框之html代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"& ...
- springboot properties文件中的数据通过@Value()形式注入
首先在resources目录下新建一个properties文件,如下图 在photoPath.properties中写入内容,key=value的形式,如下图 在你需要引用properties的类头部 ...
- maven 打包失败 提示找不到jar的问题
服务打包的时候发现打包失败,查看原因大概是找不到jar包,但是本地仓库有.最终的解决的办法是使用maven 命令重新把jar包安装到本地仓库. 命令如下:mvn install:install-fil ...
- Python学习:3.Python学习基础
Python基础概念 一.编码 Python解释器加载.py文件中的代码的时候,对内容进行编码,在Python2.x中默认使用的是ASCII,因此我们使用Python2.x版本输出中文的时候,会出现以 ...
- PHP json_decode返回null解析失败原因
在PHP5.4之前 json_decode函数有两个参数json_decode有两个参数,第一个是待解析的字符串,第二个是是否解析为Array json_decode要求的字符串比较严格:(1)使用U ...