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 ...
随机推荐
- nginx 访问localhost老是下载文件不能打开网页什么情况?
nginx打开网页直接下载文件的问题 nginx sites-available文件里的default已经修改过root 路径了. 但是访问localhost的时候总是直接下载网页而不是打开网址 很奇 ...
- 移动端超级好用的reset.css(只做参考哦具体以你们实际项目需求为准)
html { color: #333; /*规定主色调,依据业务场景(非必须)*/ background: #F6F6F6; /*规定主背景,依据业务场景(非必须)*/ overflow-y: aut ...
- Charles Petzold 编程界大师,世界顶级技术作家 《CODE》值得阅读
<CODE>The Hidden Language of Computer Hardware and Software 从书内容的第一页开始就惊叹于作者的耐心和责任心 整本书以两个隔街对窗 ...
- 在Oracle用SQL处理以 System.currentTimeMillis
有時為了系統的需求會紀錄到毫秒(Millisecond),我們會接將得到的值寫入db,但是如果要用SQL 做時間範圍的搜尋,有以下做法( systemdate欄位存放System.currentTim ...
- 性能测试,如何得到大量token,并保存在本地文件中
需求:性能测试需要大量的token,模拟登陆 设计思路: 1.使用语言:python +request+正则匹配+写入本地 2.jmeter+函数助手+正则或者json/yaml+后置处理器beans ...
- java list去重操作实现方式
Java中的List是可以包含重复元素的(hash code 和equals),接下来将介绍两种方式实现java list去重操作,感兴趣的朋友可以参考下 Java中的List是可以包含重复元素的 ...
- [LOJ] 分块九题 6
单点插入,单点查询. 优化了的链表. 链表老写错,干脆用vector,也不算慢. 注意链表退化的问题,及时(比如操作根号n次)就重新建块,实测速度可以提高一倍,这还是数据随机的情况,若涉及大量同一位置 ...
- <Spring Cloud>入门二 Eureka Client
1.搭建一个通用工程 1.1 pom 文件 <?xml version="1.0" encoding="UTF-8"?> <project x ...
- Oracle 11G RAC 修改IP
实验环境 类别 修改前 修改后 PUBLIC 172.18.4.182 rac1 192.168.56.10 rac1 172.18.4.184 rac2 192.168.56.20 rac2 PRI ...
- 【JavaEE-面试总结】(未完,待续···)
目录: 一.Java基础 二.JavaEE基础 三.JavaEE进阶 四.数据库 五.数据结构&算法 六.高级(服务器) 一.Java基础 1.1 面向对象(封装.继承.多态) 访问权限修饰符 ...