HDU 4819 Mosaic 二维线段树

二维RMQ问题

用二维线段树可以实现


#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <stack>

#define maxn 5000
#define _max(a,b) ((a)>(b)?(a):(b))
#define _min(a,b) ((a)>(b)?(b):(a))

const int maxi = 1 << 30;
const int mini = 1 << 31;
const int inf = 0x3f3f3f3f;

using namespace std;
struct SegTree {
    int maxx[maxn][maxn], minn[maxn][maxn], n, m;
    int xo, x1, x2, y1, y2, resmin, resmax, x, y, val, isleaf;
    void query1D(int o, int l, int r) {
        if(y1 <= l && r <= y2) {
            resmin = min(resmin, minn[xo][o]);
            resmax = max(resmax, maxx[xo][o]);
            //printf("y1:%d y2:%d l:%d r:%d %d %d\n", y1, y2, l, r, minn[xo][o], maxx[xo][o]);
        }
        else {
            int mid = l + (r - l) / 2;
            if(y1 <= mid) query1D(2 * o, l, mid);
            if(mid < y2) query1D(2 * o + 1, mid + 1, r);
        }
    }
    void query2D(int o, int l, int r) {
        if(x1 <= l && x2 >= r) {
            xo = o;
            query1D(1, 1, m);
        }
        else {
            int mid = l + (r - l) / 2;
            if(x1 <= mid) query2D(2 * o, l, mid);
            if(x2 > mid) query2D(2 * o + 1, mid + 1, r);
        }
    }
    void change1D(int o, int l, int r) {
        if(l == r) {
            if(isleaf) {
                maxx[xo][o] = minn[xo][o] = val;
                return ;
            }
            maxx[xo][o] = max(maxx[2 * xo][o], maxx[2 * xo + 1][o]);
            minn[xo][o] = min(minn[2 * xo][o], minn[2 * xo + 1][o]);
        }
        else {
            int mid = l + (r - l) / 2;
            if(y <= mid) change1D(2 * o, l, mid);
            else change1D(2 * o + 1, mid + 1, r);
            maxx[xo][o] = max(maxx[xo][2 * o], maxx[xo][2 * o + 1]);
            minn[xo][o] = min(minn[xo][2 * o], minn[xo][2 * o + 1]);
        }
    }
    void change2D(int o, int l, int r) {
        if(l == r) {
            xo = o;
            isleaf = 1;
            change1D(1, 1, m);
        }
        else {
            int mid = l + (r - l) / 2;
            if(x <= mid) change2D(2 * o, l, mid);
            else change2D(2 * o + 1, mid + 1, r);
            xo = o;
            isleaf = 0;
            change1D(1, 1, m);
        }
    }
    void query() {
        resmin = inf;
        resmax = -inf;
        query2D(1, 1, n);
    }
    void change() {
        change2D(1, 1, n);
    }
};
SegTree Tree;
int main() {
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    #endif
    int T, cas = 1;
    scanf("%d", &T);
    while(T--) {
        printf("Case #%d:\n", cas++);
        int n;
        scanf("%d", &n);
        Tree.n = Tree.m = n;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                int t;
                scanf("%d", &t);
                Tree.x = i, Tree.y = j, Tree.val = t;
                Tree.change();
            }
        }
        int m;
        scanf("%d", &m);
        for(int i = 0; i < m; i++) {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            Tree.x1 = max(1, a - c / 2);
            Tree.x2 = min(n, a + c / 2);
            Tree.y1 = max(1, b - c / 2);
            Tree.y2 = min(n, b + c / 2);
            Tree.query();
            Tree.val = (Tree.resmin + Tree.resmax) / 2;
            //printf("%d %d ", Tree.resmin, Tree.resmax);
            printf("%d\n", Tree.val);
            Tree.x = a, Tree.y = b;
            Tree.change();
        }
    }
    return 0;
}



题目:

Mosaic

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


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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值