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;
inline int trans(int i, int j) {
return ((j * (j + 1)) >> 1) + i;
}
vector<short> axis[2][MAX * (MAX + 1) / 2];
vector<short> endp[2][MAX * (MAX + 1) / 2];
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][trans(r1, r2)].begin(), axis[0][trans(r1, r2)].end(), c1) - axis[0][trans(r1, r2)].begin();
ind2 = lower_bound(axis[0][trans(r1, r2)].begin(), axis[0][trans(r1, r2)].end(), c2) - axis[0][trans(r1, r2)].begin();
if (ind1 >= axis[0][trans(r1, r2)].size()) return;
if (ind2 >= axis[0][trans(r1, r2)].size()) return;
if (c1 != axis[0][trans(r1, r2)][ind1]) return;
if (c2 != axis[0][trans(r1, r2)][ind2]) return;
if (c2 - c1 != ind2 - ind1) return;
ind1 = lower_bound(axis[1][trans(c1, c2)].begin(), axis[1][trans(c1, c2)].end(), r1) - axis[1][trans(c1, c2)].begin();
ind2 = lower_bound(axis[1][trans(c1, c2)].begin(), axis[1][trans(c1, c2)].end(), r2) - axis[1][trans(c1, c2)].begin();
if (ind1 >= axis[1][trans(c1, c2)].size()) return;
if (ind2 >= axis[1][trans(c1, c2)].size()) return;
if (r1 != axis[1][trans(c1, c2)][ind1]) return;
if (r2 != axis[1][trans(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][trans(st.back().second + 1, j - 1)].push_back(i);
endp[1][trans(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][trans(st.back().second + 1, j - 1)].push_back(i);
endp[1][trans(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][trans(st.back().second + 1, j - 1)].push_back(i);
endp[0][trans(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][trans(st.back().second + 1, j - 1)].push_back(i);
endp[0][trans(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:23:11: warning: comparison of integer expressions of different signedness: 'short int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | if (ind1 >= axis[0][trans(r1, r2)].size()) return;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rect.cpp:24:11: warning: comparison of integer expressions of different signedness: 'short int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | if (ind2 >= axis[0][trans(r1, r2)].size()) return;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rect.cpp:30:11: warning: comparison of integer expressions of different signedness: 'short int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | if (ind1 >= axis[1][trans(c1, c2)].size()) return;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rect.cpp:31:11: warning: comparison of integer expressions of different signedness: 'short int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | if (ind2 >= axis[1][trans(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... |