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 ...
随机推荐
- 建立dblink(database link)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/bisal/article/details/26730993 database linke是建立一个数 ...
- linux下nginx安装、配置实战
1什么是Nginx Nginx("enginex")是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器,在高连接并发的情况下Nginx是Apac ...
- settimeout()在IE8下参数无效问题解决方法
遇到这个问题,setTimeout(Scroll(),3000); 这种写法在IE8 下 不能够执行,提示参数无效, setTimeout(function(){Scroll()},3000);这种方 ...
- debian7配置iptables
vim /etc/iptables.rule 文件内容如下 *filter # Allows all loopback (lo0) traffic and drop all traffic to / ...
- Conv
folly/Conv.h folly/Conv.h is a one-stop-shop for converting values across types. Its main features a ...
- 安装Android studio出现'tools.jar' seems to be not in Android Studio classpath......的解决方法
安装Android studio出现'tools.jar' seems to be not in Android Studio classpath......的解决方法 原创 2015年07月31日 ...
- linux性能调优
1-1.0 关于ulimit linux对每个用户,系统限制其最大进程数.为提高性能,可根据设备资源情况,设置各linux用户最大进程数. [Qrui@root ~]#ulimit -a 用来显示当 ...
- django2.0数据展示流程
之前刚刚实现了数据添加的流程,那么数据展示是怎么回事 1 先在 views.py 中定义函数 增加获取数据的方式 from django.shortcuts import render from bl ...
- 使用python读取大文件
python中读取数据的时候有几种方法,无非是read,readline,readlings和xreadlines几种方法,在几种方法中,read和xreadlines可以作为迭代器使用,从而在读取大 ...
- leetcode475
public class Solution { public int FindRadius(int[] houses, int[] heaters) { houses = houses.Distinc ...