이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int INF = 88888888;
const int N = 2502;
typedef vector<vector<int> > vvi;
typedef vector<int> vi;
#define all(x) (x).begin(), (x).end()
int n, m;
int le[N][N], ri[N][N], up[N][N], down[N][N];
int go_le[2][N][N], go_ri[2][N][N], go_up[2][N][N], go_down[2][N][N];
bool check_rect(int x1, int y1, int x2, int y2, vector<vector<int> > &a)
{
if (x1 <= 0 || x2 >= n - 1 || y1 <= 0 || y2 >= m - 1) return 0;
if (x1 > x2 || y1 > y2) return 0;
if (!(down[x1 - 1][y1] == x2 + 1 || up[x2 + 1][y1] == x1 - 1)) return 0;
if (!(down[x1 - 1][y2] == x2 + 1 || up[x2 + 1][y2] == x1 - 1)) return 0;
if (!(ri[x1][y1 - 1] == y2 + 1 || le[x1][y2 + 1] == y1 - 1)) return 0;
if (!(ri[x2][y1 - 1] == y2 + 1 || le[x2][y2 + 1] == y1 - 1)) return 0;
int D = 0;
if (a[x1][y1 - 1] <= a[x1][y2 + 1])
{
D = go_down[1][x1][y1 - 1];
}
else
{
D = go_down[0][x1][y2 + 1];
}
int U = 0;
if (a[x2][y1 - 1] <= a[x2][y2 + 1])
{
U = go_up[1][x2][y1 - 1];
}
else
{
U = go_up[0][x2][y2 + 1];
}
int R = 0;
if (a[x1 - 1][y1] <= a[x2 + 1][y1])
{
R = go_ri[1][x1 - 1][y1];
}
else
{
R = go_ri[0][x2 + 1][y1];
}
int L = 0;
if (a[x1 - 1][y2] <= a[x2 + 1][y2])
{
L = go_le[1][x1 - 1][y2];
}
else
{
L = go_le[0][x2 + 1][y2];
}
return (x2 < D && U < x1 && y2 < R && L < y1);
}
long long count_rectangles(vector<vector<int> > a)
{
n = a.size(), m = a[0].size();
for (int i = 0; i < n; i++)
{
vector<pair<int, int> > st;
st.push_back({-1, INF});
for (int j = 0; j < m; j++)
{
while (st.back().second < a[i][j]) st.pop_back();
le[i][j] = st.back().first;
st.push_back({j, a[i][j]});
}
}
for (int i = 0; i < n; i++)
{
vector<pair<int, int> > st;
st.push_back({m, INF});
for (int j = m - 1; j >= 0; j--)
{
while (st.back().second < a[i][j]) st.pop_back();
ri[i][j] = st.back().first;
st.push_back({j, a[i][j]});
}
}
for (int j = 0; j < m; j++)
{
vector<pair<int, int> > st;
st.push_back({-1, INF});
for (int i = 0; i < n; i++)
{
while (st.back().second < a[i][j]) st.pop_back();
up[i][j] = st.back().first;
st.push_back({i, a[i][j]});
}
}
for (int j = 0; j < m; j++)
{
vector<pair<int, int> > st;
st.push_back({n, INF});
for (int i = n - 1; i >= 0; i--)
{
while (st.back().second < a[i][j]) st.pop_back();
down[i][j] = st.back().first;
st.push_back({i, a[i][j]});
}
}
for (int i = 1; i < n; i++)
{
vector<pair<int, int> > st;
st.push_back({-1, INF});
vector<int> pack;
pack.push_back(n);
for (int j = 0; j < m; j++)
{
int unpack = n;
while (st.back().second < a[i][j])
{
unpack = min(unpack, down[i - 1][st.back().first]);
st.pop_back();
unpack = min(unpack, pack.back());
pack.pop_back();
}
st.push_back({j, a[i][j]});
pack.push_back(unpack);
go_down[0][i][j] = unpack;
}
}
for (int i = 1; i < n; i++)
{
vector<pair<int, int> > st;
st.push_back({m, INF});
vector<int> pack;
pack.push_back(n);
for (int j = m - 1; j >= 0; j--)
{
int unpack = n;
while (st.back().second < a[i][j])
{
unpack = min(unpack, down[i - 1][st.back().first]);
st.pop_back();
unpack = min(unpack, pack.back());
pack.pop_back();
}
st.push_back({j, a[i][j]});
pack.push_back(unpack);
go_down[1][i][j] = unpack;
}
}
for (int i = 0; i + 1 < n; i++)
{
vector<pair<int, int> > st;
st.push_back({-1, INF});
vector<int> pack;
pack.push_back(-1);
for (int j = 0; j < m; j++)
{
int unpack = -1;
while (st.back().second < a[i][j])
{
unpack = max(unpack, up[i + 1][st.back().first]);
st.pop_back();
unpack = max(unpack, pack.back());
pack.pop_back();
}
st.push_back({j, a[i][j]});
pack.push_back(unpack);
go_up[0][i][j] = unpack;
}
}
for (int i = 0; i + 1 < n; i++)
{
vector<pair<int, int> > st;
st.push_back({m, INF});
vector<int> pack;
pack.push_back(-1);
for (int j = m - 1; j >= 0; j--)
{
int unpack = -1;
while (st.back().second < a[i][j])
{
unpack = max(unpack, up[i + 1][st.back().first]);
st.pop_back();
unpack = max(unpack, pack.back());
pack.pop_back();
}
st.push_back({j, a[i][j]});
pack.push_back(unpack);
go_up[1][i][j] = unpack;
}
}
for (int j = 1; j < m; j++)
{
vector<pair<int, int> > st;
st.push_back({-1, INF});
vector<int> pack;
pack.push_back(m);
for (int i = 0; i < n; i++)
{
int unpack = m;
while (st.back().second < a[i][j])
{
unpack = min(unpack, ri[st.back().first][j - 1]);
st.pop_back();
unpack = min(unpack, pack.back());
pack.pop_back();
}
st.push_back({i, a[i][j]});
pack.push_back(unpack);
go_ri[0][i][j] = unpack;
}
}
for (int j = 1; j < m; j++)
{
vector<pair<int, int> > st;
st.push_back({n, INF});
vector<int> pack;
pack.push_back(m);
for (int i = n - 1; i >= 0; i--)
{
int unpack = m;
while (st.back().second < a[i][j])
{
unpack = min(unpack, ri[st.back().first][j - 1]);
st.pop_back();
unpack = min(unpack, pack.back());
pack.pop_back();
}
st.push_back({i, a[i][j]});
pack.push_back(unpack);
go_ri[1][i][j] = unpack;
}
}
for (int j = 0; j + 1 < m; j++)
{
vector<pair<int, int> > st;
st.push_back({-1, INF});
vector<int> pack;
pack.push_back(-1);
for (int i = 0; i < n; i++)
{
int unpack = -1;
while (st.back().second < a[i][j])
{
unpack = max(unpack, le[st.back().first][j + 1]);
st.pop_back();
unpack = max(unpack, pack.back());
pack.pop_back();
}
st.push_back({i, a[i][j]});
pack.push_back(unpack);
go_le[0][i][j] = unpack;
}
}
for (int j = 0; j + 1 < m; j++)
{
vector<pair<int, int> > st;
st.push_back({n, INF});
vector<int> pack;
pack.push_back(-1);
for (int i = n - 1; i >= 0; i--)
{
int unpack = -1;
while (st.back().second < a[i][j])
{
unpack = max(unpack, le[st.back().first][j + 1]);
st.pop_back();
unpack = max(unpack, pack.back());
pack.pop_back();
}
st.push_back({i, a[i][j]});
pack.push_back(unpack);
go_le[1][i][j] = unpack;
}
}
vector<vector<vector<pair<int, int> > > > rooted(n, vector<vector<pair<int, int> > >(m));
for (int i = 1; i + 1 < n; i++)
{
for (int j = 1; j + 1 < m; j++)
{
{
int r = ri[i][j - 1] - 1;
int u0 = up[i + 1][j] + 1;
int u1 = up[i + 1][r] + 1;
int d0 = down[i - 1][j] - 1;
int d1 = down[i - 1][r] - 1;
rooted[u0][j].push_back({i, r});
rooted[u1][j].push_back({i, r});
rooted[i][j].push_back({d0, r});
rooted[i][j].push_back({d1, r});
}
{
int l = le[i][j + 1] + 1;
int u0 = up[i + 1][l] + 1;
int u1 = up[i + 1][j] + 1;
int d0 = down[i - 1][l] - 1;
int d1 = down[i - 1][j] - 1;
rooted[u0][l].push_back({i, j});
rooted[u1][l].push_back({i, j});
rooted[i][l].push_back({d0, j});
rooted[i][l].push_back({d1, j});
}
}
}
int ans = 0;
for (int i = 1; i < n; i++)
{
for (int j = 1; j < m; j++)
{
sort(all(rooted[i][j]));
rooted[i][j].resize(unique(all(rooted[i][j])) - rooted[i][j].begin());
for (auto p : rooted[i][j])
{
int i2 = p.first, j2 = p.second;
ans += (int)check_rect(i, j, i2, j2, a);
}
}
}
return ans;
}
# | 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... |