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 ...
随机推荐
- Understanding Scroll Views 深入理解 scroll view 读书笔记
Understanding Scroll Views 深入理解 scroll view 读书笔记 It may be hard to believe, but a UIScrollView is ...
- js Math 对象
Math 对象方法 方法 描述 abs(x) 返回数的绝对值. acos(x) 返回数的反余弦值. asin(x) 返回数的反正弦值. atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值 ...
- 【转】Delphi 2010 Lite加装帮助文件
基于爱好,下载了一个delphi 2010 lite,业余玩玩. 不过这东西是网友重新打包的,没有带帮助.在网上搜索一下加摸索后搞定.步骤如下: Delphi 2010本身的帮助(MSDN风格的)1. ...
- SecureCRT 64位 破解版v8.1.4
http://www.xue51.com/soft/1510.html#xzdz securecrt 破解版是一款支持SSH1和SSH2的终端仿真程序,这个程序能够在windows系统中登陆UNIX或 ...
- 标量子查询SQL改写
一网友说下面sql跑的好慢,让我看看 sql代码: select er, cid, pid, tbl, zs, sy, (select count(sr.mobile_tele_no) from tb ...
- Java:获取IP地址
文章来源:https://www.cnblogs.com/hello-tl/p/9139323.html import java.net.InetAddress; import java.net.Un ...
- LeetCode(86) Partition List
题目 Given a linked list and a value x, partition it such that all nodes less than x come before nodes ...
- LeetCode(19) Remove Nth Node From End of List
题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...
- 关于shell中常见功能的实现方式总结
一.shell脚本中连接数据库 二.
- Leetcode 310.最小高度树
最小高度树 对于一个具有树特征的无向图,我们可选择任何一个节点作为根.图因此可以成为树,在所有可能的树中,具有最小高度的树被称为最小高度树.给出这样的一个图,写出一个函数找到所有的最小高度树并返回他们 ...