Mosaic
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 2293 Accepted Submission(s): 1009
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
#include <bits/stdc++.h>
using namespace std;
const int maxn = 855;
const int INF = 1 << 30;
struct tree{
int mx[maxn << 2], mi[maxn << 2];
}c[maxn << 2];
int minv, maxv, n, x;
void buildy(int ox, int oy, int l, int r){
c[ox].mx[oy] = -INF;
c[ox].mi[oy] = INF;
if(l == r) return;
int mid = l + r >> 1;
buildy(ox, oy << 1, l, mid);
buildy(ox, oy << 1 | 1, mid + 1, r);
}
void buildx(int o, int l, int r){
buildy(o, 1, 1, n);
if(l == r) return;
int mid = l + r >> 1;
buildx(o << 1, l, mid);
buildx(o << 1 | 1, mid + 1, r);
}
void addy(int ox, int oy, int l, int r, int id, int v){
if(l == r){
c[ox].mx[oy] = c[ox].mi[oy] = v; return;
}
int mid = l + r >> 1;
if(id <= mid) addy(ox, oy << 1, l, mid, id, v);
else addy(ox, oy << 1 | 1, mid + 1, r, id, v);
c[ox].mx[oy] = max(c[ox].mx[oy << 1], c[ox].mx[oy << 1 | 1]);
c[ox].mi[oy] = min(c[ox].mi[oy << 1], c[ox].mi[oy << 1 | 1]);
}
void pushupY(int ox, int oy, int l, int r, int id){
c[ox].mx[oy] = max(c[ox << 1].mx[oy], c[ox << 1 | 1].mx[oy]);
c[ox].mi[oy] = min(c[ox << 1].mi[oy], c[ox << 1 | 1].mi[oy]);
if(l == r) return;
int mid = l + r >> 1;
if(id <= mid) pushupY(ox, oy << 1, l, mid, id);
else pushupY(ox, oy << 1 | 1, mid + 1, r, id);
}
void addx(int o, int l, int r, int x, int y, int v){
if(l == r){
addy(o, 1, 1, n, y, v); return;
}
int mid = l + r >> 1;
if(x <= mid) addx(o << 1, l, mid, x, y, v);
else addx(o << 1 | 1, mid + 1, r, x, y, v);
pushupY(o, 1, 1, n, y);
}
void queryy(int ox, int oy, int l, int r, int L, int R){
if(l >= L && r <= R){
minv = min(minv, c[ox].mi[oy]);
maxv = max(maxv, c[ox].mx[oy]);
return;
}
int mid = l + r >> 1;
if(mid >= L) queryy(ox, oy << 1, l, mid, L, R);
if(mid < R) queryy(ox, oy << 1 | 1, mid + 1, r, L, R);
}
void queryx(int o, int l, int r, int L, int R, int yl, int yr){
if(l >= L && r <= R){
queryy(o, 1, 1, n, yl, yr);
return;
}
int mid = l + r >> 1;
if(mid >= L) queryx(o << 1, l, mid, L, R, yl, yr);
if(mid < R) queryx(o << 1 | 1, mid + 1, r, L, R, yl, yr);
}
int main(){
int T, casenum = 1;
scanf("%d", &T);
while(T--){
printf("Case #%d:\n", casenum++);
scanf("%d", &n);
buildx(1, 1, n);
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
scanf("%d", &x);
addx(1, 1, n, i, j, x);
}
}
int m, l, r, v, x1, x2, y1, y2;
scanf("%d", &m);
while(m--){
scanf("%d %d %d", &l, &r, &v);
minv = INF; maxv = -INF;
x1 = max(1, l - v / 2);
x2 = min(n, l + v / 2);
y1 = max(1, r - v / 2);
y2 = min(n, r + v / 2);
queryx(1, 1, n, x1, x2, y1, y2);
printf("%d\n", (minv + maxv) / 2);
addx(1, 1, n, l, r, (minv + maxv) / 2);
}
}
return 0;
}
/*
题意:
800*800的矩阵,1e5次操作,每次query一个子块的最大值和最小值,然后修改某个点的值。
思路:
二位线段树裸题。就是将x映射到一棵线段树上,然后线段树上的每个结点在维护一棵线段树,
用于维护y轴上的信息。这样就便于理解了。
*/