This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifndef LOCAL
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2,sse4.2")
#endif
#include "rect.h"
long long count_rectangles(std::vector<std::vector<int> > a) {
int N = a.size();
int M = a[0].size();
int maxv = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
maxv = std::max(maxv, a[i][j]);
}
}
if (maxv == 0) return 0;
long long ans = 0;
if (maxv == 1) {
int pfx[N][M];
pfx[0][0] = a[0][0];
for (int i = 1; i < M; ++i) {
pfx[0][i] = a[0][i] + pfx[0][i - 1];
}
for (int i = 1; i < N; ++i) {
pfx[i][0] = a[i][0] + pfx[i - 1][0];
for (int j = 1; j < M; ++j) {
pfx[i][j] = a[i][j] + pfx[i - 1][j] + pfx[i][j - 1] - pfx[i - 1][j - 1];
}
}
for (int i = 1; i < N - 1; ++i) {
for (int j = 1; j < M - 1; ++j) {
if (a[i][j] == 0 && a[i - 1][j] == 1 && a[i][j - 1] == 1) {
int below = -1, right = -1;
for (int k = i; k < N; ++k) {
if (a[k][j] == 1) {
below = k;
break;
}
}
for (int k = j; k < M; ++k) {
if (a[i][k] == 1) {
right = k;
break;
}
}
if (below == -1 || right == -1) continue;
int summid = pfx[below - 1][right - 1] - pfx[below - 1][j - 1] - pfx[i - 1][right - 1] +
pfx[i - 1][j - 1];
if (summid) continue;
for (int k = i; k < below; ++k) {
if (!(a[k][j - 1] && a[k][right])) goto stop;
}
for (int k = j; k < right; ++k) {
if (!(a[i - 1][k] && a[below][k])) goto stop;
}
ans++;
stop:;
}
}
}
return ans;
}
if (N == 3) {
bool works[M];
works[0] = false;
works[M - 1] = false;
for (int i = 1; i < M - 1; ++i) {
works[i] = a[1][i] < a[0][i] && a[1][i] < a[2][i];
}
for (int l = 1; l < M - 1; ++l) {
if (!works[l]) continue;
int max = a[1][l];
for (int r = l; r < M - 1; ++r) {
if (!works[r]) break;
max = std::max(max, a[1][r]);
if (max >= a[1][l - 1]) break;
if (max >= a[1][r + 1]) continue;
ans++;
}
}
return ans;
}
int max[N];
for (int u = 1; u < N - 1; ++u) {
int maxc[M];
for (int i = 0; i < M; ++i) {
maxc[i] = a[u][i];
}
std::vector<int> alive;
for (int i = 1; i < M - 1; ++i) {
alive.push_back(i);
}
for (int d = u; d < N - 1; ++d) {
for (auto i: alive) {
maxc[i] = std::max(maxc[i], a[d][i]);
}
bool works[M];
std::vector<int> working, nextalive;
for (int i = 0; i < M; ++i) {
works[i] = maxc[i] < a[u - 1][i] && maxc[i] < a[d + 1][i];
if (works[i]) working.push_back(i);
if (maxc[i] < a[u - 1][i]) nextalive.push_back(i);
}
alive = nextalive;
for (auto l: working) {
for (int i = u; i <= d; ++i) {
max[i] = a[i][l];
}
for (int r = l; r < M - 1; ++r) {
if (!works[r]) break;
for (int i = u; i <= d; ++i) {
max[i] = std::max(max[i], a[i][r]);
}
for (int i = u; i <= d; ++i) {
if (max[i] >= a[i][l - 1]) goto dead;
}
for (int i = u; i <= d; ++i) {
if (max[i] >= a[i][r + 1]) goto next;
}
ans++;
next:;
}
dead:;
}
}
}
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... |