Hdu-4819-mosaic (two-dimensional line segment tree)

Source: Internet
Author: User

Problem DescriptionThe God of sheep decides to pixelate some pictures (i.e., change them into pictures with mosaic). Here's how he's gonna make it:for each picture, he divides the picture to n x n cells, where each cell is assigned a C Olor value. Then he chooses a cell, and checks the color values of the L x L region whose center are at this specific cell. Assuming the maximum and minimum color values in the region is A and B respectively, he'll replace the color value in th e 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 is 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 was the color value of the cell (I, j) of the picture. The Color values are nonnegative integers and would 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≤l I < 10000, Li is odd). This means the God of sheep would 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, a query (2, 3, 3) means changing the color value of the cell at the second row and the third column Accordin G to region (1, 2) (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4). Notice that if the A is not a entirely inside the picture, but cells is both Onsidered.

Note that the God of sheep would do the replacement one by one in the order given in the Input.
Outputfor, 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

Field=problem&key=2013%20asia%20regional%20changchun%20&source=1&searchmode=source ">2013 Asia Regional Changchun


Idea: two-dimensional line segment tree, single point update.


#include <stdio.h> #define INF 1000000001#define min (A, b) (a<b?) A:B) #define MAX (A, b) (a>b? A:B) int mx[3200][3200],mn[3200][3200],n,minnum,maxnum;void buildy (int x,int idx,int s,int e,bool flag)// A flag of 1 indicates that the first dimension is a leaf node.            0 indicates that the first dimension is not a leaf node {if (s==e) {if (flag) {scanf ("%d", &mn[x][idx]);        MX[X][IDX]=MN[X][IDX];        }} else {int mid= (s+e) >>1;        Buildy (X,idx<<1,s,mid,flag);        Buildy (X,idx<<1|1,mid+1,e,flag);        Mx[x][idx]=max (mx[x][idx<<1],mx[x][idx<<1|1]);    Mn[x][idx]=min (mn[x][idx<<1],mn[x][idx<<1|1]);        } if (!flag)//The first dimension is not a leaf node to update the first dimension {Mx[x][idx]=max (Mx[x<<1][idx],mx[x<<1|1][idx]);    Mn[x][idx]=min (Mn[x<<1][idx],mn[x<<1|1][idx]);        }}void Build (int idx,int s,int e) {if (s!=e) {int mid= (s+e) >>1;        Build (Idx<<1,s,mid);        Build (Idx<<1|1,mid+1,e);    Buildy (idx,1,1,n,0); } ElSe buildy (idx,1,1,n,1);}    void Updatey (int x,int idx,int s,int e,int pos,int val,bool flag) {if (s==e) Mn[x][idx]=mx[x][idx]=val;        else {int mid= (s+e) >>1;        if (pos<=mid) Updatey (X,idx<<1,s,mid,pos,val,flag);        else Updatey (X,idx<<1|1,mid+1,e,pos,val,flag);        Mx[x][idx]=max (mx[x][idx<<1],mx[x][idx<<1|1]);    Mn[x][idx]=min (mn[x][idx<<1],mn[x][idx<<1|1]);        } if (!flag)//The first dimension is not a leaf node to update the first dimension {Mx[x][idx]=max (Mx[x<<1][idx],mx[x<<1|1][idx]);    Mn[x][idx]=min (Mn[x<<1][idx],mn[x<<1|1][idx]);    }}void Update (int idx,int s,int e,int pos,int posy,int val) {if (s==e) Updatey (idx,1,1,n,posy,val,1);        else {int mid= (s+e) >>1;        if (pos<=mid) update (idx<<1,s,mid,pos,posy,val);        else update (idx<<1|1,mid+1,e,pos,posy,val);    Updatey (idx,1,1,n,posy,val,0); }}void queryy (int x,int idx,int s,int e,int l,int R) {if (s==l && e==r) {minnum=min (minnum,mn[x][idx]);    Maxnum=max (Maxnum,mx[x][idx]);        } else {int mid= (s+e) >>1;        if (r<=mid) queryy (x,idx<<1,s,mid,l,r);        else if (l>mid) queryy (x,idx<<1|1,mid+1,e,l,r);            else {queryy (x,idx<<1,s,mid,l,mid);        Queryy (X,IDX&LT;&LT;1|1,MID+1,E,MID+1,R);    }}}void query (int idx,int s,int e,int l,int r,int ly,int ry) {if (s==l && e==r) queryy (Idx,1,1,n,ly,ry);        else {int mid= (s+e) >>1;        if (r<=mid) query (Idx<<1,s,mid,l,r,ly,ry);        else if (l>mid) query (Idx<<1|1,mid+1,e,l,r,ly,ry);            else {query (idx<<1,s,mid,l,mid,ly,ry);        Query (Idx<<1|1,mid+1,e,mid+1,r,ly,ry);    }}}int Main () {int t,i,q,a,b,c,l,r,ly,ry,t;    scanf ("%d", &t);        for (i=1;i<=t;i++) {scanf ("%d", &n);        Build (1,1,n);        scanf ("%d", &q); printf ("Case #%d:\n ", i);            while (q--) {scanf ("%d%d%d", &a,&b,&c);            L=max (a-c/2,1);            R=min (A+c/2,n);            Ly=max (b-c/2,1);            Ry=min (B+c/2,n);            Minnum=inf;            Maxnum=-inf;            Query (1,1,n,l,r,ly,ry);            t= (Minnum+maxnum)/2;            printf ("%d\n", t);        Update (1,1,N,A,B,T); }    }}


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.