HDU4819 Mosaic (二维线段树)

Mosaic

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 2908    Accepted Submission(s): 1275


 

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

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4819

题意:给n*n的数组,q次询问,每次询问有x,y,r三个值,修改坐标(x,y)的值,并且输出修改后的值。

思路:二维线段树,意思差不多就是:一维线段树的每个节点下,又有一个线段树。(好像是这样的)

线段树的每个操作都要写两份,比如:buildx,buildy,先在x轴找到当前区间,在将当前所代表的节点传入y轴的操作。

重点!!维护线段树的时候,两个维度都要维护。

#include <iostream>
#include <stdio.h>
using namespace std;
#define INF 0x3f3f3f3f
int n;
struct node {
	int mx[800 * 3], mi[800 * 3];
} tree[800 * 3];
void buildy(int op, int i, int l, int r) {
	tree[op].mi[i] = INF;
	tree[op].mx[i] = -INF;
	if (l == r)
		return;
	int mid = (l + r) / 2;
	buildy(op, i + i, l, mid);
	buildy(op, i + i + 1, mid + 1, r);
}
void buildx(int i, int l, int r) {
	buildy(i, 1, 1, n);
	if (l == r)
		return;
	int mid = (l + r) / 2;
	buildx(i + i, l, mid);
	buildx(i + i + 1, mid + 1, r);
}
void pushup(int op,int i,int l,int r,int y)
{
	tree[op].mi[i]=min(tree[op+op].mi[i],tree[op+op+1].mi[i]);
	tree[op].mx[i]=max(tree[op+op].mx[i],tree[op+op+1].mx[i]);
	if(l==r)
	return;
	int mid=(l+r)/2;
	if(y<=mid)
	pushup(op,i+i,l,mid,y);
	else
	pushup(op,i+i+1,mid+1,r,y);
}
void addy(int op, int i, int l, int r, int y, int v) {
	if(l==r) {
		tree[op].mi[i]=v;
		tree[op].mx[i]=v;
		return;
	}
	int mid=(l+r)/2;
	if(y<=mid)
		addy(op,i+i,l,mid,y,v);
	else
		addy(op,i+i+1,mid+1,r,y,v);

	tree[op].mi[i]=min(tree[op].mi[i+i],tree[op].mi[i+i+1]);
	tree[op].mx[i]=max(tree[op].mx[i+i],tree[op].mx[i+i+1]);
}
void addx(int i, int l, int r, int x,int y,int v) {
	if (l == r) {
		addy(i,1, 1, n, y, v);
		return;
	}
	int mid = (l + r) / 2;
	if (x <= mid)
		addx(i + i, l, mid, x, y, v);
	else
		addx(i + i + 1, mid + 1, r, x, y, v);
	
	pushup(i,1,1,n,y);
}
int minv,maxv;
void queryy(int op,int i,int l,int r,int y1,int y2)
{
	if(y1<=l&&r<=y2)
	{
		minv=min(tree[op].mi[i],minv);
		maxv=max(tree[op].mx[i],maxv);
		return;
	}
	int mid=(l+r)/2;
	if(y1<=mid)
	queryy(op,i+i,l,mid,y1,y2);
	if(mid<y2)
	queryy(op,i+i+1,mid+1,r,y1,y2); 
}
void queryx(int i,int l,int r,int x1,int x2,int y1,int y2)
{
	if(x1<=l&&r<=x2)
	{
		queryy(i,1,1,n,y1,y2);
		return;
	}
	int mid=(l+r)/2;
	if(x1<=mid)
	queryx(i+i,l,mid,x1,x2,y1,y2);
	if(mid<x2)
	queryx(i+i+1,mid+1,r,x1,x2,y1,y2);
	
}
int main() {
	int t,v,m,x,y,r;
	int num=1;
	scanf("%d", &t);
	while (t--) {
		printf("Case #%d:\n",num++);
		scanf("%d", &n);
		buildx(1, 1, n);
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				scanf("%d", &v);
				addx(1, 1, n, i, j, v);
			}
		}
		scanf("%d",&m);
		while(m--)
		{
			minv=INF;
			maxv=-INF;
			scanf("%d%d%d",&x,&y,&r);
			int x1=max(1,x-r/2);
			int x2=min(n,x+r/2);
			int y1=max(1,y-r/2);
			int y2=min(n,y+r/2);
			queryx(1,1,n,x1,x2,y1,y2);
			printf("%d\n",(maxv+minv)/2);
			addx(1,1,n,x,y,(maxv+minv)/2);
		}
	}
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值