# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
561046 | hoanghq2004 | Rectangles (IOI19_rect) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "rect.h"
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2510;
int up[N][N], down[N][N], lft[N][N], rgt[N][N];
int count_rectangles(vector <vector <int>> a) {
int n = a.size();
int m = a[0].size();
for (int i = 0; i < n; ++i) {
stack <int> stk;
for (int j = 0; j < m; ++j) {
while (stk.size() && a[i][j] >= a[i][stk.top()]) {
rgt[i][stk.top()] = j - 1;
stk.pop();
}
stk.push(j);
}
while (stk.size()) {
rgt[i][stk.top()] = m - 1;
stk.pop();
}
for (int j = m - 1; j >= 0; --j) {
while (stk.size() && a[i][j] >= a[i][stk.top()]) {
lft[i][stk.top()] = j + 1;
stk.pop();
}
stk.push(j);
}
while (stk.size()) {
lft[i][stk.top()] = 0;
stk.pop();
}
}
for (int i = 0; i < m; ++i) {
stack <int> stk;
for (int j = 0; j < n; ++j) {
while (stk.size() && a[j][i] >= a[stk.top()][i]) {
down[stk.top()][i] = j - 1;
stk.pop();
}
stk.push(j);
}
while (stk.size()) {
down[stk.top()][i] = n - 1;
stk.pop();
}
for (int j = n - 1; j >= 0; --j) {
while (stk.size() && a[j][i] >= a[stk.top()][i]) {
up[stk.top()][i] = j + 1;
stk.pop();
}
stk.push(j);
}
while (stk.size()) {
up[stk.top()][i] = 0;
stk.pop();
}
}
auto check = [&](int x, int y, int u, int v) {
for (int i = x; i <= u; ++i)
if (rgt[i][y - 1] < v || lft[i][v + 1] > y) return 0;
for (int i = y; i <= v; ++i)
if (down[x - 1][i] < u || up[u + 1][i] > x) return 0;
return 1;
};
int ans = 0;
for (int x = 1; x < n - 1; ++x)
for (int y = 1; y < m - 1; ++y)
for (int u = x; u < n - 1; ++u)
for (int v = y; v < m - 1; ++v)
ans += check(x, y, u, v);
return ans;
}
//
//int main() {
// int n, m;
// cin >> n >> m;
// vector<vector<int>> a(n, vector<int>(m));
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// cin >> a[i][j];
// }
// }
// cout << count_rectangles(a) << '\n';
//}