HDU 4819 Mosaic D-section Tree

Source: Internet
Author: User

pid=4819 "> Click to open Link
MosaicTime limit:10000/5000 MS (java/others) Memory limit:102400/102400 K (java/others)
Total submission (s): 657 Accepted Submission (s): 248


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

Source2013 Asia Regional Changchun
give you a n*n matrix and then give you m a request. Each request has x,y,r this three number. Represents a maximum and a minimum value in a matrix with a length of L in the center of Line x, Y, and changes (x, y) to/2 (max + min). A one-dimensional representation of a row in a two-dimensional line segment tree, and one-dimensional representation of the column.

1859ms50772k#include<stdio.h> #include <algorithm> #define M 1007#define EPS 1e-4#define inf    0x3f3f3f3fusing 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&LT;&LT;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) | |);}    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&AMP;&AMP;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&LT;&LT;1,FA); else if (MID&LT;A1) return Query_subtree_min (A1, a2, (i<<1) |1,FA); else return min (query_subtree_min (A1,MID,I&LT;&LT;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) | | |); else return min (query_min (x1,mid,y1,y2,i<<1), Query_min (Mid+1,x2,y1,y2, (i<<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&LT;&LT;1,FA);    else if (MID&LT;A1) return Query_subtree_max (A1,A2, (i<<1) |1,FA); else return Max (Query_subtree_max (A1,MID,I&LT;&LT;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) | | |); else return Max (Query_max (x1,mid,y1,y2,i<<1), Query_max (Mid+1,x2,y1,y2, (i<<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:\n", 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\n", ans);        Update (X,y,ans); }} return 0;}


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

HDU 4819 Mosaic D-section 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.