矩阵最值
题目链接:YbtOJ
解题思路
二维st表模板题。
code
#include<iostream>
#include<cstdio>
#include<cmath>
#define int long long
using namespace std;
int n,m,T;
int a[260][260];
int f[260][260][21][21];
signed main()
{
cin>>n>>m>>T;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%lld",&a[i][j]);
for(int k=0;k<=log2(n);k++)
for(int h=0;h<=log2(m);h++)
for(int i=1;i+(1<<k)-1<=n;i++)
for(int j=1;j+(1<<h)-1<=m;j++)
{
if(!k&&!h)
f[i][j][k][h]=a[i][j];
if(k)
f[i][j][k][h]=max(f[i][j][k-1][h],f[i+(1<<(k-1))][j][k-1][h]);
if(h)
f[i][j][k][h]=max(f[i][j][k][h-1],f[i][j+(1<<(h-1))][k][h-1]);
}
while(T--)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int k1=log2(x2-x1+1);
int 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])));
}
}