Mosaic
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 1703 Accepted Submission(s): 746
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
题意:给你一个n*n的矩阵, 每个点是一个数字, Q个操作,每次选择一个子矩阵, 把中心元素替换成子矩阵中最大值和最小值之和的二分之一。
思路: 经典的二维线段树模型, 二维线段树也是树套树的一种, 就是一棵树的每个结点上再维护一棵树, 需要注意的是, 对于第一棵树, 每个结点值PushUp的时候, 因为要更新的是一棵树, 所以不能简单的像线段树一样更新, 而是应该调用更新第二颗树。 相应的, 结点值的更新也略有不同:
二维线段树其实就是行线段树套列线段树, 那么行线段树就要用行下标更新行, 用列下标更新列。
//
// main.cpp
// 160929
//
// Created by 刘哲 on 17/4/9.
// Copyright © 2016年 my_code. All rights reserved.
//
//#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <list>
#include <bitset>
#include <stack>
#define lowbit(x) (x&-x)
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=810;
int num1[maxn][maxn],num[maxn*4][maxn*4],Max[maxn*4][maxn*4],Min[maxn*4][maxn*4];
int n,max1,min1;
void pushup(int nodex,int nodey)
{
Max[nodex][nodey]=max(Max[nodex][nodey<<1],Max[nodex][nodey<<1|1]);
Min[nodex][nodey]=min(Min[nodex][nodey<<1],Min[nodex][nodey<<1|1]);
}
void buildtreey(int le,int ri,int x,int nodex,int nodey)
{
if(le==ri)
{
if(x!=-1)
Max[nodex][nodey]=Min[nodex][nodey]=num1[x][le];
else
{
Max[nodex][nodey]=max(Max[nodex<<1][nodey],Max[nodex<<1|1][nodey]);
Min[nodex][nodey]=min(Min[nodex<<1][nodey],Min[nodex<<1|1][nodey]);
}
return ;
}
int t=(le+ri)>>1;
buildtreey(le,t,x,nodex,nodey<<1);
buildtreey(t+1,ri,x,nodex,nodey<<1|1);
pushup(nodex,nodey);
}
void buildtreex(int le,int ri,int nodex)
{
if(le==ri)
{
buildtreey(1,n,le,nodex,1);
return ;
}
int t=(le+ri)>>1;
buildtreex(le,t,nodex<<1);
buildtreex(t+1,ri,nodex<<1|1);
buildtreey(1,n,-1,nodex,1);
}
void updatey(int le,int ri,int b,int add,int nodex,int nodey,int x)
{
if(le==ri)
{
if(x!=-1)
Max[nodex][nodey]=Min[nodex][nodey]=add;
else
{
Max[nodex][nodey]=max(Max[nodex<<1][nodey],Max[nodex<<1|1][nodey]);
Min[nodex][nodey]=min(Min[nodex<<1][nodey],Min[nodex<<1|1][nodey]);
}
return ;
}
int t=(le+ri)>>1;
if(b<=t) updatey(le,t,b,add,nodex,nodey<<1,x);
else updatey(t+1,ri,b,add,nodex,nodey<<1|1,x);
pushup(nodex,nodey);
}
void updatex(int a,int b,int add,int le,int ri,int nodex)
{
if(le==ri)
{
updatey(1,n,b,add,nodex,1,le);
return ;
}
int t=(le+ri)>>1;
if(a<=t) updatex(a,b,add,le,t,nodex<<1);
else updatex(a,b,add,t+1,ri,nodex<<1|1);
updatey(1,n,b,add,nodex,1,-1);
}
void queryy(int b,int d,int le,int ri,int nodex,int nodey)
{
if(b<=le&&ri<=d){
max1=max(max1,Max[nodex][nodey]);
min1=min(min1,Min[nodex][nodey]);
return ;
}
int t=(le+ri)>>1;
if(b<=t) queryy(b,d,le,t,nodex,nodey<<1);
if(d>t) queryy(b,d,t+1,ri,nodex,nodey<<1|1);
}
void queryx(int a,int b,int c,int d,int le,int ri,int nodex)
{
if(a<=le&&ri<=c)
{
queryy(b,d,1,n,nodex,1);
return ;
}
int t=(le+ri)>>1;
if(a<=t) queryx(a,b,c,d,le,t,nodex<<1);
if(c>t) queryx(a,b,c,d,t+1,ri,nodex<<1|1);
}
int main()
{
int T,m,a,b,c,t=1;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&num1[i][j]);
int xL,xR,yL,yR;
buildtreex(1,n,1);
scanf("%d",&m);
printf("Case #%d:\n",t++);
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
max1=-inf;min1=inf;
c=(c+1)/2;
xL=max(1,a-c+1),xR=min(n,a+c-1);
yL=max(1,b-c+1),yR=min(n,b+c-1);
queryx(xL,yL,xR,yR,1,n,1);
int ans=(max1+min1)>>1;
printf("%d\n",ans);
updatex(a,b,ans,1,n,1);
}
}
return 0;
}