悬线法:
如果把一个极大子矩形按照横坐标的不同切割成多个与y轴平行的线段,那么其中至少有一个悬线。
如果把一个悬线向左右两个方向尽可能的移动,那么就得到了一个矩形,称它为悬线对应的矩形。
悬线对应的矩形不一定是极大子矩形,但所有悬线对应矩形的集合一定包含了极大子矩形的集合
//https://vjudge.net/contest/520308#problem/K
#include<bits/stdc++.h>
#include<unordered_map>
#include<array>
#define ll long long
#define ull unsigned long long
#define all(a) a.begin(),a.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
const ll mod = 2147483648;
const int N = 1e3 + 5;
int n, m, a[N][N], b[N * N];
int l[N][N], r[N][N], h[N][N];
void solve()
{
cin >> n >> m;
for (int i = 1, x; i <= n * m; i++)
{
cin >> x;
b[x] = i;
}
for (int i = 1; i <= n; i++)//题目转化为求左边元素比自己小一,上边元素比自身大m的最大子矩阵
{
for (int j = 1, x; j <= m; j++)
{
cin >> x;
a[i][j] = b[x];
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
l[i][j] = r[i][j] = j;
h[i][j] = 1;
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 2; j <= m; j++)
if (a[i][j - 1] + 1 == a[i][j])
l[i][j] = l[i][j - 1];
for (int j = m - 1; j >= 1; j--)
if (a[i][j + 1] == a[i][j] + 1)
r[i][j] = r[i][j + 1];
}
for (int i = 2; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (a[i][j] == a[i - 1][j] + m)
{
l[i][j] = max(l[i][j], l[i - 1][j]);
r[i][j] = min(r[i][j], r[i - 1][j]);
h[i][j] = h[i - 1][j] + 1;
}
}
}
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
ans = max(ans, (r[i][j] - l[i][j] + 1) * h[i][j]);
cout << ans << '\n';
}
signed main()
{
IOS;
int t = 1;
//cin >> t;
while (t--)
solve();
return 0;
}