hdu4819Mosaic

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

Total Submission(s): 925    Accepted Submission(s): 408

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<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
const int N=810;
int ir[N],ic[N];
struct Nr
{
	int l,r;
	struct Nc
	{
		int l,r,mx,mn;
	}nc[N<<2];
	void build(int l,int r,int k)
	{
		nc[k].l=l;
		nc[k].r=r;
		nc[k].mn=INT_MAX;
		nc[k].mx=INT_MIN;
		if(l==r)
		{
			ic[l]=k;
			return;
		}
		int m=(l+r)>>1;
		build(l,m,k<<1);
		build(m+1,r,k<<1|1);
	}
	int seek(int l,int r,int k,bool flag)
	{
		if(nc[k].l==l&&nc[k].r==r)
			return flag?nc[k].mx:nc[k].mn;
		int m=nc[k].l+nc[k].r>>1;
		if(r<=m)
			return seek(l,r,k<<1,flag);
		if(l>m)
			return seek(l,r,k<<1|1,flag);
		return flag?max(seek(l,m,k<<1,flag),seek(m+1,r,k<<1|1,flag)):
			min(seek(l,m,k<<1,flag),seek(m+1,r,k<<1|1,flag));
	}
}nr[N<<2];
void update(int r,int c,int val)
{
	nr[ir[r]].nc[ic[c]].mn=nr[ir[r]].nc[ic[c]].mx=val;
	for(int i=ir[r];i>0;i>>=1)
		for(int j=ic[c];j>0;j>>=1)
		{
			if(i==ir[r]&&j==ic[c])
				continue;
			if(j==ic[c])//行线段树更新叶子
			{
				nr[i].nc[j].mx=max(nr[i<<1].nc[j].mx,nr[i<<1|1].nc[j].mx);
				nr[i].nc[j].mn=min(nr[i<<1].nc[j].mn,nr[i<<1|1].nc[j].mn);
			}
			else//更新列线段树
			{
				nr[i].nc[j].mx=max(nr[i].nc[j<<1].mx,nr[i].nc[j<<1|1].mx);
				nr[i].nc[j].mn=min(nr[i].nc[j<<1].mn,nr[i].nc[j<<1|1].mn);
			}
		}
}
int seek(int l,int r,int l1,int r1,int k,bool flag)
{
	if(nr[k].l==l&&nr[k].r==r)
		return nr[k].seek(l1,r1,1,flag);
	int m=nr[k].l+nr[k].r>>1;
	if(r<=m)
		return seek(l,r,l1,r1,k<<1,flag);
	if(l>m)
		return seek(l,r,l1,r1,k<<1|1,flag);
	return flag?max(seek(l,m,l1,r1,k<<1,flag),seek(m+1,r,l1,r1,k<<1|1,flag)):
		min(seek(l,m,l1,r1,k<<1,flag),seek(m+1,r,l1,r1,k<<1|1,flag));
}
void build(int l,int r,int n,int k)
{
	nr[k].l=l;
	nr[k].r=r;
	nr[k].build(1,n,1);
	if(l==r)
	{
		ir[l]=k;
		return;
	}
	int m=(l+r)>>1;
	build(l,m,n,k<<1);
	build(m+1,r,n,k<<1|1);
}
int main()
{
	int T;
	scanf("%d",&T);
	for(int cs=1;cs<=T;cs++)
	{
		printf("Case #%d:\n",cs);
		int n;
		scanf("%d",&n);
		build(1,n,n,1);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
			{
				int t;
				scanf("%d",&t);
				update(i,j,t);
			}
		int q;
		scanf("%d",&q);
		while(q--)
		{
			int r,c,L;
			scanf("%d%d%d",&r,&c,&L);
            int r1=max(r-L/2,1);
            int r2=min(r+L/2,n);
            int c1=max(c-L/2,1);
            int c2=min(c+L/2,n);
            int mx=seek(r1,r2,c1,c2,1,1);
            int mn=seek(r1,r2,c1,c2,1,0);
            int t=(mx+mn)>>1;
            printf("%d\n",t);
            update(r,c,t);
		}
	}
}
时间: 2024-11-08 18:18:30

hdu4819Mosaic的相关文章

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