标签:des style blog http color io os ar java
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
Case #1: 5 6 3 4 6
//1859MS 50772K #include<stdio.h> #include<algorithm> #define M 1007 #define eps 1e-4 #define inf 0x3f3f3f3f using 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; }
标签:des style blog http color io os ar java
原文地址:http://blog.csdn.net/crescent__moon/article/details/40342497