Check Corners Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3247 Accepted Submission(s): 1173 ...
Check Corners
Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3247 Accepted Submission(s): 1173
Input There are multiple test cases.
For each test case, the first line contains two integers m, n (1 <= m, n <= 300), which is the size of the row and column of the matrix, respectively. The next m lines with n integers each gives the elements of the matrix which fit in non-negative 32-bit integer.
The next line contains a single integer Q (1 <= Q <= 1,000,000), the number of queries. The next Q lines give one query on each line, with four integers r1, c1, r2, c2 (1 <= r1 <= r2 <= m, 1 <= c1 <= c2 <= n), which are the indices of the upper-left corner and lower-right corner of the sub-matrix in question.
Output For each test case, print Q lines with two numbers on each line, the required maximum integer and the result of the “Check corners” using “yes” or “no”. Separate the two parts with a single space.
Sample Input 4 4 4 4 10 7 2 13 9 11 5 7 8 20 13 20 8 2 4 1 1 4 4 1 1 3 3 1 3 3 4 1 1 1 1
Sample Output 20 no 13 no 20 yes 4 yes
Source 2009 Multi-University Training Contest 9 - Host by HIT
- 二維區間max,打二維ST表
- dp[i][j][e][f]表明從矩陣左上角(i,j)開始寬度範圍是2^e,高度範圍是2^f的矩形
1 #include <iostream> 2 #include <string> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <climits> 7 #include <cmath> 8 #include <vector> 9 #include <queue> 10 #include <stack> 11 #include <set> 12 #include <map> 13 using namespace std; 14 typedef long long LL ; 15 typedef unsigned long long ULL ; 16 const int maxn = 1e5 + 10 ; 17 const int inf = 0x3f3f3f3f ; 18 const int npos = -1 ; 19 const int mod = 1e9 + 7 ; 20 const int mxx = 100 + 5 ; 21 const double eps = 1e-6 ; 22 const double PI = acos(-1.0) ; 23 24 int max4(int a, int b, int c, int d){ 25 return max(max(a,b),max(c,d)); 26 } 27 int m, n, fac[9], dp[310][310][9][9]; 28 int X1, Y1, X2, Y2, ans, mx, q; 29 int main(){ 30 // freopen("in.txt","r",stdin); 31 // freopen("out.txt","w",stdout); 32 for(int i=0;i<9;i++) 33 fac[i]=(1<<i); 34 while(~scanf("%d %d",&m,&n)){ 35 for(int i=1;i<=m;i++) 36 for(int j=1;j<=n;j++) 37 scanf("%d",&dp[i][j][0][0]); 38 int rk=(int)(log((double)m)/log(2.0)); 39 int ck=(int)(log((double)n)/log(2.0)); 40 for(int e=0;e<=rk;e++) 41 for(int f=0;f<=ck;f++) 42 if(e || f) 43 for(int i=1;i+fac[e]-1<=m;i++) 44 for(int j=1;j+fac[f]-1<=n;j++) 45 if(!e) 46 dp[i][j][e][f]=max(dp[i][j][e][f-1],dp[i][j+fac[f-1]][e][f-1]); 47 else if(!f) 48 dp[i][j][e][f]=max(dp[i][j][e-1][f],dp[i+fac[e-1]][j][e-1][f]); 49 else 50 dp[i][j][e][f]=max4(dp[i][j][e-1][f-1],dp[i+fac[e-1]][j][e-1][f-1],dp[i][j+fac[f-1]][e-1][f-1],dp[i+fac[e-1]][j+fac[f-1]][e-1][f-1]); 51 scanf("%d",&q); 52 while(q--){ 53 ans=0; 54 scanf("%d %d %d %d",&X1,&Y1,&X2,&Y2); 55 rk=(int)(log((double)(X2-X1+1))/log(2.0)); 56 ck=(int)(log((double)(Y2-Y1+1))/log(2.0)); 57 mx=max4(dp[X1][Y1][rk][ck],dp[X2-fac[rk]+1][Y1][rk][ck],dp[X1][Y2-fac[ck]+1][rk][ck],dp[X2-fac[rk]+1][Y2-fac[ck]+1][rk][ck]); 58 if(mx==dp[X1][Y1][0][0]|| 59 mx==dp[X1][Y2][0][0]|| 60 mx==dp[X2][Y1][0][0]|| 61 mx==dp[X2][Y2][0][0]){ 62 ans=1; 63 } 64 printf("%d %s\n",mx,ans?"yes":"no"); 65 } 66 } 67 return 0; 68 }