# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
412466 | timmyfeng | Rectangles (IOI19_rect) | C++17 | 0 ms | 0 KiB |
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;
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];
array<int, 4> rects[8 * N];
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);
}
}
int ans = 0;
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)) {
rects[ans++] = {w, x, y, z};
}
}
}
}
}
}
}
}
sort(rects, rects + ans);
return unique(rects, rects + ans) - rects;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << count_rectangles({{4, 8, 7, 5, 6},
{7, 4, 10, 3, 5},
{9, 7, 20, 14, 2},
{9, 14, 7, 3, 6},
{5, 7, 5, 2, 7},
{4, 5, 13, 5, 6}}) << "\n";
}