HDU 4819 Mosaic (二维线段树)
Mosaic
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 213 Accepted Submission(s): 50
Can you help the God of sheep?
Each test case begins with an integer n (5 < n < 800). Then the following n rows describe the picture to pixelate, where each row has n integers representing the original color values. The j-th integer in the i-th row is the color value of cell (i, j) of the picture. Color values are nonnegative integers and will not exceed 1,000,000,000 (10^9).
After the description of the picture, there is an integer Q (Q ≤ 100000 (10^5)), indicating the number of mosaics.
Then Q actions follow: the i-th row gives the i-th replacement made by the God of sheep: xi, yi, Li (1 ≤ xi, yi ≤ n, 1 ≤ Li < 10000, Li is odd). This means the God of sheep will change the color value in (xi, yi) (located at row xi and column yi) according to the Li x Li region as described above. For example, an query (2, 3, 3) means changing the color value of the cell at the second row and the third column according to region (1, 2) (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4). Notice that if the region is not entirely inside the picture, only cells that are both in the region and the picture are considered.
Note that the God of sheep will do the replacement one by one in the order given in the input.
For each action, print the new color value of the updated cell.
3
1 2 3
4 5 6
7 8 9
5
2 2 1
3 2 3
1 1 3
1 2 3
2 2 3
5
6
3
4
6
/* ***********************************************
Author :kuangbin
Created Time :2014/5/13 23:21:07
File Name :E:\2014ACM\专题学习\数据结构\二维线段树\HDU4819.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
struct Nodey
{
int l,r;
int Max,Min;
};
int locy[MAXN],locx[MAXN];
struct Nodex
{
int l,r;
Nodey sty[MAXN*];
void build(int i,int _l,int _r)
{
sty[i].l = _l;
sty[i].r = _r;
sty[i].Max = -INF;
sty[i].Min = INF;
if(_l == _r)
{
locy[_l] = i;
return;
}
int mid = (_l + _r)/;
build(i<<,_l,mid);
build((i<<)|,mid+,_r);
}
int queryMin(int i,int _l,int _r)
{
if(sty[i].l == _l && sty[i].r == _r)
return sty[i].Min;
int mid = (sty[i].l + sty[i].r)/;
if(_r <= mid)return queryMin(i<<,_l,_r);
else if(_l > mid)return queryMin((i<<)|,_l,_r);
else return min(queryMin(i<<,_l,mid),queryMin((i<<)|,mid+,_r));
}
int queryMax(int i,int _l,int _r)
{
if(sty[i].l == _l && sty[i].r == _r)
return sty[i].Max;
int mid = (sty[i].l + sty[i].r)/;
if(_r <= mid)return queryMax(i<<,_l,_r);
else if(_l > mid)return queryMax((i<<)|,_l,_r);
else return max(queryMax(i<<,_l,mid),queryMax((i<<)|,mid+,_r));
}
}stx[MAXN*];
int n;
void build(int i,int l,int r)
{
stx[i].l = l;
stx[i].r = r;
stx[i].build(,,n);
if(l == r)
{
locx[l] = i;
return;
}
int mid = (l+r)/;
build(i<<,l,mid);
build((i<<)|,mid+,r);
}
//修改值
void Modify(int x,int y,int val)
{
int tx = locx[x];
int ty = locy[y];
stx[tx].sty[ty].Min = stx[tx].sty[ty].Max = val;
for(int i = tx;i;i >>= )
for(int j = ty;j;j >>= )
{
if(i == tx && j == ty)continue;
if(j == ty)
{
stx[i].sty[j].Min = min(stx[i<<].sty[j].Min,stx[(i<<)|].sty[j].Min);
stx[i].sty[j].Max = max(stx[i<<].sty[j].Max,stx[(i<<)|].sty[j].Max);
}
else
{
stx[i].sty[j].Min = min(stx[i].sty[j<<].Min,stx[i].sty[(j<<)|].Min);
stx[i].sty[j].Max = max(stx[i].sty[j<<].Max,stx[i].sty[(j<<)|].Max);
}
}
}
int queryMin(int i,int x1,int x2,int y1,int y2)
{
if(stx[i].l == x1 && stx[i].r == x2)
return stx[i].queryMin(,y1,y2);
int mid = (stx[i].l + stx[i].r)/;
if(x2 <= mid)return queryMin(i<<,x1,x2,y1,y2);
else if(x1 > mid)return queryMin((i<<)|,x1,x2,y1,y2);
else return min(queryMin(i<<,x1,mid,y1,y2),queryMin((i<<)|,mid+,x2,y1,y2));
}
int queryMax(int i,int x1,int x2,int y1,int y2)
{
if(stx[i].l == x1 && stx[i].r == x2)
return stx[i].queryMax(,y1,y2);
int mid = (stx[i].l + stx[i].r)/;
if(x2 <= mid)return queryMax(i<<,x1,x2,y1,y2);
else if(x1 > mid)return queryMax((i<<)|,x1,x2,y1,y2);
else return max(queryMax(i<<,x1,mid,y1,y2),queryMax((i<<)|,mid+,x2,y1,y2));
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
int iCase = ;
while(T--)
{
iCase++;
printf("Case #%d:\n",iCase);
scanf("%d",&n);
build(,,n);
for(int i = ;i <= n;i++)
for(int j = ;j <= n;j++)
{
int a;
scanf("%d",&a);
Modify(i,j,a);
}
int q;
int x,y,L;
scanf("%d",&q);
while(q--)
{
scanf("%d%d%d",&x,&y,&L);
int x1 = max(x - L/,);
int x2 = min(x + L/,n);
int y1 = max(y - L/,);
int y2 = min(y + L/,n);
int Max = queryMax(,x1,x2,y1,y2);
int Min = queryMin(,x1,x2,y1,y2);
int t = (Max+Min)/;
printf("%d\n",t);
Modify(x,y,t);
}
}
return ;
}
HDU 4819 Mosaic (二维线段树)的更多相关文章
- HDU 4819 Mosaic 二维线段树
Mosaic Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 4819 Mosaic --二维线段树(树套树)
题意: 给一个矩阵,每次查询一个子矩阵内的最大最小值,然后更新子矩阵中心点为(Max+Min)/2. 解法: 由于是矩阵,且要求区间最大最小和更新单点,很容易想到二维的线段树,可是因为之前没写过二维的 ...
- UVALive 6709 - Mosaic 二维线段树
题目链接 给一个n*n的方格, 每个方格有值. 每次询问, 给出三个数x, y, l, 求出以x, y为中心的边长为l的正方形内的最大值与最小值, 输出(maxx+minn)/2, 并将x, y这个格 ...
- HDU 4819 Mosaic(13年长春现场 二维线段树)
HDU 4819 Mosaic 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4819 题意:给定一个n*n的矩阵,每次给定一个子矩阵区域(x,y,l) ...
- 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 ...
- HDU 4819 二维线段树
13年长春现场赛的G题,赤裸裸的二维线段树,单点更新,区间查询 不过我是第一次写二维的,一开始写T了,原因是我没有好好利用行段,说白一点,还是相当于枚举行,然后对列进行线段树,那要你写二维线段树干嘛 ...
- HDU 1823 Luck and Love(二维线段树)
之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...
随机推荐
- Bank,我只是来完成作业的
写这个Bank我需要有:开户,取款,存款,转账,查询余额,退出功能. 这样我需要有两个类:Bank,User.一个Main入口. 先看这个User,他定义了各个需要的属性(字段)和字段的属性(虽然在这 ...
- Codeforces Round #341 Div.2 A. Wet Shark and Odd and Even
题意是得到最大的偶数和 解决办法很简单 排个序 取和 如果是奇数就减去最小的奇数 #include <cstdio> #include <cmath> #include < ...
- Windows下Faster-RCNN的使用
上一篇随笔中包含了关于faster rcnn的介绍. 安装与使用 1.下载Faster R-CNN源码(https://github.com/ShaoqingRen/faster_rcnn)2.安装 ...
- Sedgewick的红黑树
红黑树一直是数据结构中的难点,大部分关于算法与数据结构的学习资料(包括<算法导论>)对于这部分的讲解都是上来就下定义,告诉我们红黑树这个性质那个性质,插入删除要注意1234点,但是基本没有 ...
- Install Qt creator
download qt for linux yum install dialog move download qt file(qt-opensource-linux-x64-5.6.0.run) fr ...
- jQuery入门级part.1
jquery的选择器: 基本选择器: #id 根据id的属性值来获取元素 TagName 根据标签名来获取元素 selector1,selector2 匹配列表 ...
- WebApi:自定义筛选器
最近在项目中有这样一个需求,记录每次Api访问的调用时间,运行时间,传入数据,返回数据等信息. 第一反应就是添加一个类,用来实现相应的功能,然后在方法的代码中添加,但是这样的话,需要修改所有的方法的代 ...
- Asp.net MVC 之过滤器
整理一下MVC中的几种过滤器,以及每种过滤器是干什么用的 四种过滤器 1.AuthorizationFilter(授权过滤器) 2.ActionFilter(方法过滤器) 3.ResultFilter ...
- 黄聪:jquery 校验中国身份证号码
大陆18位身份证(第二代身份证) 身份号码是一组具有特征组合码,由十七位数字本体码和一位校验码组成. 排列顺序从左至右依次为:六位数字地区码,八位数字生日码,三位数字顺序码和一位数字校验码. 校验方法 ...
- 封装实现一个自己的tabbar
实现效果: