二维RMQ模板(hdu2888)
#include<bits/stdc++.h>
using namespace std;
const int N=305;
const int M=9;
int maze[N][N];
int maxx[N][N][M][M];
int n,m;
void pre_gao(){
int up1=0,up2=0;
while((1<<(up1+1))<n)up1++;
while((1<<(up2+1))<m)up2++;
for(int k1=0;k1<=up1;k1++)
for(int k2=0;k2<=up2;k2++){
if(k1==0&&k2==0)continue;
for(int i=0;i+(1<<k1)-1<n;i++)
for(int j=0;j+(1<<k2)-1<m;j++){
if(k1==0)maxx[i][j][k1][k2]=max(maxx[i][j][k1][k2-1],maxx[i][j+(1<<(k2-1))][k1][k2-1]);
else maxx[i][j][k1][k2]=max(maxx[i][j][k1-1][k2],maxx[i+(1<<(k1-1))][j][k1-1][k2]);
}
}
}
int query(int x1,int y1,int x2,int y2){
int up1=0,up2=0;
while((1<<(up1+1))<(x2-x1+1))up1++;
while((1<<(up2+1))<(y2-y1+1))up2++;
int m1=maxx[x1][y1][up1][up2];
int m2=maxx[x2-(1<<up1)+1][y1][up1][up2];
int m3=maxx[x1][y2-(1<<up2)+1][up1][up2];
int m4=maxx[x2-(1<<up1)+1][y2-(1<<up2)+1][up1][up2];
return max(max(m1,m2),max(m3,m4));
}
int main()
{
while(~scanf("%d%d",&n,&m)){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&maze[i][j]);
maxx[i][j][0][0]=maze[i][j];
}
}
pre_gao();
int q;
scanf("%d",&q);
while(q--){
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1--;y1--;x2--;y2--;
int _max=max(max(maze[x1][y1],maze[x2][y2]),max(maze[x1][y2],maze[x2][y1]));
int _max1=query(x1,y1,x2,y2);
printf("%d ",_max1);
if(_max==_max1){
puts("yes");
}
else puts("no");
}
}
return 0;
}
不疯魔不成活