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

field=problem&key=2013%20Asia%20Regional%20Changchun%20&source=1&searchmode=source">2013 Asia Regional Changchun

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

#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:\n",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\n",t);

            update(1,1,n,a,b,t);
        }
    }
}
时间: 2024-10-13 04:57:07

HDU-4819-Mosaic(二维线段树)的相关文章

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

题意: 给一个矩阵,每次查询一个子矩阵内的最大最小值,然后更新子矩阵中心点为(Max+Min)/2. 解法: 由于是矩阵,且要求区间最大最小和更新单点,很容易想到二维的线段树,可是因为之前没写过二维的线段树,所以没跳出来.后来熟悉了一下,原来很多细节地方都没有考虑到. 这里build,update,query都分为两个函数,第一个为Y轴的(sub_update),第二个为X轴的(update),不仅每个sub_update或sub_build后面要加Y轴的pushup函数,而且每个update或

【HDU 4819】Mosaic 二维线段树模板

二维线段树的模板题,和一维一样的思路,更新的时候注意一下细节. 存模板: /* 二维线段树模板整理 */ #include<cstdio> #include<algorithm> using namespace std; #define lson (pos<<1) #define rson (pos<<1|1) const int maxn = 805; const int INF = (1 << 30); int n; int posX[max

树套树+【UVALive】6709 Mosaic 二维线段树

题目链接:6709 Mosaic 题解:参考这个博客:二维线段树,先按行建树然后每一个节点也是一个棵线段树按列建. #include<bits/stdc++.h> #include<cmath> #include<set> #include<cstdio> #include<iomanip> #include<iostream> #include<string> #include<cstring> #inclu

UVALive 6709 - Mosaic 二维线段树

题目链接 给一个n*n的方格, 每个方格有值. 每次询问, 给出三个数x, y, l, 求出以x, y为中心的边长为l的正方形内的最大值与最小值, 输出(maxx+minn)/2, 并将x, y这个格子的值改为(maxx+minn)/2.题目保证l为奇数. 二维线段树的单点更新, 区间查询. 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb(x) push_back(x) 4 #define ll long long 5

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

HDU 4819 Mosaic 【二维线段树】

题目大意:给你一个n*n的矩阵,每次找到一个点(x,y)周围l*l的子矩阵中的最大值a和最小值b,将(x,y)更新为(a+b)/2 思路:裸的二维线段树 #include<iostream>#include<cstdio>#include <math.h>#include<algorithm>#include<string.h>#include<queue>#define MOD 10000007#define maxn 3500#d

#树套树,二维线段树#HDU 4819 Mosaic

题目 多组数据,给定一个\(n*n\)的矩阵(\(n\leq 80,a_{i,j}\leq 10^9\)) 多组询问一个以\((x,y)\)为中心,边长为\(L\)的子矩阵最大值\(mx\)和最小值\(mn\), 并将\((x,y)\)这一个位置修改为\(\lfloor\frac{mn+mx}{2}\rfloor\),每次询问输出修改后的\((x,y)\) 分析 二维线段树裸题,反正之前也没敲过, 其实和一维线段树相近,找到\(lx\sim rx\)的下标 再按照一维线段树的方式修改最大最小值就

hdu 4819 二维线段树模板

/* HDU 4819 Mosaic 题意:查询某个矩形内的最大最小值, 修改矩形内某点的值为该矩形(Mi+MA)/2; 二维线段树模板: 区间最值,单点更新. */ #include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 1010; int N, Q; struct Nodey { int l, r; int Max, Min; }; int locx[MAXN], l

HDU 1823 Luck and Love 二维线段树

Problem Description 世界上上最远的距离不是相隔天涯海角 而是我在你面前 可你却不知道我爱你 ―― 张小娴 前段日子,枫冰叶子给Wiskey做了个征婚启事,聘礼达到500万哦,天哪,可是天文数字了啊,不知多少MM蜂拥而至,顿时万人空巷,连扫地的大妈都来凑热闹来了.―_―||| 由于人数太多,Wiskey实在忙不过来,就把统计的事情全交给了枫冰叶子,自己跑回家休息去了.这可够枫冰叶子忙的了,他要处理的有两类事情,一是得接受MM的报名,二是要帮Wiskey查找符合要求的MM中缘分最

HDU 1823 二维线段树(区间max)

Luck and Love Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5262    Accepted Submission(s): 1317 Problem Description 世界上上最远的距离不是相隔天涯海角而是我在你面前可你却不知道我爱你                ―― 张小娴 前段日子,枫冰叶子给Wiskey做了