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

以(x, y)这个格子为中心 边长为L 的子矩阵,(不能越界)求出这个子矩阵的最大值和最小值,并将(x,y)这个格子改成(最大+最小)/2;

输出(x,y)。

二维线段树裸题。

#include <cstdio>
#include <algorithm>
using namespace std;
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
const int maxn = 805;
const int MAX = 0x3f3f3f3f;

int T, n, q, a, b, d, x1, x2, y1, y2, ans;
int mx[maxn<<2][maxn<<2], mi[maxn<<2][maxn<<2];

void up(int oo, int o) {
    mi[oo][o] = min(mi[oo][o<<1] , mi[oo][o<<1|1]);
    mx[oo][o] = max(mx[oo][o<<1] , mx[oo][o<<1|1]);
}

void buildx(int o, int l, int r, int oo, int ok) {
    if(l == r) {
        if(ok == -1) {
            scanf("%d", &mx[oo][o]);
            mi[oo][o] = mx[oo][o];
        } else {
            mx[oo][o] = max(mx[oo<<1][o], mx[oo<<1|1][o]);
            mi[oo][o] = min(mi[oo<<1][o], mi[oo<<1|1][o]);
        }
        return;
    }
    int m = (l+r) >> 1;
    buildx(lson, oo, ok);
    buildx(rson, oo, ok);
    up(oo, o);
}

void buildy(int o, int l, int r) {
    if(l == r) {
        buildx(1, 1, n, o, -1);
        return;
    }
    int m = (l+r) >> 1;
    buildy(lson);
    buildy(rson);
    buildx(1, 1, n, o, 1);
}

int queryx1(int o, int l, int r, int oo) {
    if(x1 <= l && r <= x2) {
        return mx[o][oo];
    }
    int m = (l+r) >> 1, res = -1;
    if(x1 <= m) res = max(res, queryx1(lson, oo));
    if(m < x2) res = max(res, queryx1(rson, oo));
    return res;
}

int queryy1(int o, int l, int r) {
    if(y1 <= l && r <= y2) {
        return queryx1(1, 1, n, o);
    }
    int m = (l+r) >> 1, res = -1;
    if(y1 <= m) res = max(res, queryy1(lson));
    if(m < y2) res = max(res, queryy1(rson));
    return res;
}

int queryx2(int o, int l, int r, int oo) {
    if(x1 <= l && r <= x2) {
        return mi[o][oo];
    }
    int m = (l+r) >> 1, res = MAX;
    if(x1 <= m) res = min(res, queryx2(lson, oo));
    if(m <  x2) res = min(res, queryx2(rson, oo));
    return res;
}

int queryy2(int o, int l, int r) {
    if(y1 <= l && r <= y2) {
        return queryx2(1, 1, n, o);
    }
    int m = (l+r) >> 1, res = MAX;
    if(y1 <= m) res = min(res, queryy2(lson));
    if(m < y2) res = min(res, queryy2(rson));
    return res;
}

void updatex(int o, int l, int r, int oo, int ok) {
    if(l == r) {
        if(ok == -1) {
            mi[oo][o] = mx[oo][o] = ans;
        } else {
            mx[oo][o] = max(mx[oo<<1][o], mx[oo<<1|1][o]);
            mi[oo][o] = min(mi[oo<<1][o], mi[oo<<1|1][o]);
        }
        return;
    }
    int m = (l+r) >> 1;
    if(b <= m) updatex(lson, oo, ok);
    else updatex(rson, oo, ok);
    up(oo, o);
}

void updatey(int o, int l, int r) {
    if(l == r) {
        updatex(1, 1, n, o, -1);
        return;
    }
    int m = (l+r) >> 1;
    if(a <= m) updatey(lson);
    else updatey(rson);
    updatex(1, 1, n, o, 1);
}

int main()
{
    //freopen("in.txt", "r", stdin);
    scanf("%d", &T);
    for(int ca = 1; ca <= T; ca++) {
        printf("Case #%d:\n", ca);
        scanf("%d", &n);
        buildy(1, 1, n);
        scanf("%d", &q);
        while(q--) {
            scanf("%d%d%d", &a, &b, &d);
            d /= 2;
            x1 = max(1, a-d);
            x2 = min(n, a+d);
            y1 = max(1, b-d);
            y2 = min(n, b+d);
            ans = queryy1(1, 1, n) + queryy2(1, 1, n);
            ans /= 2;
            printf("%d\n", ans);
            updatey(1, 1, n);

        }
    }

    return 0;
}



时间: 2024-10-13 05:10:23

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 【二维线段树】

题目大意:给你一个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做了