zoukankan      html  css  js  c++  java
  • HDU 4819 Mosaic D区段树


    Mosaic

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


    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
     

    给你一个n*n的矩阵,然后给你m个请求。每一个请求有x,y,r这个三个数。代表在以第x行第y列为中心,长度为l的矩阵中,找一个最大值和一个最小值,并将(x,y)处改为(最大值+最小值)/2.
    二维线段树中的一维代表行,还有一维代表列。

    //1859MS	50772K
    #include<stdio.h>
    #include<algorithm>
    #define M 1007
    #define eps 1e-4
    #define inf 0x3f3f3f3f
    using namespace std;
    int lx[M],ly[M];
    int n;
    struct Sub_Tree
    {
        int left,right,minn,maxx;
        int mid(){return (left+right)>>1;}
    };
    struct Tree
    {
        int left,right;
        int mid(){return (left+right)>>1;}
        Sub_Tree subtree[4*M];
    }tree[M*4];
    
    void build_subtree(int l,int r,int i,int fa)
    {
        tree[fa].subtree[i].left=l;
        tree[fa].subtree[i].right=r;
        tree[fa].subtree[i].minn=inf;
        tree[fa].subtree[i].maxx=-inf;
        if(l==r){ly[l]=i;return;}
        int mid=(l+r)>>1;
        build_subtree(l,mid,i<<1,fa);
        build_subtree(mid+1,r,(i<<1)|1,fa);
    }
    
    void build(int l,int r,int i)
    {
        tree[i].left=l;tree[i].right=r;
        build_subtree(1,n,1,i);
        if(l==r){lx[l]=i;return;}
        int mid=(l+r)>>1;
        build(l,mid,i<<1);
        build(mid+1,r,(i<<1)|1);
    }
    
    void update(int x,int y,int val)
    {
        int xx=lx[x];
        int yy=ly[y];
        tree[xx].subtree[yy].minn=tree[xx].subtree[yy].maxx=val;
        for(int i=xx;i;i>>=1)
            for(int j=yy;j;j>>=1)
            {
                if(i==xx&&j==yy)continue;
                if(j==yy)
                {
                    tree[i].subtree[j].minn=min(tree[i<<1].subtree[j].minn,tree[(i<<1)|1].subtree[j].minn);
                    tree[i].subtree[j].maxx=max(tree[i<<1].subtree[j].maxx,tree[(i<<1)|1].subtree[j].maxx);
                }
                else
                {
                    tree[i].subtree[j].minn=min(tree[i].subtree[j<<1].minn,tree[i].subtree[(j<<1)|1].minn);
                    tree[i].subtree[j].maxx=max(tree[i].subtree[j<<1].maxx,tree[i].subtree[(j<<1)|1].maxx);
                }
            }
    }
    
    int query_subtree_min(int a1,int a2,int i,int fa)
    {
        if(tree[fa].subtree[i].left==a1&&tree[fa].subtree[i].right==a2)return tree[fa].subtree[i].minn;
        int mid=tree[fa].subtree[i].mid();
        if(a2<=mid)return query_subtree_min(a1,a2,i<<1,fa);
        else if(mid<a1)return query_subtree_min(a1,a2,(i<<1)|1,fa);
        else return min(query_subtree_min(a1,mid,i<<1,fa),query_subtree_min(mid+1,a2,(i<<1)|1,fa));
    }
    
    int query_min(int x1,int x2,int y1,int y2,int i)
    {
        if(tree[i].left==x1&&tree[i].right==x2)return query_subtree_min(y1,y2,1,i);
        int mid=tree[i].mid();
        if(x2<=mid)return query_min(x1,x2,y1,y2,i<<1);
        else if(mid<x1)return query_min(x1,x2,y1,y2,(i<<1)|1);
        else return min(query_min(x1,mid,y1,y2,i<<1),query_min(mid+1,x2,y1,y2,(i<<1)|1));
    }
    
    int query_subtree_max(int a1,int a2,int i,int fa)
    {
        if(tree[fa].subtree[i].left==a1&&tree[fa].subtree[i].right==a2)return tree[fa].subtree[i].maxx;
        int mid=tree[fa].subtree[i].mid();
        if(a2<=mid)return query_subtree_max(a1,a2,i<<1,fa);
        else if(mid<a1)return query_subtree_max(a1,a2,(i<<1)|1,fa);
        else return max(query_subtree_max(a1,mid,i<<1,fa),query_subtree_max(mid+1,a2,(i<<1)|1,fa));
    }
    
    int query_max(int x1,int x2,int y1,int y2,int i)
    {
        if(tree[i].left==x1&&tree[i].right==x2)return query_subtree_max(y1,y2,1,i);
        int mid=tree[i].mid();
        if(x2<=mid)return query_max(x1,x2,y1,y2,i<<1);
        else if(mid<x1)return query_max(x1,x2,y1,y2,(i<<1)|1);
        else return max(query_max(x1,mid,y1,y2,i<<1),query_max(mid+1,x2,y1,y2,(i<<1)|1));
    }
    
    int main()
    {
        int t,cas=1;
        scanf("%d",&t);
        while(t--)
        {
           int m,a,x,y,r;
           scanf("%d",&n);
           build(1,n,1);
           for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                {
                    scanf("%d",&a);
                    update(i,j,a);
                }
            printf("Case #%d:
    ",cas++);
            scanf("%d",&m);
            while(m--)
            {
                scanf("%d%d%d",&x,&y,&r);
                int x1=max(1,x-r/2);
                int x2=min(n,x+r/2);
                int y1=max(1,y-r/2);
                int y2=min(n,y+r/2);
                int maxx=query_max(x1,x2,y1,y2,1);
                int minn=query_min(x1,x2,y1,y2,1);
                int ans=(minn+maxx)>>1;
                printf("%d
    ",ans);
                update(x,y,ans);
            }
        }
        return 0;
    }
    


    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    ghm一般规则
    沃尔玛强推RFID内外交困:供应商阳奉阴违
    电子商务物流解决方案
    database url
    物流中新技术应用的必要性
    美国物流管理协会更名标志全球物流进入供应链时代
    业内专家激辩物流挑战与机遇
    问的智慧
    调查报告:2003年物流信息化现状及挑战
    查找在菜单里提交的报表所在职责
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4818959.html
Copyright © 2011-2022 走看看