hdu 4819 Mosaic
无论是线段树还是树状数组维护最大值最小值的时候一定要注意,如果有修改操作的话,这个最小值和最大值的更新一定不能由原来的和修改的值得到,一定要重新查询一次,否则可能出现当前最小值是原来的未修改值,但事实上若修改了,此最小值不存在了。此题线段树套线段树,写的代码有点挫了。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 805
#define ull unsigned long long
#define ll long long
#define hashmod 99999839
#define mod 7
#define repe(x,y,i) for(i=x;i<=y;++i)
#define repne(x,y,i) for(i=x;i<y;++i)
#define MAX(x,y) (x) < (y) ? (y) : (x);
#define MIN(x,y) (x) < (y) ? (x) : (y);
int rson[maxn<<][],nex[maxn<<],lenr,lenc,rootr;
int cson[(maxn*maxn)<<][],mi[(maxn*maxn)<<],ma[(maxn*maxn)<<];
int a[maxn][maxn],n,ls,rs,fa,x,y,z;
int cx,cy,rx,ry;
inline void pushup(int o){
ls = cson[o][],rs = cson[o][];
mi[o] = MIN(mi[ls],mi[rs]);
ma[o] = MAX(ma[ls],ma[rs]);
}
void find(int o){
int l = ,r = n,mid = (l + r) >> ;
while(l < r){
if(mid < x) o = cson[o][],l = mid + ;
else o = cson[o][],r = mid;
mid = (l + r) >> ;
}
x = mi[o],y = ma[o];
}
int buildc(int l,int r){
int o = lenc;
lenc++;
if(l == r){//其最小,最大值可由行树的左右子树的根指向的列树中得到
if(rx == ry) mi[o] = ma[o] = a[rx][l];//如果是同一行则直接得到
else{
mi[o] = INF,ma[o] = -;
ls = nex[rson[fa][]],rs = nex[rson[fa][]];
x = l,y = r;
find(ls);
mi[o] = MIN(mi[o],x);
ma[o] = MAX(ma[o],y);
x = l,y = r;
find(rs);
mi[o] = MIN(mi[o],x);
ma[o] = MAX(ma[o],y);
}
cson[o][] = cson[o][] = -;
return o;
}
int mid = (l + r) >> ;
cson[o][] = buildc(l,mid),cson[o][] = buildc(mid+,r),pushup(o);
return o;
}
int buildr(int l,int r){
int o = lenr;
lenr++;
if(l == r){
rson[o][] = rson[o][] = -,fa = o,rx = l,ry = r;
nex[o] = buildc(,n);
return o;
}
int mid = (l + r) >> ;
rson[o][] = buildr(l,mid),rson[o][] = buildr(mid+,r),fa = o,rx = l,ry = r;
nex[o] = buildc(,n);
return o;
}
void queryc(int o,int l,int r){
if(r < cx || l > cy) return;
if(l >= cx && r <= cy){x = MIN(x,mi[o]);y = MAX(y,ma[o]);return;}
int mid = (l + r) >> ;
queryc(cson[o][],l,mid),queryc(cson[o][],mid+,r);
}
void queryr(int o,int l,int r){
if(r < rx || l > ry) return;
if(l >= rx && r <= ry){ queryc(nex[o],,n);return;}
int mid = (l + r) >> ;
queryr(rson[o][],l,mid),queryr(rson[o][],mid+,r);
}
void updatec(int o,int l,int r){
if(l == r){
if(rx == ry) mi[o] = ma[o] = z;
else {
mi[o] = INF,ma[o] = -;
ls = nex[rson[fa][]],rs = nex[rson[fa][]];
int tx = x,ty = y;
x = l,y = r;
find(ls);
mi[o] = MIN(mi[o],x);
ma[o] = MAX(ma[o],y);
x = l,y = r;
find(rs);
mi[o] = MIN(mi[o],x);
ma[o] = MAX(ma[o],y);
x = tx,y = ty;
}
return;
}
int mid = (l + r) >> ;
if(mid >= y) updatec(cson[o][],l,mid);
else updatec(cson[o][],mid+,r);
pushup(o);
}
void updater(int o,int l,int r){
if(l == r){rx = ry = l,fa = o,updatec(nex[o],,n);return;}
int mid = (l + r) >> ;
if(mid >= x) updater(rson[o][],l,mid);
else updater(rson[o][],mid+,r);
rx = l,ry = r,fa = o;
updatec(nex[o],,n);
}
int main(){
freopen("a.in","r",stdin);
freopen("b.out","w",stdout);
int q,t = ,T;
scanf("%d",&T);
register int i,j;
while(T--){
scanf("%d",&n);
repe(,n,i) repe(,n,j) scanf("%d",&a[i][j]);
lenr = lenc = ;
rootr = buildr(,n);
scanf("%d",&q);
printf("Case #%d:\n",++t);
repe(,q,i){
scanf("%d%d%d",&x,&y,&z);
rx = x - (z>>),cx = y - (z>>),ry = x + (z>>),cy = y + (z>>);
if(rx <= ) rx = ;if(cx <= ) cx = ;if(ry > n) ry = n;if(cy > n) cy = n;
int tx = x,ty = y;
x = INF,y = -,queryr(rootr,,n),z = (x + y) >> ,printf("%d\n",z);
x = tx,y = ty,updater(rootr,,n);
}
}
return ;
}
hdu 4819 Mosaic的更多相关文章
- HDU 4819 Mosaic(13年长春现场 二维线段树)
HDU 4819 Mosaic 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4819 题意:给定一个n*n的矩阵,每次给定一个子矩阵区域(x,y,l) ...
- HDU 4819 Mosaic D区段树
连接:pid=4819">http://acm.hdu.edu.cn/showproblem.php?pid=4819 意:给出一个800×800下面的矩阵.每次更新一个点的值为以这个 ...
- HDU 4819 Mosaic (二维线段树)
Mosaic Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total S ...
- HDU 4819 Mosaic 二维线段树
Mosaic Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- hdu 4819 Mosaic 树套树 模板
The God of sheep decides to pixelate some pictures (i.e., change them into pictures with mosaic). He ...
- HDU 4819 Mosaic --二维线段树(树套树)
题意: 给一个矩阵,每次查询一个子矩阵内的最大最小值,然后更新子矩阵中心点为(Max+Min)/2. 解法: 由于是矩阵,且要求区间最大最小和更新单点,很容易想到二维的线段树,可是因为之前没写过二维的 ...
- HDU 4819 Mosaic (二维线段树&区间最值)题解
思路: 二维线段树模板题,马克一下,以后当模板用 代码: #include<cstdio> #include<cmath> #include<cstring> #i ...
- HDU 4819 Mosaic 【二维线段树】
题目大意:给你一个n*n的矩阵,每次找到一个点(x,y)周围l*l的子矩阵中的最大值a和最小值b,将(x,y)更新为(a+b)/2 思路:裸的二维线段树 #include<iostream> ...
- hdu 4819 二维线段树模板
/* HDU 4819 Mosaic 题意:查询某个矩形内的最大最小值, 修改矩形内某点的值为该矩形(Mi+MA)/2; 二维线段树模板: 区间最值,单点更新. */ #include<bits ...
随机推荐
- http://bbs.chinaunix.net/thread-1463276-1-1.html
http://bbs.chinaunix.net/thread-1463276-1-1.html
- java操作Excel、PDF文件
java操作Excel.PDF文件 分享者:Vashon 分享来源:CSDN博客 下面这些是在开发中用到的一些东西,有的代码贴的不是完整的,只是贴出了关于操作EXCEL的代码: jxl是一个*国人写的 ...
- 用JS检测页面加载的不同阶段状态
这可以通过用document.onreadystatechange的方法来监听状态改变, 然后用document.readyState == “complete”判断是否加载完成. 可以采用2个div ...
- Ubuntu14.04 LTS安装 OpenCV-3.0.0-rc1 + QT5.4.1
I 安装配置工作前的准备 2 II 安装 OpenCV 2 III 安装QT 3 IV 使QT能够使用OpenCV 3 如果顺利,整个过程应该3个小时左右能够完成. 我整个过程用了一早上,配置过程中有 ...
- 【HEVC帧间预测论文】P1.2 An Efficient Inter Mode Decision Approach for H.264 Video Codin
参考:An Efficient Inter Mode Decision Approach for H.264 Video Coding <HEVC标准介绍.HEVC帧间预测论文笔记>系列博 ...
- javascript中 if(变量)和if(变量==true)的区别
if(判断表达式){执行内容} 如果判断表达式为true,则执行括号中的内容.这里,变量如果不为0,null,undefined,false,都会被处理为true.只要变量有非0的值或是某个对象,数组 ...
- os x 中出现message sent to deallocated instance 的错误总结
一般是程序中的某一个对象被release 了两次 一般情况下是与你定义的类型有关 这里面我的错误是吧 NSString 类型的变量的属性 设置为了 assign 了 目测与这个有关 补充object- ...
- toplink
TopLink,是位居第一的Java对象关系可持续性体系结构,原署WebGain公司的产品,后被Oracle收购,并重新包装为Oracle AS TopLink.TOPLink为在关系数据库表中存储 ...
- 原生ajax请求的五个步骤
//第一步,创建XMLHttpRequest对象 var xmlHttp = new XMLHttpRequest(); function CommentAll() { //第二步,注册回调函数 xm ...
- linux下的基础操作
Xfce 终端: Linux 命令行终端,打开后会进入 zsh Shell 环境,可以使用 Linux 命令. NetSurf 网络浏览器:浏览器,可以用在需要前端界面的课程里,只需要打开环境里写的 ...