P5781 [IOI 2019] 矩形区域
Genius_Star · · 题解
大牛逼题,但是刚开始读错题了,模拟赛题面是“矩形内的位置的愉悦值严格小于其边界上的愉悦值”,你能不读错???以为是矩形
思路:
暴力无法通过,考虑找一些性质。
这个矩形太大了,不会找性质,考虑缩放到每一行每一列,只考虑这一行的性质,容易发现:
-
当第
i 行的[l, r] 合法时,要么a_{i, r + 1} 是a_{i, l - 1} 右侧第一个大于它的,要么a_{i, l - 1} 是a_{i, r+ 1} 左侧第一个大于它的。 -
反证法可以轻易证明,且对于列也有一样的性质。
那么来考虑这个大矩形,每行每列都满足这样的性质就是判定的一个充要条件;于是我们可以先找到所有的行
先考虑枚举这个大矩形的左右边界
于是考虑再次基础上加上列的性质,当我们枚举
- 即对于所有
r 列的合法段[x, y] ,更新f'_{x, y} = f_{x, y} ;否则f'_{x, y} = n + 1 ;由于不能暴力赋值,所以我们可以记一个时间戳来进行优化更新。
那么对于所有
找合法段跑单调栈即可。
每个
完整代码:
#include<bits/stdc++.h>
#define ls(k) k << 1
#define rs(k) k << 1 | 1
#define fi first
#define se second
#define add(x, y) ((x + y >= mod) ? (x + y - mod) : (x + y))
#define dec(x, y) ((x - y < 0) ? (x - y + mod) : (x - y))
#define popcnt(x) __builtin_popcount(x)
#define open(s1, s2) freopen(s1, "r", stdin), freopen(s2, "w", stdout);
using namespace std;
typedef __int128 __;
typedef long double lb;
typedef double db;
typedef unsigned long long ull;
typedef long long ll;
bool Begin;
const int N = 2520;
inline ll read(){
ll x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-')
f = -1;
c = getchar();
}
while(c >= '0' && c <= '9'){
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return x * f;
}
inline void write(ll x){
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
ll ans;
int n, m, cnt, top;
int stk[N];
int a[N][N], f[N][N], tim[N][N];
pair<int, int> ac[N];
vector<int> S[N][N];
inline void solve(int *h, int n){
cnt = top = 0;
for(int i = 1; i <= n; ++i){
while(top && h[i] > h[stk[top]]){
if(i > stk[top] + 1)
ac[++cnt] = {stk[top] + 1, i - 1}; // l < r
--top;
}
if(top){
if(i > stk[top] + 1)
ac[++cnt] = {stk[top] + 1, i - 1}; // l > r
if(h[i] == h[stk[top]])
--top;
}
stk[++top] = i;
}
}
inline void solve(int l, int r, int x, int y){
int siz = 0;
for(int i = x - 1; i <= y + 1; ++i)
a[0][++siz] = a[i][r];
solve(a[0], siz);
for(int i = 1; i <= cnt; ++i){
int L = ac[i].fi + x - 2, R = ac[i].se + x - 2;
if(f[L][R] <= l)
++ans;
}
}
bool End;
int main(){
freopen("field.in", "r", stdin);
freopen("field.out", "w", stdout);
n = read(), m = read();
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
a[i][j] = read();
for(int i = 2; i < n; ++i){
solve(a[i], m);
for(int j = 1; j <= cnt; ++j)
S[ac[j].fi][ac[j].se].push_back(i);
}
for(int r = 2; r < m; ++r){
for(int i = 1; i <= n; ++i)
a[0][i] = a[i][r];
solve(a[0], n);
for(int i = 1; i <= cnt; ++i){
if(tim[ac[i].fi][ac[i].se] + 1 < r)
f[ac[i].fi][ac[i].se] = r;
tim[ac[i].fi][ac[i].se] = r;
}
for(int l = 2; l <= r; ++l){
if(S[l][r].empty())
continue;
int lst = S[l][r][0], siz = S[l][r].size();
for(int i = 1; i < siz; ++i){
if(S[l][r][i] > S[l][r][i - 1] + 1){
solve(l, r, lst, S[l][r][i - 1]);
lst = S[l][r][i];
}
}
solve(l, r, lst, S[l][r][siz - 1]);
}
}
write(ans);
//cerr << '\n' << abs(&Begin - &End) / 1048576 << "MB";
return 0;
}