This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) x.begin(), x.end()
#define mp make_pair
#define mt make_tuple
#define x first
#define y second
#include "rect.h"
namespace my {
const int N = 1010;
int getmax(int a[N][N], int li, int lj, int ri, int rj) {
int ans = -(int)1e9;;
for (int i = li; i <= ri; ++i) {
for (int j = lj; j <= rj; ++j) {
ans = max(ans, a[i][j]);
}
}
return ans;
}
int n, m;
int a[N][N];
int l[N][N], r[N][N], u[N][N], d[N][N];
set<tuple<int, int, int, int>> mem;
void dfs(int li, int lj, int ri, int rj) {
while (1) {
if (ri > n - 2 || rj > m - 2) {
return;
}
int cl = -getmax(l, li, lj, ri, rj);
if (cl < lj - 1) {
return;
}
int cu = -getmax(u, li, lj, ri, rj);
if (cu < li - 1) {
return;
}
int cr = getmax(r, li, lj, ri, rj);
int cd = getmax(d, li, lj, ri, rj);
if (cr <= rj + 1 && cd <= ri + 1) {
break;
}
rj = max(rj, cr - 1);
ri = max(ri, cd - 1);
}
if (!mem.emplace(li, lj, ri, rj).y) {
return;
}
// cout << "add " << li << " " << lj << " " << ri << " " << rj << endl;
dfs(li, lj, ri + 1, rj);
dfs(li, lj, ri, rj + 1);
}
}
ll count_rectangles(vector<vector<int>> _a) {
using namespace my;
n = (int)_a.size(), m = (int)_a[0].size();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
a[i][j] = _a[i][j];
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
l[i][j] = j - 1;
while (l[i][j] >= 0 && a[i][l[i][j]] <= a[i][j]) {
l[i][j] = l[i][l[i][j]];
}
u[i][j] = i - 1;
while (u[i][j] >= 0 && a[u[i][j]][j] <= a[i][j]) {
u[i][j] = u[u[i][j]][j];
}
}
}
for (int i = n - 1; i >= 0; --i) {
for (int j = m - 1; j >= 0; --j) {
r[i][j] = j + 1;
while (r[i][j] < m && a[i][r[i][j]] <= a[i][j]) {
r[i][j] = r[i][r[i][j]];
}
d[i][j] = i + 1;
while (d[i][j] < n && a[d[i][j]][j] <= a[i][j]) {
d[i][j] = d[d[i][j]][j];
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
l[i][j] *= -1;
u[i][j] *= -1;
}
}
// for (int i = 0; i < n; ++i) {
// for (int j = 0; j < m; ++j) {
// cout << l[i][j] << " ";
// }
// cout << endl;
// }
for (int i = 1; i < n - 1; ++i) {
for (int j = 1; j < m - 1; ++j) {
dfs(i, j, i, j);
}
}
return (int)mem.size();
}
#ifdef LC
#include "grader.cpp"
#endif
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |