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 INF = 88888888;
typedef vector<vector<int> > vvi;
typedef vector<int> vi;
struct rect{
int x1, y1, x2, y2;
bool operator == (rect b){
return b.x1 == x1 && b.y1 == y1 && b.x2 == x2 && b.y2 == y2;
}
};
bool comp(rect a, rect b)
{
return pair<pair<int, int>, pair<int, int> >{{a.x1, a.y1}, {a.x2, a.y2}} <
pair<pair<int, int>, pair<int, int> >{{b.x1, b.y1}, {b.x2, b.y2}};
}
#define all(x) (x).begin(), (x).end()
long long count_rectangles(vector<vector<int> > a)
{
int n = a.size(), m = a[0].size();
vvi le(n, vi(m)), ri(n, vi(m)), up(n, vi(m)), down(n, vi(m));
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]});
}
}
vector<rect> ch;
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;
ch.push_back({u0, j, i, r});
ch.push_back({u1, j, i, r});
ch.push_back({i, j, d0, r});
ch.push_back({i, j, 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;
ch.push_back({u0, l, i, j});
ch.push_back({u1, l, i, j});
ch.push_back({i, l, d0, j});
ch.push_back({i, l, d1, j});
}
}
}
sort(all(ch), comp);
int ans = 0;
for (int i = 0; i < ch.size(); i++)
{
if (i && ch[i] == ch[i - 1]) continue;
if (ch[i].x1 <= ch[i].x2 && ch[i].y1 <= ch[i].y2 && 1 <= ch[i].x1 && 1 <= ch[i].y1 && ch[i].x2 < n - 1 && ch[i].y2 < m - 1)
{
int t = 1;
for (int j = ch[i].y1; j <= ch[i].y2; j++) if (down[ch[i].x1 - 1][j] <= ch[i].x2) t = 0;
for (int j = ch[i].y1; j <= ch[i].y2; j++) if (up[ch[i].x2 + 1][j] >= ch[i].x1) t = 0;
for (int j = ch[i].x1; j <= ch[i].x2; j++) if (ri[j][ch[i].y1 - 1] <= ch[i].y2) t = 0;
for (int j = ch[i].x1; j <= ch[i].x2; j++) if (le[j][ch[i].y2 + 1] >= ch[i].y1) t = 0;
if (t)
{
ans++;
//cout << ch[i].x1 << " " << ch[i].y1 << " " << ch[i].x2 << " " << ch[i].y2 << "\n";
}
}
}
return ans;
}
Compilation message (stderr)
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:104:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<rect>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for (int i = 0; i < ch.size(); i++)
| ~~^~~~~~~~~~~
# | 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... |