您的位置:首页 > 大数据 > 人工智能

Hdu4819 Mosaic 二维线段树维护区间最值+单点更新

2017-10-02 18:49 501 查看


Mosaic

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)

Total Submission(s): 2077 Accepted Submission(s): 927



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

二维线段树的板子题,查询时修改X1,Y1,X2,Y2,然后直接query_x,查询后minx和maxx均会被修改。更新也是如此,先修改X1,Y1,X2,Y2,然后把val改成需要修改的值,直接update。

就是这么无脑地用板子。。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
typedef long long ll;
using namespace std;
const long long inf = 0x3f3f3f3f;
const int maxn = 810;
int n,X1,Y1,X2,Y2,val;
int maxx, minx;
int s[maxn][maxn], MAX[maxn*4+10][maxn*4+10], MIN[maxn*4+10][maxn*4+10];
void buildy(int x, int l, int r, int rtx, int rty){
//system("pause");
//cout<<"buildy "<<l<<" "<<r<<endl;
int m;
if(l==r){
if(x!=-1){
MAX[rtx][rty] = MIN[rtx][rty] = s[x][l];
}else{
MAX[rtx][rty] = max(MAX[rtx<<1][rty], MAX[rtx<<1|1][rty]);
MIN[rtx][rty] = min(MIN[rtx<<1][rty], MIN[rtx<<1|1][rty]);
}
return;
}
m = (l+r)>>1;
buildy(x,l,m,rtx,rty<<1);
buildy(x,m+1,r,rtx,rty<<1|1);
MAX[rtx][rty] = max(MAX[rtx][rty<<1], MAX[rtx][rty<<1|1]);
MIN[rtx][rty] = min(MIN[rtx][rty<<1], MIN[rtx][rty<<1|1]);
}
void buildx(int l, int r, int rtx){
//cout<<l<<" "<<r<<" "<<rtx<<endl;
int m;
if(l==r){
buildy(l,1,n,rtx,1);
return;
}
m = (l+r)>>1;
buildx(l,m,rtx<<1);
buildx(m+1,r,rtx<<1|1);
buildy(-1,1,n,rtx,1);
}
void updatey(int x, int l, int r, int rtx, int rty){
int m;
if(l==r){
if(x!=-1){
MAX[rtx][rty] = MIN[rtx][rty] = val;
}else{
MAX[rtx][rty] = max(MAX[rtx<<1][rty], MAX[rtx<<1|1][rty]);
MIN[rtx][rty] = min(MIN[rtx<<1][rty], MIN[rtx<<1|1][rty]);
}
return;
}
m=(l+r)>>1;
if(Y1<=m){
updatey(x,l,m,rtx,rty<<1);
}
if(Y1>m){
updatey(x,m+1,r,rtx,rty<<1|1);
}
MAX[rtx][rty] = max(MAX[rtx][rty<<1],MAX[rtx][rty<<1|1]);
MIN[rtx][rty] = min(MIN[rtx][rty<<1],MIN[rtx][rty<<1|1]);
}
void updatex(int l, int r, int rtx){
int m;
if(l==r){
updatey(l,1,n,rtx,1);
return;
}
m = (l+r)>>1;
if(X1<=m){
updatex(l,m,rtx<<1);
}
if(X1>m){
updatex(m+1,r,rtx<<1|1);
}
updatey(-1,1,n,rtx,1);
}
void quert_y(int l, int r, int rtx, int rty){
int m;
if(Y1<=l&&r<=Y2){
maxx = max(maxx, MAX[rtx][rty]);
minx = min(minx, MIN[rtx][rty]);
return;
}
m = (l+r)>>1;
if(Y1<=m)
quert_y(l,m,rtx,rty<<1);
if(Y2>m)
quert_y(m+1,r,rtx,rty<<1|1);
}

void query_x(int l, int r, int rtx){
int m;
if(X1<=l&&r<=X2){
quert_y(1,n,rtx,1);
return;
}
m = (l+r)>>1;
if(X1<=m)
query_x(l,m,rtx<<1);
if(X2>m)
query_x(m+1,r,rtx<<1|1);
}

int main(){
int rnd = 1;
int t,i,j,q;
long long x,y,l;
scanf("%d",&t);
while(t--){
printf("Case #%d:\n",rnd++);
scanf("%d",&n);
for(i=0;i<=n;i++){
for(j=0;j<3*n;j++){
MAX[i][j] = -inf;
MIN[i][j] = inf;
}
}
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
scanf("%lld",&s[i][j]);
//update(i, 1, j, 1,n,x);
}
}
buildx(1,n,1);
scanf("%d",&q);
long long ans, ansMin, ansMax;
for(i=1;i<=q;i++){
scanf("%lld%lld%lld",&x,&y,&l);
ansMin = inf, ansMax = -inf;
maxx = -inf, minx = inf;
int up = (x-l/2>=1?(x-l/2):1);
int down = (x+ l/2<=n?(x+l/2):n);
int left =  ( (y-l/2)>=1?(y-l/2):1 );
int right = ( (y+l/2)<=n?(y+l/2):n );
/*for(j=up; j<=down;j++){
int left =  ( (y-l/2)>=1?(y-l/2):1 );
int right = ( (y+l/2)<=n?(y+l/2):n );
//cout<<"test "<<up<<" "<<down<<" "<<left<<" "<<right<<endl;
ansMin = min(queryMin(j ,1,1,n,left, right ), ansMin);
ansMax = max(queryMax(j ,1,1,n,left, right ), ansMax);
}*/
X1 = up, Y1 = left, X2 = down, Y2 = right;
query_x(1,n,1);
//cout<<"maxx "<<maxx<<" minx "<<minx<<" "<<X1<<" "<<X2<<" "<<Y1<<" "<<Y2 <<endl;
ans = ((long long)minx + (long long)maxx)/2;
printf("%lld\n",ans);
val = ans;
X1 = x, Y1 = y, X2 = x, Y2 = y;
updatex(1,n,1);
//update(x, 1, y, 1, n, ans);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: