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
以(x, y)这个格子为中心 边长为L 的子矩阵,(不能越界)求出这个子矩阵的最大值和最小值,并将(x,y)这个格子改成(最大+最小)/2;
输出(x,y)。
二维线段树裸题。
#include <cstdio> #include <algorithm> using namespace std; #define lson o<<1, l, m #define rson o<<1|1, m+1, r const int maxn = 805; const int MAX = 0x3f3f3f3f; int T, n, q, a, b, d, x1, x2, y1, y2, ans; int mx[maxn<<2][maxn<<2], mi[maxn<<2][maxn<<2]; void up(int oo, int o) { mi[oo][o] = min(mi[oo][o<<1] , mi[oo][o<<1|1]); mx[oo][o] = max(mx[oo][o<<1] , mx[oo][o<<1|1]); } void buildx(int o, int l, int r, int oo, int ok) { if(l == r) { if(ok == -1) { scanf("%d", &mx[oo][o]); mi[oo][o] = mx[oo][o]; } else { mx[oo][o] = max(mx[oo<<1][o], mx[oo<<1|1][o]); mi[oo][o] = min(mi[oo<<1][o], mi[oo<<1|1][o]); } return; } int m = (l+r) >> 1; buildx(lson, oo, ok); buildx(rson, oo, ok); up(oo, o); } void buildy(int o, int l, int r) { if(l == r) { buildx(1, 1, n, o, -1); return; } int m = (l+r) >> 1; buildy(lson); buildy(rson); buildx(1, 1, n, o, 1); } int queryx1(int o, int l, int r, int oo) { if(x1 <= l && r <= x2) { return mx[o][oo]; } int m = (l+r) >> 1, res = -1; if(x1 <= m) res = max(res, queryx1(lson, oo)); if(m < x2) res = max(res, queryx1(rson, oo)); return res; } int queryy1(int o, int l, int r) { if(y1 <= l && r <= y2) { return queryx1(1, 1, n, o); } int m = (l+r) >> 1, res = -1; if(y1 <= m) res = max(res, queryy1(lson)); if(m < y2) res = max(res, queryy1(rson)); return res; } int queryx2(int o, int l, int r, int oo) { if(x1 <= l && r <= x2) { return mi[o][oo]; } int m = (l+r) >> 1, res = MAX; if(x1 <= m) res = min(res, queryx2(lson, oo)); if(m < x2) res = min(res, queryx2(rson, oo)); return res; } int queryy2(int o, int l, int r) { if(y1 <= l && r <= y2) { return queryx2(1, 1, n, o); } int m = (l+r) >> 1, res = MAX; if(y1 <= m) res = min(res, queryy2(lson)); if(m < y2) res = min(res, queryy2(rson)); return res; } void updatex(int o, int l, int r, int oo, int ok) { if(l == r) { if(ok == -1) { mi[oo][o] = mx[oo][o] = ans; } else { mx[oo][o] = max(mx[oo<<1][o], mx[oo<<1|1][o]); mi[oo][o] = min(mi[oo<<1][o], mi[oo<<1|1][o]); } return; } int m = (l+r) >> 1; if(b <= m) updatex(lson, oo, ok); else updatex(rson, oo, ok); up(oo, o); } void updatey(int o, int l, int r) { if(l == r) { updatex(1, 1, n, o, -1); return; } int m = (l+r) >> 1; if(a <= m) updatey(lson); else updatey(rson); updatex(1, 1, n, o, 1); } int main() { //freopen("in.txt", "r", stdin); scanf("%d", &T); for(int ca = 1; ca <= T; ca++) { printf("Case #%d:\n", ca); scanf("%d", &n); buildy(1, 1, n); scanf("%d", &q); while(q--) { scanf("%d%d%d", &a, &b, &d); d /= 2; x1 = max(1, a-d); x2 = min(n, a+d); y1 = max(1, b-d); y2 = min(n, b+d); ans = queryy1(1, 1, n) + queryy2(1, 1, n); ans /= 2; printf("%d\n", ans); updatey(1, 1, n); } } return 0; }