hdoj 4819 Mosaic 【二维线段树 单点更新 区间查询】

题目链接:hdoj 4819 Mosaic

Mosaic

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

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

题意:查询区间最大值mx与最小值mi,单点更新为(mx + mi) / 2。

AC代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 800 + 10;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
struct Nodey {
    int ly, ry, Max, Min, val;
};
int nx, ny;
int posx[MAXN], posy[MAXN];
struct Nodex {
    int lx, rx;
    Nodey treey[MAXN<<2];
    void Build_y(int o, int l, int r) {
        treey[o].ly = l; treey[o].ry = r;
        treey[o].Max = 0; treey[o].Min = INF;
        if(l == r) {
            posy[l] = o;
            return ;
        }
        int mid = (l + r) >> 1;
        Build_y(ll, l, mid);
        Build_y(rr, mid+1, r);
    }
    int Query_y(int o, int y1, int y2, int op) {
        if(treey[o].ly == y1 && treey[o].ry == y2) {
            return op ? treey[o].Max : treey[o].Min;
        }
        int mid = (treey[o].ly + treey[o].ry) >> 1;
        if(y2 <= mid) return Query_y(ll, y1, y2, op);
        else if(y1 > mid) return Query_y(rr, y1, y2, op);
        else return op ? max(Query_y(ll, y1, mid, op), Query_y(rr, mid+1, y2, op))
                       : min(Query_y(ll, y1, mid, op), Query_y(rr, mid+1, y2, op));
    }
};
Nodex treex[MAXN<<2];
void Build_x(int o, int l, int r) {
    treex[o].lx = l; treex[o].rx = r;
    treex[o].Build_y(1, 1, ny);
    if(l == r) {
        posx[l] = o;
        return ;
    }
    int mid = (l + r) >> 1;
    Build_x(ll, l, mid);
    Build_x(rr, mid+1, r);
}
int Query_x(int o, int x1, int x2, int y1, int y2, int op) {
    if(treex[o].lx == x1 && treex[o].rx == x2) {
        return treex[o].Query_y(1, y1, y2, op);
    }
    int mid = (treex[o].lx + treex[o].rx) >> 1;
    if(x2 <= mid) return Query_x(ll, x1, x2, y1, y2, op);
    else if(x1 > mid) return Query_x(rr, x1, x2, y1, y2, op);
    else return op ? max(Query_x(ll, x1, mid, y1, y2, op), Query_x(rr, mid+1, x2, y1, y2, op))
                   : min(Query_x(ll, x1, mid, y1, y2, op), Query_x(rr, mid+1, x2, y1, y2, op));
}
void PushUpy(int x, int y) {
    treex[x].treey[y].Max = max(treex[x].treey[y<<1].Max, treex[x].treey[y<<1|1].Max);
    treex[x].treey[y].Min = min(treex[x].treey[y<<1].Min, treex[x].treey[y<<1|1].Min);
}
void PushUpx(int x, int y) {
    treex[x].treey[y].Max = max(treex[x<<1].treey[y].Max, treex[x<<1|1].treey[y].Max);
    treex[x].treey[y].Min = min(treex[x<<1].treey[y].Min, treex[x<<1|1].treey[y].Min);
}
void Change(int x, int y, int v) {
    treex[x].treey[y].Max = v;
    treex[x].treey[y].Min = v;
}
void Update(int x, int y, int v) {
    for(int i = posx[x]; i ; i >>= 1) {
        for(int j = posy[y]; j ; j >>= 1) {
            if(i == posx[x] && j == posy[y]) {
                Change(posx[x], posy[y], v);
                continue;
            }
            PushUpy(i, j);
        }
        if(i == posx[x]) continue;
        for(int j = posy[y]; j ; j >>= 1) {
            PushUpx(i, j);
        }
    }
}
int main()
{
    int t, kcase = 1; scanf("%d", &t);
    while(t--) {
        scanf("%d", &nx); ny = nx;
        Build_x(1, 1, nx);
        for(int i = 1; i <= nx; i++) {
            for(int j = 1; j <= ny; j++) {
                int v; scanf("%d", &v);
                Update(i, j, v);
            }
        }
        int Q; scanf("%d", &Q);
        printf("Case #%d:\n", kcase++);
        while(Q--) {
            int x, y, l;
            scanf("%d%d%d", &x, &y, &l);
            int x1 = max(1, x - l / 2);
            int x2 = min(nx, x + l / 2);
            int y1 = max(1, y - l / 2);
            int y2 = min(ny, y + l / 2);
            int mx = Query_x(1, x1, x2, y1, y2, 1);
            int mi = Query_x(1, x1, x2, y1, y2, 0);
            int ans = (mx + mi) >> 1;
            printf("%d\n", ans);
            Update(x, y, ans);
        }
    }
    return 0;
}

你可能感兴趣的:(线段树)