HDU-4819-Mosaic(二维线段树)
2016-02-04 11:00
459 查看
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
[align=left]Source[/align]
2013 Asia Regional Changchun
思路:二维线段树,单点更新。
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
Case #1: 5 6 3 4 6
[align=left]Source[/align]
2013 Asia Regional Changchun
思路:二维线段树,单点更新。
#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)//flag为1表示第一维为叶子节点。为0表示第一维不是叶子节点 { 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)//第一维不是叶子节点就更新第一维 { 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)//第一维不是叶子节点就更新第一维 { 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<<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); } } }
相关文章推荐
- git non fast forward rejected && [git]error: RPC failed; result=55, HTTP code = 0
- cookie之之path和domain
- 【hdu2815-Mod Tree】高次同余方程-拓展BadyStepGaintStep
- waitpid传入参数
- 服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
- 例题6-2 UVA 514 - Rails (铁轨)
- quailty's Contest #1 A1 道路修建 Small
- 为何谷歌围棋AI AlphaGo可能会把李世石击溃
- VC6.0 error LNK2001: unresolved external symbol _main解决办法
- Flash, Flex, Air, Flashplayer之间的相互关系是什么?
- wait和notify
- Spark PairRDD 行动与数据分区
- Centos下apache启动时httpd: apr_sockaddr_info_get() failed for 报错
- Connection for controluser as defined in your configuration failed.
- Failed to initialize NVML: GPU access blocked by the operating system
- Jenkins进阶系列之——02email-ext邮件通知模板
- Jenkins进阶系列之——01使用email-ext替换Jenkins的默认邮件通知
- Gmail两步验证
- NSLayoutConstraint
- 简单的AIDL的使用