#include <bits/stdc++.h>
#include "rect.h"
#define eb emplace_back
using namespace std;
using tii=tuple<int, int, int>;
int N, M, A;
vector<tii> H, V;
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=0; i<N; 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()) H.eb(st.back(), 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()) H.eb(j, 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=0; j<M; 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()) V.eb(st.back(), 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()) V.eb(i, 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);
}
}
sort(H.begin(), H.end());
H.resize(unique(H.begin(), H.end())-H.begin());
sort(V.begin(), V.end());
V.resize(unique(V.begin(), V.end())-V.begin());
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.begin(), H.end(), tii(L, R, U+1))-H.begin();
int b2=lower_bound(H.begin(), H.end(), tii(L, R, D-1))-H.begin();
if (b1>=H.size()||tii(L, R, U+1)<H[b1]) continue;
if (b2>=H.size()||tii(L, R, D-1)<H[b2]) continue;
if (b2-b1!=D-U-2) continue;
b1=lower_bound(V.begin(), V.end(), tii(U, D, L+1))-V.begin();
b2=lower_bound(V.begin(), V.end(), tii(U, D, R-1))-V.begin();
if (b1>=V.size()||tii(U, D, L+1)<V[b1]) continue;
if (b2>=V.size()||tii(U, D, R-1)<V[b2]) continue;
if (b2-b1!=R-L-2) continue;
A++;
}
return A;
}
Compilation message
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:83:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | if (b1>=H.size()||tii(L, R, U+1)<H[b1]) continue;
| ~~^~~~~~~~~~
rect.cpp:84:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | if (b2>=H.size()||tii(L, R, D-1)<H[b2]) continue;
| ~~^~~~~~~~~~
rect.cpp:89:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | if (b1>=V.size()||tii(U, D, L+1)<V[b1]) continue;
| ~~^~~~~~~~~~
rect.cpp:90:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | if (b2>=V.size()||tii(U, D, R-1)<V[b2]) continue;
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
960 KB |
Output is correct |
2 |
Correct |
5 ms |
820 KB |
Output is correct |
3 |
Correct |
4 ms |
960 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
896 KB |
Output is correct |
6 |
Correct |
4 ms |
896 KB |
Output is correct |
7 |
Correct |
5 ms |
896 KB |
Output is correct |
8 |
Correct |
4 ms |
840 KB |
Output is correct |
9 |
Correct |
5 ms |
896 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Incorrect |
3068 ms |
235208 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |