標籤:des style blog http color io os ar java
點擊開啟連結
Mosaic
Time 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 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
給你一個n*n的矩陣,然後給你m個請求,每個請求有x,y,r這個三個數,代表在以第x行第y列為中心,長度為l的矩陣中,找一個最大值和一個最小值,並將(x,y)處改為(最大值+最小值)/2.二維線段樹中的一維代表行,另一維代表列。
//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<<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)|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&&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<<1,fa); else if(mid<a1)return query_subtree_min(a1,a2,(i<<1)|1,fa); else return min(query_subtree_min(a1,mid,i<<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)|1); else return min(query_min(x1,mid,y1,y2,i<<1),query_min(mid+1,x2,y1,y2,(i<<1)|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<<1,fa); else if(mid<a1)return query_subtree_max(a1,a2,(i<<1)|1,fa); else return max(query_subtree_max(a1,mid,i<<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)|1); else return max(query_max(x1,mid,y1,y2,i<<1),query_max(mid+1,x2,y1,y2,(i<<1)|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;}
HDU 4819 Mosaic 二維線段樹