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 "rect.h"
#include <bits/stdc++.h>
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define MAX 2502
int N, M;
vector<short> axis[2][MAX][MAX];
vector<short> endp[2][MAX][MAX];
short l[MAX][MAX];
short r[MAX][MAX];
short d[MAX];
short u[MAX];
int ans = 0; void chk(short r1, short c1, short r2, short c2) {
if (r1 > r2 || c1 > c2 || r2 >= N - 1 || c2 >= M - 1 || r1 <= 0 || c1 <= 0) return;
short ind1, ind2;
ind1 = lower_bound(axis[0][r1][r2].begin(), axis[0][r1][r2].end(), c1) - axis[0][r1][r2].begin();
ind2 = lower_bound(axis[0][r1][r2].begin(), axis[0][r1][r2].end(), c2) - axis[0][r1][r2].begin();
if (ind1 >= axis[0][r1][r2].size()) return;
if (ind2 >= axis[0][r1][r2].size()) return;
if (c1 != axis[0][r1][r2][ind1]) return;
if (c2 != axis[0][r1][r2][ind2]) return;
if (c2 - c1 != ind2 - ind1) return;
ind1 = lower_bound(axis[1][c1][c2].begin(), axis[1][c1][c2].end(), r1) - axis[1][c1][c2].begin();
ind2 = lower_bound(axis[1][c1][c2].begin(), axis[1][c1][c2].end(), r2) - axis[1][c1][c2].begin();
if (ind1 >= axis[1][c1][c2].size()) return;
if (ind2 >= axis[1][c1][c2].size()) return;
if (r1 != axis[1][c1][c2][ind1]) return;
if (r2 != axis[1][c1][c2][ind2]) return;
if (r2 - r1 != ind2 - ind1) return;
ans++;
}
long long count_rectangles(std::vector<std::vector<int> > A) {
N = A.size();
M = A[0].size();
int i, j;
memset(l, -1, sizeof(l));
memset(r, -1, sizeof(r));
for (i = 0; i < N; i++) {
vector<pii> st;
for (j = 0; j < M; j++) {
int cc = 1;
while (st.size() && st.back().first <= A[i][j]) {
if (st.back().first == A[i][j]) cc = 0;
if (st.back().second + 1 <= j) r[i][st.back().second] = j - 1;
if (st.back().second + 1 <= j - 1) {
axis[1][st.back().second + 1][j - 1].push_back(i);
endp[1][i][st.back().second + 1].push_back(j - 1);
}
st.pop_back();
}
if (st.size()) l[i][j] = st.back().second + 1;
if (cc && st.size() && st.back().second + 1 <= j - 1) {
axis[1][st.back().second + 1][j - 1].push_back(i);
endp[1][i][st.back().second + 1].push_back(j - 1);
}
st.emplace_back(A[i][j], j);
}
}
vector<tuple<short, short, short, short>> v;
for (i = 0; i < M; i++) {
vector<pii> st;
memset(u, -1, sizeof(u));
memset(d, -1, sizeof(d));
for (j = 0; j < N; j++) {
int cc = 1;
while (st.size() && st.back().first <= A[j][i]) {
if (st.back().first == A[j][i]) cc = 0;
if (st.back().second + 1 <= j) d[st.back().second] = j - 1;
if (st.back().second + 1 <= j - 1) {
axis[0][st.back().second + 1][j - 1].push_back(i);
endp[0][st.back().second + 1][i].push_back(j - 1);
}
st.pop_back();
}
if (st.size()) u[j] = st.back().second + 1;
if (cc && st.size() && st.back().second + 1 <= j - 1) {
axis[0][st.back().second + 1][j - 1].push_back(i);
endp[0][st.back().second + 1][i].push_back(j - 1);
}
st.emplace_back(A[j][i], j);
}
for (j = 1; j < N - 1; j++) if (~l[j][i] && ~r[j][i] && ~u[j] && ~d[j]) v.emplace_back(l[j][i], u[j], r[j][i], d[j]);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (auto& [a, b, c, d] : v) chk(b, a, d, c);
return ans;
}
Compilation message (stderr)
rect.cpp: In function 'void chk(short int, short int, short int, short int)':
rect.cpp:20:11: warning: comparison of integer expressions of different signedness: 'short int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | if (ind1 >= axis[0][r1][r2].size()) return;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
rect.cpp:21:11: warning: comparison of integer expressions of different signedness: 'short int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | if (ind2 >= axis[0][r1][r2].size()) return;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
rect.cpp:27:11: warning: comparison of integer expressions of different signedness: 'short int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | if (ind1 >= axis[1][c1][c2].size()) return;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
rect.cpp:28:11: warning: comparison of integer expressions of different signedness: 'short int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | if (ind2 >= axis[1][c1][c2].size()) return;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |