UVa 11297 Census (二维线段树)
题意:给定上一个二维矩阵,有两种操作
第一种是修改 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 (二维线段树)的更多相关文章
- UVA 11297 Census ——二维线段树
[题目分析] 二维线段树模板题目. 简直就是无比的暴力.时间复杂度为两个log. 标记的更新方式比较奇特,空间复杂度为N^2. 模板题目. [代码] #include <cstdio> # ...
- UVA 11297 Census(二维线段树)
Description This year, there have been many problems with population calculations, since in some cit ...
- UVA 11297 线段树套线段树(二维线段树)
题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要 不同的处理方式,非叶子形成的 ...
- POJ2155 Matrix二维线段树经典题
题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...
- HDU 1823 Luck and Love(二维线段树)
之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...
- poj 2155:Matrix(二维线段树,矩阵取反,好题)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17880 Accepted: 6709 Descripti ...
- poj 1195:Mobile phones(二维线段树,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14391 Accepted: 6685 De ...
- POJ 2155 Matrix (二维线段树)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17226 Accepted: 6461 Descripti ...
- HDU 4819 Mosaic (二维线段树)
Mosaic Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total S ...
随机推荐
- php 的两个扩展 memcache 和 memcachd
今天绕了很大弯子, 因为自己写的php的memcache类. 于是出现了下面问题 在本地测试好好的, 线上就出了问题 原因是线上使用的是memcache, 我本地使用的是memcached 区别参考网 ...
- C++ 函数特性_参数默认值
函数参数默认值写法 有默认参数值的参数必须在参数表的最右边 ,) // 这是正确的写法 , int k) // 这是错误写法 先声明,后定义 在写函数时要先在代码前面声明,然后再去定义. 函数默认参数 ...
- pythonNet day03
TCP应用之 httpserver 1.接收http请求 2.查看http请求 3.返回一个网页给客户端 # 做的是一个本地服务端,接收来自浏览器客户端的请求 from socket import * ...
- Vue 获取数据、事件对象、todolist
vue中在方法里获取data里的msg:this.msg 在微信小程序里this.data.msg 改变data里的msg:this.msg="改变后的msg" 可以通过list. ...
- tensorflow下载和安装
下载以及安装 选择类型 必须选择以下类型的TensorFlow之一来安装: TensorFlow仅支持CPU支持.如果您的系统没有NVIDIA®GPU,则必须安装此版本.请注意,此版本的Tenso ...
- 用 Vue 改造 Bootstrap,渐进提升项目框架
前言 Vue 横空出世,以迅雷不及掩耳之势横扫前端界,俨然有当年 jQuery 之势.我认为 Vue 成功的关键在于三点: 学习曲线平缓,有点经验的前端基本上一天就能看完文档,然后就可以上手操作. 上 ...
- mybatis 需要注意的点 MyBatis 插入空值时,需要指定JdbcType (201
转自:https://blog.csdn.net/snakemoving/article/details/76052875 前天遇到一个问题 异常显示如下: 引用 Exception in threa ...
- Tomcat 实战-调优方案
来自: http://blog.csdn.net/u010028869/article/details/51793821 来自: https://www.cnblogs.com/baihuites ...
- torque
torque - 必应词典 美[tɔrk]英[tɔː(r)k] n.(使机器等旋转的)转矩 网络扭矩:扭力:力矩 变形过去分词:torqued:现在分词:torquing:第三人称单数:torques ...
- springboot 配置jsp支持
springboot默认并不支持jsp模板,所以需要配置. 下面是一个可以运行的例子: 首先配置属性文件: spring.http.encoding.force=true spring.http. ...