HDU 4819 Mosaic (two-dimensional line segment tree)

Source: Internet
Author: User
Mosaic Time Limit: 10000/5000 MS (Java/others) memory limit: 102400/102400 K (Java/Others)
Total submission (s): 647 accepted submission (s): 245


Problem descriptionthe 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?
Inputthe 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 integerand 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.
Outputfor 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
 
Source2013 Asia Regional Changchun
Recommendliuyiding | we have carefully selected several similar problems for you: 5065 5064 5062 5061 question: give you a grid of n (n <= 5059. Each grid contains numbers. Then, the Q operation x y l converts the number in the grid of column Y in row X into a square area with the length of the Center side of the grid X and the Y (maximum number + minimum number) /2. then output the modified value. Idea: Two-dimensional line segment tree. That is, each node of a line segment tree is also a line segment tree. Create a line segment tree based on X, and then create a line segment tree based on Y at each node of the X-ray Tree. The update and modification are similar to that of the one-dimensional tree. Look at the code to understand. For details, see the code:
#include<algorithm>#include<iostream>#include<string.h>#include<stdio.h>using namespace std;const int INF=0x3f3f3f3f;const int maxn=810;typedef long long ll;#define lson L,mid,ls#define rson mid+1,R,rsstruct Tree{    int mav[maxn<<2][maxn<<2],miv[maxn<<2][maxn<<2];    int n,m,x,y,val,isleaf,x1,y1,x2,y2,xrt,amav,amiv;    void buildy(int L,int R,int rt)    {        if(L==R)        {            if(isleaf)            {                scanf("%d",&mav[xrt][rt]);                miv[xrt][rt]=mav[xrt][rt];            }            else            {                mav[xrt][rt]=max(mav[xrt<<1][rt],mav[xrt<<1|1][rt]);                miv[xrt][rt]=min(miv[xrt<<1][rt],miv[xrt<<1|1][rt]);            }            return;        }        int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;        buildy(lson);        buildy(rson);        mav[xrt][rt]=max(mav[xrt][ls],mav[xrt][rs]);        miv[xrt][rt]=min(miv[xrt][ls],miv[xrt][rs]);    }    void buildx(int L,int R,int rt)    {        if(L==R)        {            xrt=rt,isleaf=1;            buildy(1,m,1);            return;        }        int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;        buildx(lson);        buildx(rson);        xrt=rt,isleaf=0;        buildy(1,m,1);    }    void build(int nn,int mm)    {        n=nn,m=mm;        buildx(1,n,1);    }    void updatey(int L,int R,int rt)    {        if(L==R)        {            if(isleaf)                mav[xrt][rt]=miv[xrt][rt]=val;            else            {                mav[xrt][rt]=max(mav[xrt<<1][rt],mav[xrt<<1|1][rt]);                miv[xrt][rt]=min(miv[xrt<<1][rt],miv[xrt<<1|1][rt]);            }            return;        }        int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;        if(y<=mid)            updatey(lson);        else            updatey(rson);        mav[xrt][rt]=max(mav[xrt][ls],mav[xrt][rs]);        miv[xrt][rt]=min(miv[xrt][ls],miv[xrt][rs]);    }    void updatex(int L,int R,int rt)    {        if(L==R)        {            xrt=rt,isleaf=1;            updatey(1,m,1);            return;        }        int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;        if(x<=mid)            updatex(lson);        else            updatex(rson);        xrt=rt,isleaf=0;        updatey(1,m,1);    }    void update(int xx,int yy,int v)    {        x=xx,y=yy,val=v;        updatex(1,n,1);    }    void queryy(int L,int R,int rt)    {        if(y1<=L&&R<=y2)        {            amav=max(amav,mav[xrt][rt]);            amiv=min(amiv,miv[xrt][rt]);            return;        }        int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;        if(y1<=mid)            queryy(lson);        if(y2>mid)            queryy(rson);    }    void queryx(int L,int R,int rt)    {        if(x1<=L&&R<=x2)        {            xrt=rt;            queryy(1,m,1);            return;        }        int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;        if(x1<=mid)            queryx(lson);        if(x2>mid)            queryx(rson);    }    void query(int xx1,int yy1,int xx2,int yy2)    {        x1=xx1,y1=yy1,x2=xx2,y2=yy2;        amav=-1,amiv=INF;        queryx(1,n,1);    }} tree;int main(){    int t,cas=1,n,m,i,x,y,len,x1,y1,x2,y2,ans;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        tree.build(n,n);        printf("Case #%d:\n",cas++);        scanf("%d",&m);        for(i=0;i<m;i++)        {            scanf("%d%d%d",&x,&y,&len);            len=len>>1;            x1=max(1,x-len),y1=max(1,y-len);            x2=min(n,x+len),y2=min(n,y+len);            tree.query(x1,y1,x2,y2);            ans=(tree.amav+tree.amiv)>>1;            tree.update(x,y,ans);            printf("%d\n",ans);        }    }    return 0;}


HDU 4819 Mosaic (two-dimensional line segment tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.