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

第一种是修改 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. 在AD中存取照片

    AD中有存放照片的字段吗? 答案肯定是有的.photo,jpegPhoto,thumbnailPhoto 前端时间客户,包括领导 在问通讯录中的照片为什么存在数据库中而不是AD中,AD中的属性能不能利 ...

  2. 阿里云 搭建Git服务器

    1. 服务端安装git, ssh, 客户端安装git sudo apt-get install git sudo apt-get install ssh 2. 服务单创建用户server sudo a ...

  3. MFC vs. SDK程序流程

    大家都知道,windows API编程以及其消息处理,其过程都清晰可见,大体步骤如下: 1)声明消息窗口类 2)注册窗口类 3)createwindows 4)消息获得以及分派(windows pro ...

  4. Samba服务创建共享文件系统

    Linux 系统中的Samba Linux系统中的Samba服务器又提供了另外一种技术来弥补这种安全性的不足的技术,那就是采用账户映射方式为Samba服务器提供虚拟账户(不与Linux系统中的用户账户 ...

  5. 解决phpexcel保存时文件命中文出现 乱码 (这个真的有用)

    Phpexcel导出Excel文件时有两个主要的过程: 1.定义文件名 2.填充Excel数据 这两个过程中可能会出现一些乱码问题,下面我来说一下解决办法: 解决文件名的乱码: 乱码原因:客户使用的中 ...

  6. 并发包学习(一)-Atomic包小记

    此篇是J.U.C学习的第一篇Atomic包相关的内容,希望此篇总结能对自己的基础有所提升.本文总结来源自<Java并发编程的艺术>第七章并配以自己的实践理解.如有错误还请指正. 一.案例分 ...

  7. padding margin border 和元素大小

    元素占用宽度 = 元素宽度+padding+border+margin 注意margin只是隔开元素,不会使得元素变大,而padding会使得元素变大,也就是说 元素真实宽度=元素宽度+padding ...

  8. 迷你MVVM框架 avalonjs 1.2.5发布

    avalon1.2.5发布,升级ms-widget,整合avalon.require.text到核心,并且修复了avalon.mobile的avalon.innerHTML方法的BUG,让它能执行脚本 ...

  9. Kubernetes集群的安装部署

    此文参照https://www.cnblogs.com/zhenyuyaodidiao/p/6500830.html,并根据实操过程略作修改. 1.环境介绍及准备: 1.1 物理机操作系统 物理机操作 ...

  10. String和CharSequence

    String 是java中的字符串,它继承于CharSequence. String类所包含的API接口非常多.为了便于今后的使用,我对String的API进行了分类,并都给出的演示程序. Strin ...