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. SQL、T-SQL与PL-SQL的区别

    SQL.T-SQL与PL-SQL的区别 SQL是Structrued Query Language的缩写,即结构化查询语言.它是负责与ANSI(美国国家标准学会)维护的数据库交互的标准.作为关系数据库 ...

  2. BigDecimal运算(加、减、乘、除)

    public class BigDecimalOperation { private BigDecimalOperation(){ } public static BigDecimal add(dou ...

  3. Vue--- 使用vuex使用流程 1.0

    Vuex 1.安装vuex npm install  -save vuex 2. 引入 创建store文件夹目录 创建 vuex     指挥公共目录    store['state','action ...

  4. meclipse6.5破解

    package com.test.ssh.common;   import java.text.DecimalFormat; import java.text.NumberFormat; import ...

  5. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--F-等式

    链接:https://www.nowcoder.com/acm/contest/90/F 来源:牛客网 1.题目描述 给定n,求1/x + 1/y = 1/n (x<=y)的解数.(x.y.n均 ...

  6. 替换html里面的\r\n及解决记事本中的每个段落只有一行的情形

    1. 在用python爬取小说的时候, 发现在内容里每次换行都有\r\n(即回车, 换行)出现. 此时可以采用  s.replace('\\r\\n','') , 其中s为字符串类型. 2. 在爬取完 ...

  7. 判断无向图两点间是否存在长度为K的路径

    #include <iostream> #include <vector> #define MAXN 5 using namespace std; struct edge { ...

  8. Sonarqube中文插件-Linux[20180105]

    前言     上次安装了Sonarqube英文版使用起来不方便,这次为Sonarqube安装中文插件. 前期准备:     软件下载: https://github.com/SonarQubeComm ...

  9. TcpServer 使用简介

    1.简介 1) Poco 的 TcpServer 是一个多线程的 Tcp 服务器. 服务器使用 ServerSocket(Poco 的一个用于初始化服务器的socket的类) 来接收链接.Server ...

  10. 服务命令只支持基本的LSB操作(启动、停止、重新启动、尝试重启、重新加载、强制重新加载、状态)。对于其他操作,请尝试使用systemctl。

    The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, forc ...