HDOJ 4819 Mosaic

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   java   io   strong   


裸的二維線段樹。。。。


MosaicTime Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 494    Accepted Submission(s): 177


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 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. 
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 
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn=1000;const int INF=(1<<30);int xL,xR,yL,yR,Value;int Max[maxn<<2][maxn<<2],Min[maxn<<2][maxn<<2];int n,mat[maxn][maxn];int minv,maxv;void pushup(int xrt,int rt){Max[xrt][rt]=max(Max[xrt][rt<<1],Max[xrt][rt<<1|1]);Min[xrt][rt]=min(Min[xrt][rt<<1],Min[xrt][rt<<1|1]);}void buildY(int xrt,int x,int l,int r,int rt){if(l==r){if(x==-1){Max[xrt][rt]=max(Max[xrt<<1][rt],Max[xrt<<1|1][rt]);Min[xrt][rt]=min(Min[xrt<<1][rt],Min[xrt<<1|1][rt]);}else{Max[xrt][rt]=Min[xrt][rt]=mat[x][l];}return ;}int m=(l+r)/2;buildY(xrt,x,lson); buildY(xrt,x,rson);pushup(xrt,rt);}void buildX(int l,int r,int rt){if(l==r){buildY(rt,l,1,n,1);return ;}int m=(l+r)/2;buildX(lson); buildX(rson);buildY(rt,-1,1,n,1);}void updateY(int xrt,int x,int l,int r,int rt){if(l==r){if(x==-1){Max[xrt][rt]=max(Max[xrt<<1][rt],Max[xrt<<1|1][rt]);Min[xrt][rt]=min(Min[xrt<<1][rt],Min[xrt<<1|1][rt]);}else{Max[xrt][rt]=Min[xrt][rt]=Value;}return ;}int m=(l+r)/2;if(yL<=m) updateY(xrt,x,lson);else updateY(xrt,x,rson);pushup(xrt,rt);}void updateX(int l,int r,int rt){if(l==r){updateY(rt,l,1,n,1);return ;}int m=(l+r)/2;if(xL<=m) updateX(lson);else updateX(rson);updateY(rt,-1,1,n,1);}void queryY(int xrt,int l,int r,int rt){if(yL<=l&&r<=yR){minv=min(minv,Min[xrt][rt]);maxv=max(maxv,Max[xrt][rt]);return ;}int m=(l+r)/2;if(yL<=m) queryY(xrt,lson);if(yR>m) queryY(xrt,rson);}void queryX(int l,int r,int rt){if(xL<=l&&r<=xR){queryY(rt,1,n,1);return ;}int m=(l+r)/2;if(xL<=m) queryX(lson);if(xR>m) queryX(rson);}int main(){int T_T,cas=1;scanf("%d",&T_T);while(T_T--){scanf("%d",&n);for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)scanf("%d",&mat[i][j]);buildX(1,n,1);int q;scanf("%d",&q);printf("Case #%d:\n",cas++);while(q--){int x,y,l;scanf("%d%d%d",&x,&y,&l);l=l/2;xL=max(1,x-l);xR=min(n,x+l);yL=max(1,y-l);yR=min(n,y+l);minv=INF; maxv=-INF;queryX(1,n,1);Value=(minv+maxv)/2;printf("%d\n",Value);xL=x;yL=y;updateX(1,n,1);}}return 0;}



HDOJ 4819 Mosaic

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.