Mosaic 二维线段树板子(真

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43880084/article/details/102678760

LINK

Mosaic
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 2904 Accepted Submission(s): 1272

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

Recommend
liuyiding | We have carefully selected several similar problems for you: 6742 6741 6740 6739 6738


题意就是找出x-l/2,y-l/2 左下 —x+l/2,y+l/2右上端点的矩形中的最大最小值,然后输出平均值并且将平均值赋值给(x,y)

#include<bits/stdc++.h>
using namespace std;
const int N=801,inf=0x3f3f3f3f;
typedef long long ll;
struct node
{
	int  mi,mx;
}T[N<<2][N<<2];
int a[N][N];int n;
void up(int rt,int fa)//维护y 
{
	T[fa][rt].mi=min(T[fa][rt<<1].mi,T[fa][rt<<1|1].mi);
	T[fa][rt].mx=max(T[fa][rt<<1].mx,T[fa][rt<<1|1].mx );
}
void buildy(int fa,int rt,int l,int r,int pd,int pos)
{
	if(l==r)
	{
		if(pd) {
			T[fa][rt].mi=T[fa][rt].mx=a[pos][l];
		}else 
		{
			//维护x 
			T[fa][rt].mi=min(T[fa<<1][rt].mi,T[fa<<1|1][rt].mi);
			T[fa][rt].mx=max(T[fa<<1][rt].mx,T[fa<<1|1][rt].mx);
		}
		return ;
	}
	int mid=(l+r)>>1;
	buildy(fa,rt<<1,l,mid,pd,pos);
	buildy(fa,rt<<1|1,mid+1,r,pd,pos);
	up(rt,fa);
}
void build(int rt,int l,int r)
{
	if(l==r)
	{
		buildy(rt,1,1,n,1,l);
		return ;
	}
	int mid=(l+r)>>1;
	build(rt<<1,l,mid);
	build(rt<<1|1,mid+1,r);
	buildy(rt,1,1,n,0,0);
}
void updatey(int fa,int rt,int l,int r,int y,int v,int pd)
{
	if(l==r)
	{
		if(pd) {
			T[fa][rt].mi=T[fa][rt].mx=v;
		}else 
		{
			T[fa][rt].mi=min(T[fa<<1][rt].mi,T[fa<<1|1][rt].mi);
			T[fa][rt].mx=max(T[fa<<1][rt].mx,T[fa<<1|1][rt].mx);
		}return ;
	}
	int mid=(l+r)>>1;
	if(mid>=y) updatey(fa,rt<<1,l,mid,y,v,pd);
	if(y>mid) updatey(fa,rt<<1|1,mid+1,r,y,v,pd);
	up(rt,fa);
}
void update(int rt,int l,int r,int x,int y,int v)
{
	if(l==r)
	{
		updatey(rt,1,1,n,y,v,1);
		return ;
	}
	int mid=(l+r)>>1;
	if(mid>=x) update(rt<<1,l,mid,x,y,v);
	if(x>mid) update(rt<<1|1,mid+1,r,x,y,v);
	updatey(rt,1,1,n,y,v,0);
}
void queryy(int fa,int rt,int l,int r,int L,int R,int &maxx,int& minn)
{
	if(l>=L&&r<=R) {
		maxx=max(maxx,T[fa][rt].mx);
		minn=min(minn,T[fa][rt].mi);return ;
	}
	int mid=(l+r)>>1;
	if(L<=mid) queryy(fa,rt<<1,l,mid,L,R,maxx,minn);
	if(R>mid) queryy(fa,rt<<1|1,mid+1,r,L,R,maxx,minn);
}
void query(int rt,int l,int r,int L,int R,int aa,int bb,int &maxx,int& minn)
{
	if(l>=L&&r<=R) {
		queryy(rt,1,1,n,aa,bb,maxx,minn);
		return ;
	}
	int mid=(l+r)>>1;
	if(L<=mid) query(rt<<1,l,mid,L,R,aa,bb,maxx,minn);
	if(R>mid) query(rt<<1|1,mid+1,r,L,R,aa,bb,maxx,minn);
}
int main()
{
	int tt;scanf("%d",&tt);int cas=0;
	while(tt--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++) 
			{
				scanf("%d",&a[i][j]); 
			}
		}
		build(1,1,n);
		int m;scanf("%d",&m);
		printf("Case #%d:\n",++cas);
		while(m--)
		{
			int x,y,l;scanf("%d%d%d",&x,&y,&l);l=l>>1;
			int maxx=-1,minn=inf;int a1=x-l,a2=x+l,b1=y-l,b2=y+l;
			a1=max(a1,1);a2=min(n,a2);b1=max(1,b1);b2=min(n,b2);
			query(1,1,n,a1,a2,b1,b2,maxx,minn);
			int ans=(maxx+minn)/2;
			printf("%d\n",ans);
			update(1,1,n,x,y,ans);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43880084/article/details/102678760