首页 > 代码库 > HDU 4819 Mosaic(二维线段树)
HDU 4819 Mosaic(二维线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=4819
Mosaic
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 401 Accepted Submission(s): 140
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?
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.
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.
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
题意:
给出一个图片,分成N×N个单元格,有M次操作,每次操作将(x,y)的值变为以(x,y) 为中心L(L为奇数)为边长的区域内的最小值和最大值的均值(floor((maximum+minimum)/2)),并输出该值。
分析:
明显的二维线段树的单点更新和区间查询,维护最值。
更新肯定是先在二维内找到叶子节点的那棵线段树,然后再在这棵树上更新,这部分很简单,就想成一维的写。但是这毕竟是二维线段树,二维的部分也需要维护,当然这部分比较麻烦,我们先想一想一维的:一维的节点维护的是值,我们只要根据它的左右儿子的信息维护(pushup)就行。二维的节点维护的是树(一维的线段树),类比一下,我们也需要根据它的左右儿子来维护,但是我们并不能简单地通过pushup来维护它的信息(因为一个节点对应的是一棵一维线段树,而不是一个线段),我们要进入这棵一维线段树中去维护各个点(线段)的信息。
维护好过后查询就方便了,简单地降维即可。
具体请见代码(so ugly...如何优雅地拍出二维线段树呢?):
/* * * Author : fcbruce * * Date : 2014-08-15 08:09:04 * */ #include <cstdio> #include <iostream> #include <cstdlib> #include <algorithm> #include <ctime> #include <cctype> #include <cmath> #include <string> #include <cstring> #include <stack> #include <queue> #include <list> #include <vector> #include <map> #include <set> #define sqr(x) ((x)*(x)) #define LL long long #define itn int #define INF 0x3f3f3f3f #define PI 3.1415926535897932384626 #define eps 1e-10 #define maxm #define maxn 808 using namespace std; int n; int minv[maxn<<2][maxn<<2],maxv[maxn<<2][maxn<<2]; inline int ReadInt() { int flag=0; char ch = getchar(); int data = http://www.mamicode.com/0;>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。