HDU-2888 Check Corners 二维RMQ

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2888

  模板题。解题思路如下(转载别人写的):

dp[row][col][i][j] 表示[row,row+2^i-1]x[col,col+2^j-1] 二维区间内的最小值
这是RMQ-ST算法的核心: 倍增思想
== min( [row,row+ 2^(i-1)-1]x[col,col+2^j-1], [row+2^(i-1),row+2^i-1]x[col,col+2^j-1] )
= min(dp[row][col][i-1][j], dp[row+(1<<(i-1))][col][i-1][j] )
//y轴不变,x轴二分 (i!=0)

== min( [row,row+2^i-1]x[col,col+2^(j-1)-1],  [row,row+2^i-1]x[col+2^(i-1),col+2^j-1] )
= min(dp[row][col][i][j-1], dp[row][col+(1<<(j-1))][i][j-1] ) 
//x轴不变,y轴二分 (j!=0)
即:
dp[row][col][i][j] = min(dp[row][col][i-1][j], dp[row + (1<<(i-1))][col][i-1][j] )   
             或    = min(dp[row][col][i][j-1], dp[row][col+(1<<(j-1))][i][j-1] )
查询[x1,x2]x[y1,y2]
令 kx = (int)log2(x2-x1+1);
   ky = (int)log2(y2-y1+1);
查询结果为
   m1 = dp[x1][y1][kx][ky]                    = dp[x1][y1][kx][ky];
   m2 = dp[x2-2^kx+1][y1][kx]ky]              = dp[x2-(1<<kx)+1][y1][kx][ky];
   m3 = dp[x1][y2-2^ky+1][kx][ky]             = dp[x1][y2-(1<<ky)+1][kx][ky];
   m4 = dp[x2-2^kx+1][y2-2^ky+1][kx][ky]      = dp[x2-(1<<kx)+1][y2-(1<<ky)+1][kx][ky];

结果 = min(m1,m2,m3,m4)

 

  1 //STATUS:C++_AC_4109MS_30160KB
  2 #include <functional>
  3 #include <algorithm>
  4 #include <iostream>
  5 //#include <ext/rope>
  6 #include <fstream>
  7 #include <sstream>
  8 #include <iomanip>
  9 #include <numeric>
 10 #include <cstring>
 11 #include <cassert>
 12 #include <cstdio>
 13 #include <string>
 14 #include <vector>
 15 #include <bitset>
 16 #include <queue>
 17 #include <stack>
 18 #include <cmath>
 19 #include <ctime>
 20 #include <list>
 21 #include <set>
 22 #include <map>
 23 using namespace std;
 24 //#pragma comment(linker,"/STACK:102400000,102400000")
 25 //using namespace __gnu_cxx;
 26 //define
 27 #define pii pair<int,int>
 28 #define mem(a,b) memset(a,b,sizeof(a))
 29 #define lson l,mid,rt<<1
 30 #define rson mid+1,r,rt<<1|1
 31 #define PI acos(-1.0)
 32 //typedef
 33 typedef long long LL;
 34 typedef unsigned long long ULL;
 35 //const
 36 const int N=310;
 37 const int INF=0x3f3f3f3f;
 38 const int MOD=1e+7,STA=8000010;
 39 const LL LNF=1LL<<60;
 40 const double EPS=1e-8;
 41 const double OO=1e15;
 42 const int dx[4]={-1,0,1,0};
 43 const int dy[4]={0,1,0,-1};
 44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
 45 //Daily Use ...
 46 inline int sign(double x){return (x>EPS)-(x<-EPS);}
 47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
 48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
 49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
 50 template<class T> inline T Min(T a,T b){return a<b?a:b;}
 51 template<class T> inline T Max(T a,T b){return a>b?a:b;}
 52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
 53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
 54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
 55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
 56 //End
 57 int val[N][N],dp[N][N][9][9];
 58 int n,m,Q;
 59 void init()
 60 {
 61     for(int row = 1; row <= n; row++)
 62         for(int col = 1; col <=m; col++)
 63             dp[row][col][0][0] = val[row][col];
 64     int mx = log(double(n)) / log(2.0);
 65     int my = log(double(m)) / log(2.0);
 66     for(int i=0; i<= mx; i++)
 67     {
 68         for(int j = 0; j<=my; j++)
 69         {
 70             if(i == 0 && j ==0) continue;
 71             for(int row = 1; row+(1<<i)-1 <= n; row++)
 72             {
 73                 for(int col = 1; col+(1<<j)-1 <= m; col++)
 74                 {
 75                     if(i == 0)//y轴二分
 76                         dp[row][col][i][j]=max(dp[row][col][i][j-1],dp[row][col+(1<<(j-1))][i][j-1]);
 77                     else//x轴二分
 78                         dp[row][col][i][j]=max(dp[row][col][i-1][j],dp[row+(1<<(i-1))][col][i-1][j]);
 79                 }
 80             }
 81         }
 82     }
 83 }
 84 int RMQ2D(int x1,int y1,int x2,int y2)
 85 {
 86     int kx = log(double(x2-x1+1)) / log(2.0);
 87     int ky = log(double(y2-y1+1)) / log(2.0);
 88     int m1 = dp[x1][y1][kx][ky];
 89     int m2 = dp[x2-(1<<kx)+1][y1][kx][ky];
 90     int m3 = dp[x1][y2-(1<<ky)+1][kx][ky];
 91     int m4 = dp[x2-(1<<kx)+1][y2-(1<<ky)+1][kx][ky];
 92     return max( max(m1,m2) , max(m3,m4));
 93 }
 94 //END
 95 int main()
 96 {
 97  //   freopen("in.txt","r",stdin);
 98     int i,j,ans;
 99     int x1,y1,x2,y2;
100     while(~scanf("%d%d",&n,&m))
101     {
102         for(i=1;i<=n;i++)
103             for(j=1;j<=m;j++)
104                 scanf("%d",&val[i][j]);
105         init();
106         scanf("%d",&Q);
107         while(Q--){
108             scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
109             ans=RMQ2D(x1,y1,x2,y2);
110             if(ans==val[x1][y1] || ans==val[x2][y2]
111                || ans==val[x1][y2] || ans==val[x2][y1]){
112                 printf("%d yes\n",ans);
113             }
114             else printf("%d no\n",ans);
115         }
116     }
117     return 0;
118 }

 

posted @ 2014-03-03 16:54  zhsl  阅读(333)  评论(0编辑  收藏  举报