이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 2500;
vector<int> where_h[N][N], where_v[N][N];
int jump_h[N][N][2], jump_v[N][N][2];
bool check(vector<int> &s, int a, int b) {
return (int) (upper_bound(s.begin(), s.end(), b) - lower_bound(s.begin(), s.end(), a)) == b - a + 1;
}
long long count_rectangles(vector<vector<int>> a) {
int n = a.size(), m = a[0].size();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
for (int k = 0; k < 2; ++k) {
jump_h[i][j][0] = jump_v[i][j][0] = -1;
}
}
}
for (int i = 0; i < n; ++i) {
vector<int> mono;
for (int j = 0; j < m; ++j) {
bool equal = false;
while (!mono.empty() && a[i][mono.back()] <= a[i][j]) {
int k = mono.back();
mono.pop_back();
equal = a[i][k] == a[i][j];
if (k + 1 <= j - 1) {
jump_h[i][k + 1][1] = j - 1;
where_h[k + 1][j - 1].push_back(i);
}
}
if (!mono.empty() && !equal) {
int k = mono.back();
if (k + 1 <= j - 1) {
jump_h[i][j - 1][0] = k + 1;
where_h[k + 1][j - 1].push_back(i);
}
}
mono.push_back(j);
}
}
for (int i = 0; i < m; ++i) {
vector<int> mono;
for (int j = 0; j < n; ++j) {
bool equal = false;
while (!mono.empty() && a[mono.back()][i] <= a[j][i]) {
int k = mono.back();
mono.pop_back();
equal = a[k][i] == a[j][i];
if (k + 1 <= j - 1) {
jump_v[k + 1][i][1] = j - 1;
where_v[k + 1][j - 1].push_back(i);
}
}
if (!mono.empty() && !equal) {
int k = mono.back();
if (k + 1 <= j - 1) {
jump_v[j - 1][i][0] = k + 1;
where_v[k + 1][j - 1].push_back(i);
}
}
mono.push_back(j);
}
}
vector<array<int, 4>> ans;
for (int a = 0; a < n; ++a) {
for (int b = 0; b < m; ++b) {
for (auto c : jump_v[a][b]) {
if (c != -1) {
for (auto d : {a, c}) {
for (auto e : jump_h[d][b]) {
if (e != -1) {
int w = min(a, c), x = max(a, c);
int y = min(b, e), z = max(b, e);
if (check(where_v[w][x], y, z) && check(where_h[y][z], w, x)) {
ans.push_back({w, x, y, z});
}
}
}
}
}
}
}
}
sort(ans.begin(), ans.end());
return unique(ans.begin(), ans.end()) - ans.begin();
}
# | 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... |