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"
using namespace std;
#define int long long
const int MAXN = 2500;
int32_t upBad[MAXN][MAXN];
int32_t downBad[MAXN][MAXN];
int32_t leftBad[MAXN][MAXN];
int32_t rightBad[MAXN][MAXN];
int32_t up[MAXN][12][MAXN], down[MAXN][12][MAXN];
int32_t Left[MAXN][12][MAXN], Right[MAXN][12][MAXN];
int n,m;
int32_t dsu[MAXN * MAXN];
int set_of(int u) {
if(dsu[u] == u) return u;
return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
dsu[set_of(u)] = set_of(v);
}
int up_query(int i, int l, int r) {
int k = 32 - __builtin_clz(r - l + 1) - 1;
return max(up[i][k][l], up[i][k][r - (1<<k) + 1]);
}
int down_query(int i, int l, int r) {
int k = 32 - __builtin_clz(r - l + 1) - 1;
return min(down[i][k][l], down[i][k][r - (1<<k) + 1]);
}
int left_query(int i, int l, int r) {
int k = 32 - __builtin_clz(r - l + 1) - 1;
return max(Left[i][k][l], Left[i][k][r - (1<<k) + 1]);
}
int right_query(int i, int l, int r) {
int k = 32 - __builtin_clz(r - l + 1) - 1;
return min(Right[i][k][l], Right[i][k][r - (1<<k) + 1]);
}
int count_rectangles(std::vector<std::vector<int32_t> > a) {
n = a.size(), m = a[0].size();
int32_t mx = 0;
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mx = max(mx, a[i][j]);
dsu[i * m + j] = i * m + j;
rightBad[i][j] = m;
downBad[i][j] = n;
upBad[i][j] = -1;
leftBad[i][j] = -1;
for(int k=j+1; k<m; k++) {
if(a[i][k] >= a[i][j]) {
rightBad[i][j] = k; break;
}
}
for(int k=j-1; k>=0; k--) {
if(a[i][k] >= a[i][j]) {
leftBad[i][j] = k; break;
}
}
for(int k=i+1; k<n; k++) {
if(a[k][j] >= a[i][j]) {
downBad[i][j] = k; break;
}
}
for(int k=i-1; k>=0; k--) {
if(a[k][j] >= a[i][j]) {
upBad[i][j] = k; break;
}
}
}
}
if(mx == 1) {
for(int i=0; i+1<n; i++) {
for(int j=0; j+1<m; j++) {
if(a[i][j] == 0 && a[i][j+1] == 0) union_(i*m + j, i*m + j + 1);
if(a[i][j] == 0 && a[i+1][j] == 0) union_(i*m + j, (i+1)*m + j);
}
}
vector<int> ll[n * m];
for(int i=0; i<n*m; i++) ll[set_of(i)].push_back(i);
int ans = 0;
for(int i=0; i<n*m; i++) {
if(ll[i].size()) {
int r = ll[i][0] / m, c = ll[i][0] % m;
if(a[r][c] == 1) continue;
int u, d, l;
u = l = 1e9, d = r = -1;
for(int x: ll[i]) {
u = min(u, x / m);
d = max(d, x / m);
l = min(l, x % m);
r = max(r, x % m);
}
if((d - u + 1) * (r - l + 1) == ll[i].size() && u > 0 && l > 0 && d + 1 < n && r + 1 < m) {
ans++;
}
}
}
return ans;
}
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
Left[j][0][i] = leftBad[i][j];
Right[j][0][i] = rightBad[i][j];
up[i][0][j] = upBad[i][j];
down[i][0][j] = downBad[i][j];
}
}
for(int k=1; k<12; k++) {
for(int i=0; i<n; i++) {
for(int j=0; j+(1<<k)-1<m; j++) {
up[i][k][j] = max(up[i][k-1][j], up[i][k-1][j+(1<<(k-1))]);
down[i][k][j] = min(down[i][k-1][j], down[i][k-1][j+(1<<(k-1))]);
}
}
for(int i=0; i<m; i++) {
for(int j=0; j+(1<<k)-1<n; j++) {
Left[i][k][j] = max(Left[i][k-1][j], Left[i][k-1][j+(1<<(k-1))]);
Right[i][k][j] = min(Right[i][k-1][j], Right[i][k-1][j+(1<<(k-1))]);
}
}
}
int ans = 0;
for(int len=1; len<=n-2; len++) {
for(int i=1; i+len-1<=n-2; i++) {
int sto[m];
for(int x=0; x<m; x++) sto[x] = -1;
for(int x=1; x+1<m; x++) {
int u, d, l, r;
bool alive;
u = i, d = i + len - 1;
// as left
l = x, r = right_query(x - 1, u, d) - 1;
sto[l] = r;
if(u <= d && l <= r && l > 0 && r + 1 < m) {
alive = 1;
alive &= (right_query(l - 1, u, d) > r);
alive &= (left_query(r + 1, u, d) < l);
alive &= (down_query(u - 1, l, r) > d);
alive &= (up_query(d + 1, l, r) < u);
ans += alive;
//if(alive) cout << "u = " << u << ", d = " << d << ", l = " << l << ", r = "<<r<<"\n";
}
// as right
l = left_query(x + 1, u, d) + 1, r = x;
if(u <= d && l <= r && sto[l] != r && l > 0 && r + 1 < m) {
alive = 1;
alive &= (right_query(l - 1, u, d) > r);
alive &= (left_query(r + 1, u, d) < l);
alive &= (down_query(u - 1, l, r) > d);
alive &= (up_query(d + 1, l, r) < u);
ans += alive;
//if(alive) cout << "u = " << u << ", d = " << d << ", l = " << l << ", r = "<<r<<"\n";
}
}
}
}
return ans;
}
/*
g++ -std=c++17 -O2 -o rect grader.cpp rect.cpp
./rect < input.txt
*/
Compilation message (stderr)
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:107:38: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | if((d - u + 1) * (r - l + 1) == ll[i].size() && u > 0 && l > 0 && d + 1 < n && r + 1 < m) {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
# | 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... |