二维完整代码
//By AcerMo
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int M=305;
int n,m,q;
int f[M][M][10][10];
inline void read(int &x)
{
x=0;char ch=getchar();
while (!isdigit(ch)) ch=getchar();
while (isdigit(ch)) x=x*10+ch-'0',ch=getchar();
return ;
}
inline void write(int x)
{
if (x>9) write(x/10);
putchar(x%10+'0');
return ;
}
inline void pre()
{
int t=log2(n),e=log2(m);
for (int i=0;i<=t;i++)
for (int k=0;k<=e;k++)
{
if (i==0&&k==0) continue;
for (int j=1;j<=n-(1<<i)+1;j++)
for (int p=1;p<=m-(1<<k)+1;p++)
if (i==0)
f[j][p][i][k]=max(f[j][p][i][k-1],f[j][p+(1<<(k-1))][i][k-1]);
else
f[j][p][i][k]=max(f[j][p][i-1][k],f[j+(1<<(i-1))][p][i-1][k]);
}
return ;
}
inline int que(int x,int y,int l,int r)
{
int t=log2(l-x+1);
int e=log2(r-y+1);
int a1=max(f[x][y][t][e],f[l-(1<<t)+1][r-(1<<e)+1][t][e]);
int a2=max(f[l-(1<<t)+1][y][t][e],f[x][r-(1<<e)+1][t][e]);
return max(a1,a2);
}
signed main()
{
read(n);read(m);
for (int i=1;i<=n;i++)
for (int k=1;k<=m;k++)
read(f[i][k][0][0]);
pre();read(q);
while (q--)
{
int x,y;read(x),read(y);
int l,r;read(l),read(r);
write(que(x,y,l,r));puts("");
}
return 0;
}
|