题目链接:矩阵最值
分析
二维ST表模板题。一维扩展一下就可。
上代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int n,m,q;
int a[300][300],f[300][300][20][20];
void prest()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
f[i][j][0][0]=a[i][j];
}
}
for(int k=0;k<=log2(n);k++)
{
for(int h=0;h<=log2(m);h++)
{
if(!k&&!h) continue;
for(int i=1;i+(1<<k)-1<=n;i++)
{
for(int j=1;j+(1<<h)-1<=m;j++)
{
if(k)
f[i][j][k][h]=max(f[i][j][k-1][h],f[i+(1<<(k-1))][j][k-1][h]);
else if(h)
f[i][j][k][h]=max(f[i][j][k][h-1],f[i][j+(1<<(h-1))][k][h-1]);
}
}
}
}
}
int main()
{
cin>>n>>m>>q;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
}
}
prest();
while(q--)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int k1,k2;
k1=log2(x2-x1+1);
k2=log2(y2-y1+1);
printf("%d\n",max(max(f[x1][y1][k1][k2],f[x2-(1<<k1)+1][y1][k1][k2]),
max(f[x1][y2-(1<<k2)+1][k1][k2],f[x2-(1<<k1)+1][y2-(1<<k2)+1][k1][k2])));
}
return 0;
}