hdu 4819 Mosaic(树套树型二维线段树)

来源:互联网 发布:http js.zy.com 编辑:程序博客网 时间:2024/05/16 06:01

Mosaic

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


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
131 2 34 5 67 8 952 2 13 2 31 1 31 2 32 2 3
 

Sample Output
Case #1:56346
 

Source
2013 Asia Regional Changchun
 题目大意:给出一个矩阵,查询矩阵中的最大值和最小值,然后修改矩阵位置居中的点为最大值和最小值的平均数
题目分析:二维线段树,不解释
#include <algorithm>#include <cstdio>#include <iostream>#include <cstring>#define MAX 807#define INF 1000000001using namespace std;struct Node{    int l , r , maxn , minn;};struct Tree{    int l , r;    Node node[MAX<<2];}tree[MAX<<2];int t,mx,mi,q;int n,a[MAX][MAX];void push_up ( int u , int i ){    tree[u].node[i].maxn = max ( tree[u].node[i<<1].maxn , tree[u].node[i<<1|1].maxn );    tree[u].node[i].minn = min ( tree[u].node[i<<1].minn , tree[u].node[i<<1|1].minn );}void build ( int u , int i , int x ,  int l , int r ){    tree[u].node[i].l = l , tree[u].node[i].r = r;    if ( l == r )    {        if ( x != -1 ) tree[u].node[i].maxn = tree[u].node[i].minn = a[x][l];        return;    }    int mid = l + r >> 1;    build ( u , i<<1 , x , l , mid );    build ( u , i<<1|1 , x , mid+1 , r );    push_up ( u , i );}void push_up ( int u , int i , int l , int r ){    tree[u].node[i].l = l , tree[u].node[i].r = r;    if ( l == r )    {        tree[u].node[i].maxn = max ( tree[u<<1].node[i].maxn , tree[u<<1|1].node[i].maxn );        tree[u].node[i].minn = min ( tree[u<<1].node[i].minn , tree[u<<1|1].node[i].minn );        return;    }    int mid = l + r >> 1;    push_up ( u , i<<1 , l , mid );    push_up ( u , i<<1|1 , mid+1 , r );    push_up ( u , i );}void build ( int u , int l , int r ){    tree[u].l = l , tree[u].r = r;    if ( l == r )    {        build ( u , 1 , l , 1 , n );        return;    }    int mid = l + r >> 1;    build ( u<<1 , l , mid );    build ( u<<1|1 , mid+1 , r );    push_up ( u , 1 , 1 , n );}   void update ( int u , int i , int x , int v ){    int l = tree[u].node[i].l , r = tree[u].node[i].r;    if ( l == r )    {        tree[u].node[i].maxn = v;        tree[u].node[i].minn = v;        return;    }    int mid = l + r >> 1;    if ( x > mid ) update ( u , i<<1|1 , x , v );    else update ( u , i<<1 , x ,v );    push_up ( u , i );}void push_up ( int u , int i , int x ){    int l = tree[u].node[i].l , r = tree[u].node[i].r;    if ( l == r )    {        tree[u].node[i].maxn = max ( tree[u<<1].node[i].maxn , tree[u<<1|1].node[i].maxn );        tree[u].node[i].minn = min ( tree[u<<1].node[i].minn , tree[u<<1|1].node[i].minn );        return;    }    int mid = l + r >> 1;    if ( x > mid ) push_up ( u , i<<1|1 , x );    else push_up ( u , i<<1 , x );    push_up ( u , i );}void update ( int u , int x , int y , int v , int flag ){    int l = tree[u].l , r = tree[u].r;    if ( l == r )    {        update ( u , 1 , y , v );        return;    }    int mid = l + r >> 1;    if ( x > mid )        update ( u<<1|1  , x , y , v , 1 );    else update ( u<<1 , x , y , v , 1 );    push_up ( u , 1 , y );}void query ( int u , int i , int left , int right  ){    int l = tree[u].node[i].l , r = tree[u].node[i].r;    if ( left <= l && r <= right )    {        mx = max ( mx , tree[u].node[i].maxn );        mi = min ( mi , tree[u].node[i].minn );        return;    }    int mid = l + r >> 1;    if ( left <= mid && right >= l ) query ( u , i<<1 , left , right );    if ( left <= r && right > mid ) query ( u , i<<1|1 , left , right );}void query ( int u , int left , int right , int up , int down ){    int l = tree[u].l , r = tree[u].r;    if ( left <= l && r <= right )    {        query ( u , 1 , up , down );        return;    }    int mid = l + r >> 1;    if ( left <= mid && right >= l ) query ( u<<1 , left , right , up , down );    if ( left <= r && right > mid ) query ( u<<1|1 , left , right , up , down ); }int main ( ){    scanf ( "%d" , &t );    int c = 1;    while ( t-- )    {        scanf ( "%d" , &n );        for ( int i = 1 ; i <= n ; i++ )            for ( int j = 1 ; j <= n ; j++ )                scanf ( "%d" , &a[i][j] );        build ( 1 , 1 , n );        scanf ( "%d" , &q );        int l,x,y;        printf ( "Case #%d:\n" , c++ );        for ( int i = 0 ; i < q ; i++ )        {            scanf ( "%d%d%d" , &x , &y , &l );            l /= 2;            int x1 = x-l>0?x-l:1;            int y1 = y-l>0?y-l:1;            int x2 = x+l<n?x+l:n;            int y2 = y+l<n?y+l:n;            mx = -INF , mi = INF;            query ( 1 , x1 , x2 , y1 , y2 );            int ans = (mx+mi)/2;           // cout << mx << " " << mi << endl;            printf ( "%d\n" , ans );            update ( 1 , x , y , ans , 1 );        }    }}


0 0
原创粉丝点击