HDOJ 4819 Mosaic

裸的二维线段树。。。。

Mosaic

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)

Total Submission(s): 494    Accepted Submission(s): 177

Problem Description

The God of sheep decides to pixelate some pictures (i.e., change them into pictures with mosaic). Here‘s how he is gonna make it: for each picture, he divides the picture into n x n cells, where each cell is assigned a color value. Then he chooses a cell, and
checks the color values in the L x L region whose center is at this specific cell. Assuming the maximum and minimum color values in the region is A and B respectively, he will replace the color value in the chosen cell with floor((A + B) / 2).

Can you help the God of sheep?

Input

The first line contains an integer T (T ≤ 5) indicating the number of test cases. Then T test cases follow.

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.

Output

For each test case, print a line "Case #t:"(without quotes, t means the index of the test case) at the beginning.

For each action, print the new color value of the updated cell.

Sample Input

1
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

Sample Output

Case #1:
5
6
3
4
6

Source

2013 Asia Regional Changchun

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

const int maxn=1000;
const int INF=(1<<30);

int xL,xR,yL,yR,Value;
int Max[maxn<<2][maxn<<2],Min[maxn<<2][maxn<<2];
int n,mat[maxn][maxn];
int minv,maxv;

void pushup(int xrt,int rt)
{
	Max[xrt][rt]=max(Max[xrt][rt<<1],Max[xrt][rt<<1|1]);
	Min[xrt][rt]=min(Min[xrt][rt<<1],Min[xrt][rt<<1|1]);
}

void buildY(int xrt,int x,int l,int r,int rt)
{
	if(l==r)
	{
		if(x==-1)
		{
			Max[xrt][rt]=max(Max[xrt<<1][rt],Max[xrt<<1|1][rt]);
			Min[xrt][rt]=min(Min[xrt<<1][rt],Min[xrt<<1|1][rt]);
		}
		else
		{
			Max[xrt][rt]=Min[xrt][rt]=mat[x][l];
		}
		return ;
	}
	int m=(l+r)/2;
	buildY(xrt,x,lson); buildY(xrt,x,rson);
	pushup(xrt,rt);
}

void buildX(int l,int r,int rt)
{
	if(l==r)
	{
		buildY(rt,l,1,n,1);
		return ;
	}
	int m=(l+r)/2;
	buildX(lson); buildX(rson);
	buildY(rt,-1,1,n,1);
}

void updateY(int xrt,int x,int l,int r,int rt)
{
	if(l==r)
	{
		if(x==-1)
		{
			Max[xrt][rt]=max(Max[xrt<<1][rt],Max[xrt<<1|1][rt]);
			Min[xrt][rt]=min(Min[xrt<<1][rt],Min[xrt<<1|1][rt]);
		}
		else
		{
			Max[xrt][rt]=Min[xrt][rt]=Value;
		}
		return ;
	}
	int m=(l+r)/2;
	if(yL<=m) updateY(xrt,x,lson);
	else updateY(xrt,x,rson);
	pushup(xrt,rt);
}

void updateX(int l,int r,int rt)
{
	if(l==r)
	{
		updateY(rt,l,1,n,1);
		return ;
	}
	int m=(l+r)/2;
	if(xL<=m) updateX(lson);
	else updateX(rson);
	updateY(rt,-1,1,n,1);
}

void queryY(int xrt,int l,int r,int rt)
{
	if(yL<=l&&r<=yR)
	{
		minv=min(minv,Min[xrt][rt]);
		maxv=max(maxv,Max[xrt][rt]);
		return ;
	}
	int m=(l+r)/2;
	if(yL<=m) queryY(xrt,lson);
	if(yR>m) queryY(xrt,rson);
}

void queryX(int l,int r,int rt)
{
	if(xL<=l&&r<=xR)
	{
		queryY(rt,1,n,1);
		return ;
	}
	int m=(l+r)/2;
	if(xL<=m) queryX(lson);
	if(xR>m) queryX(rson);
}

int main()
{
	int T_T,cas=1;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				scanf("%d",&mat[i][j]);
		buildX(1,n,1);
		int q;
		scanf("%d",&q);
		printf("Case #%d:\n",cas++);
		while(q--)
		{
			int x,y,l;
			scanf("%d%d%d",&x,&y,&l);
			l=l/2;
			xL=max(1,x-l);xR=min(n,x+l);
			yL=max(1,y-l);yR=min(n,y+l);
			minv=INF; maxv=-INF;
			queryX(1,n,1);
			Value=(minv+maxv)/2;
			printf("%d\n",Value);
			xL=x;yL=y;
			updateX(1,n,1);
		}
	}
	return 0;
}
时间: 2024-10-29 21:00:59

