题意:给定上一个二维矩阵,有两种操作

第一种是修改 c x y val 把(x, y) 改成 val

第二种是查询 q x1 y1 x2 y2 查询这个矩形内的最大值和最小值。

析:二维线段树裸板。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("in.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 500 + 10;
const int mod = 1000;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r > 0 && r <= n && c > 0 && c <= m;
} int minv[maxn<<2][maxn<<2];
int maxv[maxn<<2][maxn<<2];
int mmin, mmax; void push_upy(int x, int rt){
minv[x][rt] = min(minv[x][rt<<1], minv[x][rt<<1|1]);
maxv[x][rt] = max(maxv[x][rt<<1], maxv[x][rt<<1|1]);
}
void push_upx(int rt, int x){
minv[rt][x] = min(minv[rt<<1][x], minv[rt<<1|1][x]);
maxv[rt][x] = max(maxv[rt<<1][x], maxv[rt<<1|1][x]);
} void buildy(int x, int l, int r, int rt, bool ok){
if(l == r){
if(ok){ scanf("%d", minv[x]+rt); maxv[x][rt] = minv[x][rt]; return ; }
push_upx(x, rt);
return ;
}
int m = l + r >> 1;
buildy(x, lson, ok);
buildy(x, rson, ok);
push_upy(x, rt);
} void buildx(int l, int r, int rt){
if(l == r){ buildy(rt, all, 1); return ; }
int m = l + r >> 1;
buildx(lson);
buildx(rson);
buildy(rt, all, 0);
} void updatey(int x, int Y, int val, int l, int r, int rt, bool ok){
if(l == r){
if(ok){ minv[x][rt] = maxv[x][rt] = val; return ; }
push_upx(x, rt);
return ;
}
int m = l + r >> 1;
if(Y <= m) updatey(x, Y, val, lson, ok);
else updatey(x, Y, val, rson, ok);
push_upy(x, rt);
} void updatex(int X, int Y, int val, int l, int r, int rt){
if(l == r){ updatey(rt, Y, val, all, 1); return ; }
int m = l + r >> 1;
if(X <= m) updatex(X, Y, val, lson);
else updatex(X, Y, val, rson);
updatey(rt, Y, val, all, 0);
} void queryy(int x, int L, int R, int l, int r, int rt){
if(L <= l && r <= R){
mmin = min(mmin, minv[x][rt]);
mmax = max(mmax, maxv[x][rt]);
return ;
}
int m = l + r >> 1;
if(L <= m) queryy(x, L, R, lson);
if(R > m) queryy(x, L, R, rson);
} void queryx(int L, int R, int Y1, int Y2, int l, int r, int rt){
if(L <= l && r <= R){
queryy(rt, Y1, Y2, all);
return ;
}
int m = l + r >> 1;
if(L <= m) queryx(L, R, Y1, Y2, lson);
if(R > m) queryx(L, R, Y1, Y2, rson);
} int main(){
while(scanf("%d", &n) == 1){
buildx(1, n, 1);
scanf("%d", &m);
char op[5];
int x1, y1, x2 ,y2;
while(m--){
scanf("%s %d %d %d", op, &x1, &y1, &x2);
if(op[0] == 'c') updatex(x1, y1, x2, all);
else{
scanf("%d", &y2);
mmin = INF; mmax = 0;
queryx(x1, x2, y1, y2, all);
printf("%d %d\n", mmax, mmin);
}
}
}
return 0;
}

  

UVa 11297 Census (二维线段树)的更多相关文章

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

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

  2. UVA 11297 Census(二维线段树)

    Description This year, there have been many problems with population calculations, since in some cit ...

  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. php switch case的"bug"

    首先说明,这不是一个bug.应该说是一个比较容易中招的陷阱. 今天使用switch遇到一个问题,代码如下: <?php $num = 0; switch ($price) { case $pri ...

  2. bzoj4331: JSOI2012 越狱老虎桥

    Description 这里,是美丽的南京:这里,是秀美的进香河:这里是安逸的老虎桥.  如果说进香河的美,美在其秀美的风光,倒不如说是美在了那惬意的南京古典小巷式生活.如果说进香河的迷人,在其淳朴的 ...

  3. bzoj2783 树

    第一行是两个整数N和S,其中N是树的节点数. 第二行是N个正整数,第i个整数表示节点i的正整数. 接下来的N-1行每行是2个整数x和y,表示y是x的儿子. 输出格式: 输出路径节点总和为S的路径数量. ...

  4. jsp---猜数字游戏,深有感触

    猜数字游戏注意两点.1.随机数和猜的数字不能放在同一个页面,不然随机数不停出现,猜的数字不可能相等的. 2.数据类型的相互转换.包装类Integer和int的用法,前者是类,后者是基本数据类型 cai ...

  5. 五、jdk工具之jmap(java memory map)、 mat之四--结合mat对内存泄露的分析、jhat之二--结合jmap生成的dump结果在浏览器上展示

    目录 一.jdk工具之jps(JVM Process Status Tools)命令使用 二.jdk命令之javah命令(C Header and Stub File Generator) 三.jdk ...

  6. Air test 基于屏幕比例实现滑动的方法

    # -*- encoding=utf8 -*- __author__ = "chenshanju" __docs__ = "基于iOS类实现滑动" from a ...

  7. ROS创建Web代理(Web proxy)给QQ使用HTTP代理

    使用Web代理可以提高网页的访问速度,因为访问的数据会存储在内存或是硬盘中,就会直接从代理服务器中读取.同时,为了提高网络访问的安全性,可以给Web代理服务器设置相应的权限,使它的安全性得到提高. 下 ...

  8. httpclient的几种请求URL的方式

    一.httpclient项目有两种使用方式.一种是commons项目,这一个就只更新到3.1版本了.现在挪到了HttpComponents子项目下了,这里重点讲解HttpComponents下面的ht ...

  9. 转 linux 下装 usb driver

    http://www.george-smart.co.uk/wiki/Xilinx_JTAG_Linux

  10. 安装wget

    今天给服务器安装新LNMP环境时,wget 时提示 -bash:wget command not found,很明显没有安装wget软件包.一般linux最小化安装时,wget不会默认被安装. 可以通 ...