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"
using namespace std;
const int MAX_W = 205;
const int MAX_N = 2505;
const int d[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
int N, M;
long long ans;
vector <vector <int>> A;
vector <vector <vector <int>>> maxRow, maxCol;
int qs[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
long long count_rectangles(vector <vector <int>> a) {
N = a.size(), M = a[0].size();
A.resize(N + 1, vector <int> (M + 1, 0));
if(min(N, M) < 3) {
return 0;
}
int maxA = 0;
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
A[i + 1][j + 1] = a[i][j];
maxA = max(maxA, a[i][j]);
}
}
if((N <= 700 and M <= 700) or (N <= 3 and M <= 2500)) {
maxRow.resize(N + 1, vector <vector <int>> (M + 1, vector <int> (M + 1)));
maxCol.resize(M + 1, vector <vector <int>> (N + 1, vector <int> (N + 1)));
for(int r = 1; r <= N; r++) {
for(int c = 1; c <= M; c++) {
maxRow[r][c][c] = A[r][c];
}
for(int sz = 2; sz <= M; sz++) {
for(int c1 = 1, c2 = sz; c2 <= M; c1++, c2++) {
maxRow[r][c1][c2] = max(maxRow[r][c1 + 1][c2], maxRow[r][c1][c2 - 1]);
}
}
}
for(int c = 1; c <= M; c++) {
for(int r = 1; r <= N; r++) {
maxCol[c][r][r] = A[r][c];
}
for(int sz = 2; sz <= N; sz++) {
for(int r1 = 1, r2 = sz; r2 <= N; r1++, r2++) {
maxCol[c][r1][r2] = max(maxCol[c][r1 + 1][r2], maxCol[c][r1][r2 - 1]);
}
}
}
for(int r1 = 2; r1 <= N - 1; r1++) {
for(int r2 = r1; r2 <= N - 1; r2++) {
for(int c1 = 2; c1 <= M - 1; c1++) {
bool okCol = true;
for(int c2 = c1; c2 <= M - 1; c2++) {
okCol &= (min(A[r1 - 1][c2], A[r2 + 1][c2]) > maxCol[c2][r1][r2]);
bool okRow = true;
for(int r3 = r1; r3 <= r2; r3++) {
okRow &= (min(A[r3][c1 - 1], A[r3][c2 + 1]) > maxRow[r3][c1][c2]);
}
ans += (okCol == true and okRow == true);
}
}
}
}
}
else {
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= M; j++) {
qs[i][j] = qs[i - 1][j] + qs[i][j - 1] - qs[i - 1][j - 1] + A[i][j];
}
}
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= M; j++) {
if(A[i][j] == 0 and visited[i][j] == false) {
visited[i][j] = true;
int minR = N, maxR = 1, minC = M, maxC = 1;
queue <pair <int, int>> q;
q.emplace(i, j);
while(!q.empty()) {
auto [ux, uy] = q.front();
q.pop();
minR = min(minR, ux);
maxR = max(maxR, ux);
minC = min(minC, uy);
maxC = max(maxC, uy);
for(int ii = 0; ii < 4; ii++) {
int vx = ux + d[ii][0], vy = uy + d[ii][1];
if(vx < 1 or vx > N or vy < 1 or vy > M) {
continue;
}
if(visited[vx][vy] == false and A[vx][vy] == 0) {
visited[vx][vy] = true;
q.emplace(vx, vy);
}
}
}
if(minR > 1 and minC > 1 and maxR < N and maxC < M and qs[maxR][maxC] - qs[maxR][minC - 1] - qs[minR - 1][maxC] + qs[minR - 1][minC - 1] == 0) {
ans++;
}
}
}
}
}
return ans;
}
# | 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... |