HDOJ 4819 Mosaic的相关文章

HDU 4819 Mosaic

题意: 一个矩形内每个格子都有一个值  现在有q个操作  每个操作给出坐标(x,y)和长度L  每次操作输出以(x,y)为中心的边长为L的矩形内的最大值和最小值之和的一半  并将这个值更新到(x,y)坐标上 思路: 区间查询最大最小值  单点更新  明显是线段树的特征  不过这里是二维的线段树  我用的是树套树的写法 我对二维线段树的理解:(个人理解不一定正确) 初始化麻烦  相当于做n*n次单点更新 树套树操作的思维就先找到满足第一位的区间  这个区间对应几棵树  然后在这几棵树(即第二维)里

HDU 4819 Mosaic D区段树

pid=4819">点击打开链接 Mosaic Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 657    Accepted Submission(s): 248 Problem Description The God of sheep decides to pixelate some pictures (i.e., ch

HDU 4819 Mosaic (二维线段树)

Problem Description The God of sheep decides to pixelate some pictures (i.e., change them into pictures with mosaic). Here's how he is gonna make it: for each picture, he divides the picture into n x n cells, where each cell is assigned a color value

HDU 4819 Mosaic --二维线段树(树套树)

题意: 给一个矩阵,每次查询一个子矩阵内的最大最小值,然后更新子矩阵中心点为(Max+Min)/2. 解法: 由于是矩阵,且要求区间最大最小和更新单点,很容易想到二维的线段树,可是因为之前没写过二维的线段树,所以没跳出来.后来熟悉了一下,原来很多细节地方都没有考虑到. 这里build,update,query都分为两个函数,第一个为Y轴的(sub_update),第二个为X轴的(update),不仅每个sub_update或sub_build后面要加Y轴的pushup函数,而且每个update或

HDU 4819 Mosaic 【二维线段树】

题目大意:给你一个n*n的矩阵,每次找到一个点(x,y)周围l*l的子矩阵中的最大值a和最小值b,将(x,y)更新为(a+b)/2 思路:裸的二维线段树 #include<iostream>#include<cstdio>#include <math.h>#include<algorithm>#include<string.h>#include<queue>#define MOD 10000007#define maxn 3500#d

#树套树,二维线段树#HDU 4819 Mosaic

题目 多组数据,给定一个\(n*n\)的矩阵(\(n\leq 80,a_{i,j}\leq 10^9\)) 多组询问一个以\((x,y)\)为中心,边长为\(L\)的子矩阵最大值\(mx\)和最小值\(mn\), 并将\((x,y)\)这一个位置修改为\(\lfloor\frac{mn+mx}{2}\rfloor\),每次询问输出修改后的\((x,y)\) 分析 二维线段树裸题,反正之前也没敲过, 其实和一维线段树相近,找到\(lx\sim rx\)的下标 再按照一维线段树的方式修改最大最小值就

hdu 4819 二维线段树模板

/* HDU 4819 Mosaic 题意:查询某个矩形内的最大最小值, 修改矩形内某点的值为该矩形(Mi+MA)/2; 二维线段树模板: 区间最值,单点更新. */ #include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 1010; int N, Q; struct Nodey { int l, r; int Max, Min; }; int locx[MAXN], l

HDU 4819:Mosaic(线段树套线段树)

http://acm.hdu.edu.cn/showproblem.php?pid=4819 题意:给出一个矩阵,然后q个询问,每个询问有a,b,c,代表(a,b)这个点上下左右c/2的矩形区域内的(最大值+最小值)/2是多少,并且将(a,b)的值替换成这个答案. 思路:很久以前被暴力跑过去的一道题,今天怎么交也过不去...果然是人品爆发了. 学了一下树套树,一开始觉得挺容易理解,但是后面PushUp那里挺难懂的(对我来说). 我的理解: 对于每个线段树的结点开一棵线段树,即tree[x][y]

【HDU 4819】Mosaic 二维线段树模板

二维线段树的模板题,和一维一样的思路,更新的时候注意一下细节. 存模板: /* 二维线段树模板整理 */ #include<cstdio> #include<algorithm> using namespace std; #define lson (pos<<1) #define rson (pos<<1|1) const int maxn = 805; const int INF = (1 << 30); int n; int posX[max