HDU 2888 Check Corners(简单二维RMQ)
http://acm.hdu.edu.cn/showproblem.php?pid=2888
题意:
给定一个n * m的矩阵,再给定q个询问,每次询问(r1,c1)为左上角,(r2,c2)为右下角的子矩形的最大值,并且判断该最大值是否出现在了这个子矩阵的4个顶角上?
分析:
基本的二维RMQ应用.
注意题目中存的矩阵是无符号整数的矩阵.
AC代码:3625ms
<span style="font-size:18px;">#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN =305; unsigned int val[MAXN][MAXN]; unsigned int dmax[MAXN][MAXN][9][9]; void initRMQ(int n,int m) { for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) dmax[i][j][0][0] = val[i][j]; for(int ii=0;(1<<ii)<=n;ii++) for(int jj=0;(1<<jj)<=m;jj++) if(ii+jj) for(int i=1;i+(1<<ii)-1<=n;i++) for(int j=1;j+(1<<jj)-1<=m;j++) if(ii) dmax[i][j][ii][jj]=max(dmax[i][j][ii-1][jj] , dmax[i+(1<<(ii-1))][j][ii-1][jj]); else dmax[i][j][ii][jj]=max(dmax[i][j][ii][jj-1] , dmax[i][j+(1<<(jj-1))][ii][jj-1]); } unsigned int getMax(int x1,int y1,int x2,int y2) { int k1=0; while((1<<(k1+1))<=x2-x1+1)k1++; int k2=0; while((1<<(k2+1))<=y2-y1+1)k2++; x2 = x2-(1<<k1)+1; y2 = y2-(1<<k2)+1; return max(max(dmax[x1][y1][k1][k2] ,dmax[x1][y2][k1][k2] ) , max(dmax[x2][y1][k1][k2] , dmax[x2][y2][k1][k2])); } int main() { int n,m; while(scanf("%d%d",&n,&m)==2&&n&&m) { for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%ud",&val[i][j]); initRMQ(n,m); int q; scanf("%d",&q); while(q--) { int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); unsigned int max_num = getMax(x1,y1,x2,y2); bool ok=false; if(val[x1][y1]==max_num || val[x1][y2]==max_num || val[x2][y1]==max_num ||val[x2][y2]==max_num) ok=true; printf("%d %s\n",max_num,ok?"yes":"no"); } } return 0; } </span>