HDU4819 Mosaic(二维线段树模板)

Mosaic

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


 

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


题解:直接是一个二维线段树模板 区间查询外加区间更新

建树:

首先对x轴方向建树:

这是一维的线段树建立而二维的线段树建立就是在对于一维的每一个节点上在建一个关于y的二维线段树所以对于每一一个节点他们都有4分支;

关于对y的线段树建立有两种情况:

  1. x轴方向定点:这时候就可以直接依照一维的在y上面建就好了
  2. x轴方向是定点:同样在y轴方向建立一维线段树直到遇到y方向的定点然后在更新x方向的值

对于x方向是不是定点我们可以传一个flag值去进行判断

具体代码:


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
const int maxn=800+2;
const int inf=0x3f3f3f3f;
typedef long long ll;
struct Node
{
    ll maxw,minw;
}tree[maxn<<2][maxn<<2];
int n,q;
ll minw,maxw;
void push_upy(int deep,int k)
{
    tree[deep][k].minw=min(tree[deep][k<<1].minw,tree[deep][k<<1|1].minw);
    tree[deep][k].maxw=max(tree[deep][k<<1].maxw,tree[deep][k<<1|1].maxw);
    return ;
}
void push_upx(int deep,int k)
{
    tree[deep][k].minw=min(tree[deep<<1][k].minw,tree[deep<<1|1][k].minw);
    tree[deep][k].maxw=max(tree[deep<<1][k].maxw,tree[deep<<1|1][k].maxw);
    return ;
}
void bulid_y(int ly,int ry,int deep,int k,int flag)
{
    tree[deep][k].maxw=-inf;
    tree[deep][k].minw=inf;
    if(ly==ry)
    {
        if(flag)
        {
        scanf("%lld",&tree[deep][k].maxw);
        tree[deep][k].minw=tree[deep][k].maxw;
        }
        else {push_upx(deep,k);}//更新x方向
        return ;
    }
    int mind=(ly+ry)>>1;
    bulid_y(ly,mind,deep,k<<1,flag);
    bulid_y(mind+1,ry,deep,k<<1|1,flag);
    push_upy(deep,k);
    return ;
}
void bulid_x(int lx,int rx,int deep)
{
    if(lx==rx)
    {
        bulid_y(1,n,deep,1,1);
        return ;
    }
    int mind=(lx+rx)>>1;
    bulid_x(lx,mind,deep<<1);
    bulid_x(mind+1,rx,deep<<1|1);
    bulid_y(1,n,deep,1,0);//同区域y轴方向
    return ;
}
void query_y(int ly,int ry,int Ly,int Ry,int deep,int k)
{
    if(Ly<=ly&&ry<=Ry)
    {
        minw=min(tree[deep][k].minw,minw);
        maxw=max(tree[deep][k].maxw,maxw);
        return;
    }
    int mind=(ly+ry)>>1;
    if(Ly<=mind)
    query_y(ly,mind,Ly,Ry,deep,k<<1);
    if(Ry>mind)
    query_y(mind+1,ry,Ly,Ry,deep,k<<1|1);
    return ;
}
void query_x(int lx,int rx,int Lx,int Rx,int Ly,int Ry,int deep)
{
    if(Lx<=lx&&rx<=Rx)
    {
        query_y(1,n,Ly,Ry,deep,1);
        return;
    }
    int mind=(lx+rx)>>1;
    if(Lx<=mind)
    query_x(lx,mind,Lx,Rx,Ly,Ry,deep<<1);
    if(Rx>mind)
    query_x(mind+1,rx,Lx,Rx,Ly,Ry,deep<<1|1);
    return ;
}
void update_y(int ly,int ry,int y,int deep,int k,int flag)
{
    if(ly==ry&&ly==y)
    {
        if(flag)
        {
            tree[deep][k].minw=tree[deep][k].maxw=floor((minw+maxw)/2);
        }
        else push_upx(deep,k);
        return ;
    }
    int mind=(ly+ry)>>1;
    if(y<=mind){update_y(ly,mind,y,deep,k<<1,flag);}
    else       {update_y(mind+1,ry,y,deep,k<<1|1,flag);}
    push_upy(deep,k);
    return;
}
void update_x(int lx,int rx,int x,int y,int deep)
{
    if(lx==rx&&lx==x)
    {
        update_y(1,n,y,deep,1,1);
        return ;
    }
    int mind=(lx+rx)>>1;
    if(x<=mind){update_x(lx,mind,x,y,deep<<1);}
    else       {update_x(mind+1,rx,x,y,deep<<1|1);}
    update_y(1,n,y,deep,1,0);
    return;
}
int main()
{
   // freopen("data.txt","r",stdin);
    int t,ti=1;
    scanf("%d",&t);
    while(t--)
    {
        printf("Case #%d:\n",ti++);
        scanf("%d",&n);
        bulid_x(1,n,1);
        scanf("%d",&q);
        int x,y,l;
        while(q--)
        {
            scanf("%d%d%d",&x,&y,&l);
            minw=inf;maxw=-inf;
            int r=l/2;
            query_x(1,n,max(x-r,1),min(x+r,n),max(y-r,1),min(y+r,n),1);
            printf("%lld\n",(ll)floor((minw+maxw)/2));
            update_x(1,n,x,y,1);
        }
    }

   return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值