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(二维线段树)的更多相关文章

  1. UVa 11297 Census (二维线段树)

    题意:给定上一个二维矩阵,有两种操作 第一种是修改 c x y val 把(x, y) 改成 val 第二种是查询 q x1 y1 x2 y2 查询这个矩形内的最大值和最小值. 析:二维线段树裸板. ...

  2. UVA 11297 Census ——二维线段树

    [题目分析] 二维线段树模板题目. 简直就是无比的暴力.时间复杂度为两个log. 标记的更新方式比较奇特,空间复杂度为N^2. 模板题目. [代码] #include <cstdio> # ...

  3. UVA 11297 线段树套线段树(二维线段树)

    题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要  不同的处理方式,非叶子形成的 ...

  4. POJ2155 Matrix二维线段树经典题

    题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...

  5. HDU 1823 Luck and Love(二维线段树)

    之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...

  6. poj 2155:Matrix(二维线段树,矩阵取反,好题)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17880   Accepted: 6709 Descripti ...

  7. poj 1195:Mobile phones(二维线段树,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14391   Accepted: 6685 De ...

  8. POJ 2155 Matrix (二维线段树)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17226   Accepted: 6461 Descripti ...

  9. HDU 4819 Mosaic (二维线段树)

    Mosaic Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total S ...

随机推荐

  1. JavaScript中的Map和Set

    JavaScript的默认对象表示方法{}可以视为其他语言中的Map或者Dictionary的数据结构,即一组键值对. 但是JavaScript的对象有个小问题,就是键必须是字符串,但实际上Numbe ...

  2. Docker官方文档翻译1

    转载请标明出处: https://blog.csdn.net/forezp/article/details/80098675 本文出自方志朋的博客 本系列教程翻译于docker文档,文档地址:http ...

  3. SpringBoot学习16:springboot整合junit单元测试

    1.创建maven项目,修改pom.xml文件 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springfram ...

  4. rest_framework --- APIView

    一.什么是rest_framework 它是基于Django的,帮助我们快速开发符合RESTful规范的接口框架. 安装方式有很多种,可以通过pip,或者在pycharm中安装也可以 二.APIVie ...

  5. 牛客小白月赛2 I 艺 【归并思想】【离散化】

    链接:https://www.nowcoder.com/acm/contest/86/I来源:牛客网 题目描述 接下去,Sεlιнα(Selina) 又搞了个文艺竞演. 虽说是文艺竞演,其实只是为了满 ...

  6. js 关于字符串转数字及数字保留位数的控制

    1.parseInt()和parseFloat()两个转换函数,将字符串转换成相应的数字. 1.parseInt() parseInt进行转换时,将字符串转成相应的整数.浮点数以后的数字都不要了. p ...

  7. 谈谈toLocaleString()

    如何理解toLocaleString()? toLocaleString()就是把数组转换为本地字符串.首先调用每个数组元素的toLocaleString()方法,然后使用地区特定的分隔符把生成的字符 ...

  8. 常见的Dom操作

    1.什么是DOM? DOM又称文档对象模型( DOM, Document Object Model )主要用于对HTML和XML文档的内容进行操作.DOM描绘了一个层次化的节点树,通过对节点进行操作, ...

  9. 介绍几个PHP 自带的加密解密函数

    PHP 自带的加密解密函数 目前经常使用的加密函数有:md5(), sha1(), crypt(), base64_encode(), urlencode() . 其中 md5(), sha1(), ...

  10. dedecms添加/编辑文章如何把附加选项去掉默认勾选状态

    1.去掉添加时默认勾选状态. 在 系统->系统基本参数->其它选项 中,如图中的三个选项选择否即可. 设置完后可以看到添加时已经默认不勾选,但是编辑文章时还是默认勾选状态. 2.去掉编辑时 ...