UVA 11297 Census(二维线段树)
2013-09-13 23:21
591 查看
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 = 2010; 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) >> 1; if(y1 <= M) query1D(o * 2, L, M); if(M < y2) query1D(o * 2 + 1, M + 1, R); } } void query2D(int o, int L, int R) { if(x1 <= L && R <= x2) {xo = o; query1D(1, 1, m);} else { int M = (L + R) >> 1; if(x1 <= M) query2D(o * 2, L, M); if(M < x2) query2D(o * 2 + 1, M + 1, 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 * 2][o], Max[xo * 2 + 1][o]); Min[xo][o] = min(Min[xo * 2][o], Min[xo * 2 + 1][o]); } else { int M = (L + R) >> 1; if(y <= M) modify1D(o * 2, L, M); else modify1D(o * 2 + 1, M + 1, R); Max[xo][o] = max(Max[xo][o * 2], Max[xo][o * 2 + 1]); Min[xo][o] = min(Min[xo][o * 2], Min[xo][o * 2 + 1]); } } void modify2D(int o, int L, int R) { if(L == R) {xo = o; xleaf = 1; modify1D(1, 1, m);} else { int M = (L + R) / 2; if(x <= M) modify2D(o * 2, L, M); else modify2D(o * 2 + 1, M + 1, R); xo = o; xleaf = 0; modify1D(1, 1, m); } } void query() {vmax = -INF; vmin = INF; query2D(1, 1, n);} void modify() {modify2D(1, 1, n);} } t; int main() { int n, m, Q; char op[10]; scanf("%d%d", &n, &m); t.n = n; t.m = m; for(int i = 1; i <= n; ++i) for(int j = 1; 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 0; }
View Code
相关文章推荐
- UVa 11297 Census 二维线段树模板
- Uva 11297 Census,二维线段树,板子
- UVa 11297 Census(二维线段树+单点更新)
- UVA11297--Census(二维线段树)
- UVA-11297-Census(二维线段树RMQ,单点修改)
- Uva 11297 Census 二维线段树
- UVa 11297 Census (二维线段树)
- UVA:11297-Census(二维线段树)
- 二维线段树UVA11297
- UVA 11297 线段树套线段树(二维线段树)
- UVA-11297 Census(线段树套线段树)
- Uva 11297 Census(线段树)
- UVA 11297 Census(2D线段树解决子矩阵的最大最小)
- 二维线段树区间修改(add,set)uva11992
- 【UVA11992】Fast Matrix Operations——二维线段树
- UVALive 6709 - Mosaic 二维线段树
- uva11297 Census 【二维线段树】
- UVA - 12074 The Ultimate Bamboo Eater 二维线段树
- Uva 11992 Fast Matrix Operations (二维线段树)
- UVa 11297 - Census