HDU-4819-Mosaic(二维线段树)

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


思路:二维线段树,单点更新。


#include <stdio.h>
#define INF 1000000001
#define min(A,B)(A<B?A:B)
#define max(A,B)(A>B?A:B)

int mx[3200][3200],mn[3200][3200],n,minnum,maxnum;

void buildy(int x,int idx,int s,int e,bool flag)//flag为1表示第一维为叶子节点。为0表示第一维不是叶子节点
{
    if(s==e)
    {
        if(flag)
        {
            scanf("%d",&mn[x][idx]);

            mx[x][idx]=mn[x][idx];
        }
    }
    else
    {
        int mid=(s+e)>>1;

        buildy(x,idx<<1,s,mid,flag);
        buildy(x,idx<<1|1,mid+1,e,flag);

        mx[x][idx]=max(mx[x][idx<<1],mx[x][idx<<1|1]);
        mn[x][idx]=min(mn[x][idx<<1],mn[x][idx<<1|1]);
    }

    if(!flag)//第一维不是叶子节点就更新第一维
    {
        mx[x][idx]=max(mx[x<<1][idx],mx[x<<1|1][idx]);
        mn[x][idx]=min(mn[x<<1][idx],mn[x<<1|1][idx]);
    }
    
}

void build(int idx,int s,int e)
{
    if(s!=e)
    {
        int mid=(s+e)>>1;

        build(idx<<1,s,mid);
        build(idx<<1|1,mid+1,e);

        buildy(idx,1,1,n,0);
    }
    else buildy(idx,1,1,n,1);
}

void updatey(int x,int idx,int s,int e,int pos,int val,bool flag)
{
    if(s==e) mn[x][idx]=mx[x][idx]=val;
    else
    {
        int mid=(s+e)>>1;

        if(pos<=mid) updatey(x,idx<<1,s,mid,pos,val,flag);
        else updatey(x,idx<<1|1,mid+1,e,pos,val,flag);

        mx[x][idx]=max(mx[x][idx<<1],mx[x][idx<<1|1]);
        mn[x][idx]=min(mn[x][idx<<1],mn[x][idx<<1|1]);
    }

    if(!flag)//第一维不是叶子节点就更新第一维
    {
        mx[x][idx]=max(mx[x<<1][idx],mx[x<<1|1][idx]);
        mn[x][idx]=min(mn[x<<1][idx],mn[x<<1|1][idx]);
    }
    
}

void update(int idx,int s,int e,int pos,int posy,int val)
{
    if(s==e) updatey(idx,1,1,n,posy,val,1);
    else
    {
        int mid=(s+e)>>1;

        if(pos<=mid) update(idx<<1,s,mid,pos,posy,val);
        else update(idx<<1|1,mid+1,e,pos,posy,val);

        updatey(idx,1,1,n,posy,val,0);
    }
}

void queryy(int x,int idx,int s,int e,int l,int r)
{
    if(s==l && e==r)
    {
        minnum=min(minnum,mn[x][idx]);
        maxnum=max(maxnum,mx[x][idx]);
    }
    else
    {
        int mid=(s+e)>>1;

        if(r<=mid) queryy(x,idx<<1,s,mid,l,r);
        else if(l>mid) queryy(x,idx<<1|1,mid+1,e,l,r);
        else
        {
            queryy(x,idx<<1,s,mid,l,mid);
            queryy(x,idx<<1|1,mid+1,e,mid+1,r);
        }
    }
}

void query(int idx,int s,int e,int l,int r,int ly,int ry)
{
    if(s==l && e==r) queryy(idx,1,1,n,ly,ry);
    else
    {
        int mid=(s+e)>>1;

        if(r<=mid) query(idx<<1,s,mid,l,r,ly,ry);
        else if(l>mid) query(idx<<1|1,mid+1,e,l,r,ly,ry);
        else
        {
            query(idx<<1,s,mid,l,mid,ly,ry);
            query(idx<<1|1,mid+1,e,mid+1,r,ly,ry);
        }
    }
}

int main()
{
    int T,i,q,a,b,c,l,r,ly,ry,t;

    scanf("%d",&T);

    for(i=1;i<=T;i++)
    {
        scanf("%d",&n);

        build(1,1,n);

        scanf("%d",&q);

        printf("Case #%d:
",i);

        while(q--)
        {
            scanf("%d%d%d",&a,&b,&c);

            l=max(a-c/2,1);
            r=min(a+c/2,n);
            ly=max(b-c/2,1);
            ry=min(b+c/2,n);

            minnum=INF;
            maxnum=-INF;

            query(1,1,n,l,r,ly,ry);

            t=(minnum+maxnum)/2;

            printf("%d
",t);

            update(1,1,n,a,b,t);
        }
    }
}


原文地址:https://www.cnblogs.com/gcczhongduan/p/5181136.html