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 ...
随机推荐
- jq获取设置选中值
var standard = $('input[name="standard"]:checked').val(); $("input[name='advertByid'] ...
- sybase sql anywhere 5.0 安装后sybase central中无法打开视图等的解决办法
无法打开的原因初步分析要用英文版的xp,后来在如下处发现问题,是sql anywhere的版本太旧了, 可能没有使用Unicode编码,设置一下如下选项可以解决问题.
- MYSQL 二次筛选,统计,最大值,最小值,分组,靠拢
HAVING 筛选后再 筛选 SELECT CLASS,SUM(TOTAL_SCORES) FROM student_score GROUP BY CLASS HAVING SUM(TOTAL_SCO ...
- asp.net mvc 5 微信接入VB版 - 接入认证
微信接入官方文档是php的,网上被抄好几遍的代码是c#的,就是没vb的.今天我把这个坑填了,做vb版的接入认证. 首先是照着开发文档把微信接入的模型写好.在Models文件夹新建一个Model Pub ...
- 解决异常System.Runtime.Interopservices.COMException RequestLock问题
工具——导入导出设置,重置调试设置就可以了,这是调试文件的异常
- js实现ctrl+v粘贴并上传图片
前端页面: <textarea class="scroll" id="text" placeholder="在此输入...">& ...
- 记忆化搜索 || POJ 1088 滑雪
从任意一点可以往上下左右比它小的数那里走,问最远长度是多少 *解法:每一点dfs搜索一遍 记忆化搜索:http://blog.csdn.net/acmer_sly/article/details/53 ...
- 微信小程序入口场景的问题整理与相关解决方案
前言 最近一段时间都在做小程序. 虽然是第二次开发小程序,但是上次做小程序已经是一年前的事了,所以最终还是被坑得死去活来. 这次是从零开始开发一个小程序,其实除了一些莫名其妙的兼容性问题,大多数坑点都 ...
- MySQL字符集设定与查询
一.字符集设定 MySQL数据库允许对服务器.数据库.数据表.数据列级别的字符集作出互不影响的设定. 1.对服务器字符集设定是通过配置文件中选项character-set-server 设置,如 ch ...
- 5 SQL 复杂查询
5 复杂查询 5-1 视图 究竟视图是什么呢?如果用一句话概述的话,就是“从SQL的角度来看视图就是一张表”.实际上,在SQL语句中并不需要区分哪些是表,哪些是视图. 那么视图和表到底右什么不同呢?区 ...