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

第一种是修改 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. LeetCode OJ - Best Time to Buy and Sell Stock

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xiezhihua120/article/details/32939749 Say you have ...

  2. Eclipse添加中文javadoc

    SUN官方API中文版[JDK1.6]1.6API文档(中文)的下载地址:ZIP格式用来设置javadoc,下载地址:http://download.java.net/jdk/jdk-api-loca ...

  3. [LeetCode系列] K节点倒序问题迭代解法

    给定链表和整数k, 使用in-space方法将链表按k个为一组进行倒序, 如果剩余个数不足k个则保留其原始顺序. 如给定1->2->3->4->5, k = 2, 需要返回 2 ...

  4. cocos2dx继承结构图

    包含关系 CCDirector->CCScene->CCLayer->CCSprite->CCAction 继承关系 CCObject---CCAction(动作,控制图层运动 ...

  5. android中一个评分的控件

    RatingBar android中一个评分的控件 如何使用 Android Studio下: dependencies { compile 'com.hedgehog.ratingbar:app:1 ...

  6. 利用IOS画图功能画出五角星,并且可以调整五角星的填充范围

    我们要花的为一个黄色的五角星并且其中的填充黄色能够任意调整,比如只填满半个五角星,或者只填满一个角等等. 首先要重写DrawRect 方法,然后在这里实现我们的画图代码. - (void)drawRe ...

  7. 深入浅出 Java Concurrency (5): 原子操作 part 4 CAS操作

    在JDK 5之前Java语言是靠synchronized关键字保证同步的,这会导致有锁(后面的章节还会谈到锁). 锁机制存在以下问题: (1)在多线程竞争下,加锁.释放锁会导致比较多的上下文切换和调度 ...

  8. Pthreads 信号量,路障,条件变量

    ▶ 使用信号量来进行线程间信息传递 ● 代码 #include <stdio.h> #include <pthread.h> #include <semaphore.h& ...

  9. IOS 上架要求视频及屏幕截屏

    客户提供上架的资料 1.IOS 上架要求视频演示,录制一段视频,上传到优酷,需要url连接. 2.手机截屏,每个尺寸5张.5s/6/6p *5=15张.截屏图片分辨率. iPhone4s手机 3.5I ...

  10. 使用API调用外部程序并监控程序状态

    Public Type SHELLEXECUTEINFO    cbSize As Long    fMask As Long    hwnd As Long    lpVerb As String  ...