Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 213 Accepted Submission(s): 50
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2014/5/13 23:21:07 4 File Name :E:\2014ACM\专题学习\数据结构\二维线段树\HDU4819.cpp 5 ************************************************ */ 6 7 #include <stdio.h> 8 #include <string.h> 9 #include <iostream> 10 #include <algorithm> 11 #include <vector> 12 #include <queue> 13 #include <set> 14 #include <map> 15 #include <string> 16 #include <math.h> 17 #include <stdlib.h> 18 #include <time.h> 19 using namespace std; 20 const int INF = 0x3f3f3f3f; 21 const int MAXN = 1010; 22 struct Nodey 23 { 24 int l,r; 25 int Max,Min; 26 }; 27 int locy[MAXN],locx[MAXN]; 28 struct Nodex 29 { 30 int l,r; 31 Nodey sty[MAXN*4]; 32 void build(int i,int _l,int _r) 33 { 34 sty[i].l = _l; 35 sty[i].r = _r; 36 sty[i].Max = -INF; 37 sty[i].Min = INF; 38 if(_l == _r) 39 { 40 locy[_l] = i; 41 return; 42 } 43 int mid = (_l + _r)/2; 44 build(i<<1,_l,mid); 45 build((i<<1)|1,mid+1,_r); 46 } 47 int queryMin(int i,int _l,int _r) 48 { 49 if(sty[i].l == _l && sty[i].r == _r) 50 return sty[i].Min; 51 int mid = (sty[i].l + sty[i].r)/2; 52 if(_r <= mid)return queryMin(i<<1,_l,_r); 53 else if(_l > mid)return queryMin((i<<1)|1,_l,_r); 54 else return min(queryMin(i<<1,_l,mid),queryMin((i<<1)|1,mid+1,_r)); 55 } 56 int queryMax(int i,int _l,int _r) 57 { 58 if(sty[i].l == _l && sty[i].r == _r) 59 return sty[i].Max; 60 int mid = (sty[i].l + sty[i].r)/2; 61 if(_r <= mid)return queryMax(i<<1,_l,_r); 62 else if(_l > mid)return queryMax((i<<1)|1,_l,_r); 63 else return max(queryMax(i<<1,_l,mid),queryMax((i<<1)|1,mid+1,_r)); 64 } 65 }stx[MAXN*4]; 66 int n; 67 void build(int i,int l,int r) 68 { 69 stx[i].l = l; 70 stx[i].r = r; 71 stx[i].build(1,1,n); 72 if(l == r) 73 { 74 locx[l] = i; 75 return; 76 } 77 int mid = (l+r)/2; 78 build(i<<1,l,mid); 79 build((i<<1)|1,mid+1,r); 80 } 81 //修改值 82 void Modify(int x,int y,int val) 83 { 84 int tx = locx[x]; 85 int ty = locy[y]; 86 stx[tx].sty[ty].Min = stx[tx].sty[ty].Max = val; 87 for(int i = tx;i;i >>= 1) 88 for(int j = ty;j;j >>= 1) 89 { 90 if(i == tx && j == ty)continue; 91 if(j == ty) 92 { 93 stx[i].sty[j].Min = min(stx[i<<1].sty[j].Min,stx[(i<<1)|1].sty[j].Min); 94 stx[i].sty[j].Max = max(stx[i<<1].sty[j].Max,stx[(i<<1)|1].sty[j].Max); 95 } 96 else 97 { 98 stx[i].sty[j].Min = min(stx[i].sty[j<<1].Min,stx[i].sty[(j<<1)|1].Min); 99 stx[i].sty[j].Max = max(stx[i].sty[j<<1].Max,stx[i].sty[(j<<1)|1].Max); 100 } 101 } 102 } 103 int queryMin(int i,int x1,int x2,int y1,int y2) 104 { 105 if(stx[i].l == x1 && stx[i].r == x2) 106 return stx[i].queryMin(1,y1,y2); 107 int mid = (stx[i].l + stx[i].r)/2; 108 if(x2 <= mid)return queryMin(i<<1,x1,x2,y1,y2); 109 else if(x1 > mid)return queryMin((i<<1)|1,x1,x2,y1,y2); 110 else return min(queryMin(i<<1,x1,mid,y1,y2),queryMin((i<<1)|1,mid+1,x2,y1,y2)); 111 } 112 int queryMax(int i,int x1,int x2,int y1,int y2) 113 { 114 if(stx[i].l == x1 && stx[i].r == x2) 115 return stx[i].queryMax(1,y1,y2); 116 int mid = (stx[i].l + stx[i].r)/2; 117 if(x2 <= mid)return queryMax(i<<1,x1,x2,y1,y2); 118 else if(x1 > mid)return queryMax((i<<1)|1,x1,x2,y1,y2); 119 else return max(queryMax(i<<1,x1,mid,y1,y2),queryMax((i<<1)|1,mid+1,x2,y1,y2)); 120 } 121 122 123 int main() 124 { 125 //freopen("in.txt","r",stdin); 126 //freopen("out.txt","w",stdout); 127 int T; 128 scanf("%d",&T); 129 int iCase = 0; 130 while(T--) 131 { 132 iCase++; 133 printf("Case #%d:\n",iCase); 134 scanf("%d",&n); 135 build(1,1,n); 136 for(int i = 1;i <= n;i++) 137 for(int j = 1;j <= n;j++) 138 { 139 int a; 140 scanf("%d",&a); 141 Modify(i,j,a); 142 } 143 int q; 144 int x,y,L; 145 scanf("%d",&q); 146 while(q--) 147 { 148 scanf("%d%d%d",&x,&y,&L); 149 int x1 = max(x - L/2,1); 150 int x2 = min(x + L/2,n); 151 int y1 = max(y - L/2,1); 152 int y2 = min(y + L/2,n); 153 int Max = queryMax(1,x1,x2,y1,y2); 154 int Min = queryMin(1,x1,x2,y1,y2); 155 int t = (Max+Min)/2; 156 printf("%d\n",t); 157 Modify(x,y,t); 158 } 159 } 160 return 0; 161 }