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 ...
随机推荐
- Restframework中常见API的编写方式
1.框架一(继承APIView) 这里的第一部分使用骨架请参考我的博客(第三篇),它采用了restframework中最基础的办法(APIView)实现了相关请求,以下的框架都是基于它的 2.框架二( ...
- C# sqlhelp
public class SqlHelp { //数据库连接字符串 public static string connectionString = ConfigurationManager.Conne ...
- vue 父子组件相互传值
子传父 逻辑: 单击子组件的按钮 ,触发它的单击事件 通过 $emit 触发父级自定义事件 并传一个值给父级 <div id="id"> <h3>儿子 ...
- Java中Redis缓存
1:安装 安装可分为单机版redis 和集群版redis 安装比较简单,自行百度即可 2:集成 pom文件中加入jedis 依赖,spring创建redis的application-resid配置, ...
- Git----将本地代码推送到远程仓库
1.初始化本地 git init 2.添加文件 -A等于 -. 和-a的集合 git add -A 3.提交 git commit -m 'add' 4.关联到远程库 git remote add o ...
- Windows使用Node.js自动生成Vue.js模版环境部署步骤-----记录
node.js官网下载并安装node 进入node文档目录下,运行cmd 输入 node -v 查看node版本 出现表示安装完成 输入 npm -v 显示npm版本信息 安装cnpm 输入 npm ...
- 爬虫之request模块高级
一.cookie&session cookie:服务器端使用cookie来记录客户端的状态信息 实现流程: 执行登陆操作(获取cookie) 在发起个人主页请求时,需要将cookie携带到该请 ...
- 笨方法学python之import sys与from sys import argv的区别
这是在网上看到的一个大神的解答: sys is a module that contains “system functionality”. sys.argv is a list containing ...
- Tensorflow的最佳实践
Tensorflow的最佳实践 1.变量管理 Tensorflow提供了变量管理机制,可直接通过变量的名字获取变量,无需通过传参数传递数据.方式如下: #以下为两种创建变量的方法 v=tf.get ...
- hoj第三场G-manhattanp ositioning system
---恢复内容开始--- 一.题意 在二维坐标系内,给定若干个点和目标点距离该点的曼哈顿距离,求是否存在唯一解使得目标点坐标为整数. 二.题解 重新思考题目模型,考虑曼哈顿距离一定时,几何含义为,以给 ...