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>
#include "rect.h"
#define eb emplace_back
#define hsh(b, c) ((b)*2500+(c))
using namespace std;
int N, M;
vector<int> H[2505], V[2505];
long long count_rectangles(vector<vector<int>> a) {
N=a.size(); M=a[0].size();
vector<vector<int>> l, r, u, d;
for (int i=0; i<N; i++)
l.eb(vector<int>(M, -1));
r=u=d=l;
for (int i=1; i<N-1; i++) {
vector<int> st;
for (int j=0; j<M; j++) {
while (st.size()&&a[i][st.back()]<a[i][j]) st.pop_back();
if (st.size()&&st.back()<j-1) H[st.back()].eb(hsh(j, i));
st.eb(j);
}
st.clear();
for (int j=M-1; j>=0; j--) {
while (st.size()&&a[i][st.back()]<a[i][j]) st.pop_back();
if (st.size()&&st.back()>j+1&&a[i][st.back()]!=a[i][j]) H[j].eb(hsh(st.back(), i));
st.eb(j);
}
st.clear();
for (int j=0; j<M; j++) {
while (st.size()&&a[i][st.back()]<=a[i][j]) st.pop_back();
if (st.size()) l[i][j]=st.back();
st.eb(j);
}
st.clear();
for (int j=M-1; j>=0; j--) {
while (st.size()&&a[i][st.back()]<=a[i][j]) st.pop_back();
if (st.size()) r[i][j]=st.back();
st.eb(j);
}
}
for (int j=1; j<M-1; j++) {
vector<int> st;
for (int i=0; i<N; i++) {
while (st.size()&&a[st.back()][j]<a[i][j]) st.pop_back();
if (st.size()&&st.back()<i-1) V[st.back()].eb(hsh(i, j));
st.eb(i);
}
st.clear();
for (int i=N-1; i>=0; i--) {
while (st.size()&&a[st.back()][j]<a[i][j]) st.pop_back();
if (st.size()&&st.back()>i+1&&a[st.back()][j]!=a[i][j]) V[i].eb(hsh(st.back(), j));
st.eb(i);
}
st.clear();
for (int i=0; i<N; i++) {
while (st.size()&&a[st.back()][j]<=a[i][j]) st.pop_back();
if (st.size()) u[i][j]=st.back();
st.eb(i);
}
st.clear();
for (int i=N-1; i>=0; i--) {
while (st.size()&&a[st.back()][j]<=a[i][j]) st.pop_back();
if (st.size()) d[i][j]=st.back();
st.eb(i);
}
}
for (int i=0; i<M; i++) sort(H[i].begin(), H[i].end());
for (int i=0; i<N; i++) sort(V[i].begin(), V[i].end());
vector<tuple<int, int, int, int>> A;
for (int i=1; i<N-1; i++) for (int j=1; j<M-1; j++) {
int L=l[i][j], R=r[i][j], U=u[i][j], D=d[i][j];
if (L==-1||R==-1||U==-1||D==-1) continue;
int b1=lower_bound(H[L].begin(), H[L].end(), hsh(R, U+1))-H[L].begin();
if (b1>=H[L].size()||hsh(R, U+1)!=H[L][b1]) continue;
b1+=D-U-2;
if (b1>=H[L].size()||hsh(R, D-1)!=H[L][b1]) continue;
b1=lower_bound(V[U].begin(), V[U].end(), hsh(D, L+1))-V[U].begin();
if (b1>=V[U].size()||hsh(D, L+1)!=V[U][b1]) continue;
b1+=R-L-2;
if (b1>=V[U].size()||hsh(D, R-1)!=V[U][b1]) continue;
A.eb(L, R, U, D);
}
sort(A.begin(), A.end());
return unique(A.begin(), A.end())-A.begin();
}
Compilation message (stderr)
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:80:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | if (b1>=H[L].size()||hsh(R, U+1)!=H[L][b1]) continue;
| ~~^~~~~~~~~~~~~
rect.cpp:82:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | if (b1>=H[L].size()||hsh(R, D-1)!=H[L][b1]) continue;
| ~~^~~~~~~~~~~~~
rect.cpp:85:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | if (b1>=V[U].size()||hsh(D, L+1)!=V[U][b1]) continue;
| ~~^~~~~~~~~~~~~
rect.cpp:87:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | if (b1>=V[U].size()||hsh(D, R-1)!=V[U][b1]) continue;
| ~~^~~~~~~~~~~~~
# | 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... |