hdu4819-Mosaic 二維線段樹

Mosaic

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 1891    Accepted Submission(s): 831


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
題意:給你一個n*n的矩陣,給你一個座標(x,y),詢問在座標附近l的範圍內,最大值a和最小值b,將(x,y)的值改爲(a+b)/2;
解題思路:這是一道關於二維線段樹的模板題,一維線段樹是解決區間有關的問題,而二維線段樹解決的是關於區域的問題,一維線段樹是一顆二叉樹,而二維線段樹是一顆四叉樹,非葉子節點有2個或者4個子樹,有關二維線段樹的具體知識我會稍後寫出有關博客,二維線段樹專題下面是ac代碼:
#include<iostream>
#include<cstdio>
#include <math.h>
#include<algorithm>
#include<string.h>
#include<queue>
#define MOD 10000007
#define maxn 3500
#define LL long long
using namespace std;
int tree_max[maxn][maxn],tree_min[maxn][maxn],ans_min,ans_max,n;
int a[maxn][maxn];
void build_y(int xx,int node,int l,int r,int x,int type)
{
        if(l+1==r){
                if(type)tree_max[xx][node]=tree_min[xx][node]=a[x][l];
                else
                {
                        tree_max[xx][node]=max(tree_max[xx*2][node],tree_max[xx*2+1][node]);
                        tree_min[xx][node]=min(tree_min[xx*2][node],tree_min[xx*2+1][node]);
                }
                return ;
        }
        int mid=(l+r)>>1;
        build_y(xx,node*2,l,mid,x,type);
        build_y(xx,node*2+1,mid,r,x,type);
        tree_max[xx][node]=max(tree_max[xx][node*2],tree_max[xx][node*2+1]);
        tree_min[xx][node]=min(tree_min[xx][node*2],tree_min[xx][node*2+1]);
}
void build_x(int node,int l,int r)
{
        if(l+1==r)
        {
                build_y(node,1,1,n+1,l,1);
                return ;
        }
        int mid=(l+r)>>1;
        build_x(node*2,l,mid);
        build_x(node*2+1,mid,r);
        build_y(node,1,1,n+1,l,0);
}
void query_y(int xx,int node,int l,int r,int ql,int qr)
{
        if(ql<=l&&r<=qr)
        {
                ans_max=max(ans_max,tree_max[xx][node]);
                ans_min=min(ans_min,tree_min[xx][node]);
                return;
        }
        int mid=(l+r)>>1;
        if(ql<mid)query_y(xx,node*2,l,mid,ql,qr);
        if(qr>mid)query_y(xx,node*2+1,mid,r,ql,qr);
}
void query_x(int node,int l,int r,int ql,int qr,int y1,int y2)
{
        if(ql<=l && r<=qr)
        {
                query_y(node,1,1,1+n,y1,y2);return ;
        }
        int mid=(l+r)>>1;
        if(ql<mid)query_x(node*2,l,mid,ql,qr,y1,y2);
        if(qr>mid)query_x(node*2+1,mid,r,ql,qr,y1,y2);
}
void update_y(int xx,int node,int l,int r,int pos,int num,int type){
        if(l+1==r){
                if(type)tree_max[xx][node]=tree_min[xx][node]=num;
                else{
                        tree_max[xx][node]=max(tree_max[xx*2][node],tree_max[xx*2+1][node]);
                        tree_min[xx][node]=min(tree_min[xx*2][node],tree_min[xx*2+1][node]);
                }
                return ;
        }
        int mid=(l+r)>>1;
        if(pos<mid)update_y(xx,node*2,l,mid,pos,num,type);else
        update_y(xx,node*2+1,mid,r,pos,num,type);
        tree_max[xx][node]=max(tree_max[xx][node*2],tree_max[xx][node*2+1]);
        tree_min[xx][node]=min(tree_min[xx][node*2],tree_min[xx][node*2+1]);
}
void update_x(int node,int l,int r,int x,int y,int num)
{
        if(l+1==r)
        {
                update_y(node,1,1,n+1,y,num,1);
                return ;
        }
        int mid=(l+r)>>1;
        if(x<mid)update_x(node*2,l,mid,x,y,num);
        else update_x(node*2+1,mid,r,x,y,num);
        update_y(node,1,1,n+1,y,num,0);
}
int main()
{
        int t,cas=0;
        scanf("%d",&t);
        while(t--)
        {
                memset(tree_max,0,sizeof(tree_max));
                memset(tree_min,0,sizeof(tree_min));
                printf("Case #%d:\n",++cas);
                int q,x,y,l;
                scanf("%d",&n);
                for(int i=1;i<=n;i++)
                        for(int j=1;j<=n;j++)scanf("%d",&a[i][j]);
                build_x(1,1,n+1);
                scanf("%d",&q);
                while(q--)
                {
                        scanf("%d%d%d",&x,&y,&l);l=(l-1)>>1;
                        ans_max=0;ans_min=0x3f3f3f3f;
                        query_x(1,1,n+1,max(x-l,1),min(x+l+1,n+1),max(y-l,1),min(y+l+1,n+1));
                        update_x(1,1,n+1,x,y,(ans_max+ans_min)>>1);
                        printf("%d\n",(ans_max+ans_min)>>1);
                }
        }
        return 0;
}

題目鏈接:點擊打開鏈接http://acm.hdu.edu.cn/showproblem.php?pid=4819

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章