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;
using ll = long long;
int lg[2501];
struct sparse_table {
int n, e;
int (*f)(int, int);
vector<vector<int>> st;
sparse_table() {
}
sparse_table(vector<int> &a, int (*_f)(int, int), int _e) : n(a.size()), f(_f), e(_e) {
st = vector(n, vector<int>(lg[n] + 1, e));
for (int i = 0; i < n; i++) {
st[i][0] = a[i];
}
for (int j = 0; j < lg[n]; j++) {
for (int i = 0; i + (2 << j) <= n; i++) {
st[i][j + 1] = f(st[i][j], st[i + (1 << j)][j]);
}
}
}
int query(int l, int r) {
if (l == r) {
return e;
}
int k = lg[r - l];
return f(st[l][k], st[r - (1 << k)][k]);
}
};
int fmin(int x, int y) {
return min(x, y);
}
int fmax(int x, int y) {
return max(x, y);
}
ll count_rectangles(vector<vector<int>> a) {
for (int i = 2; i <= 2500; i++) {
lg[i] = lg[i / 2] + 1;
}
int n = a.size();
int m = a[0].size();
bool subtask6 = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] > 1) {
subtask6 = 0;
}
}
}
vector<int> stk;
int L[n][m], R[n][m], U[n][m], D[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
while (!stk.empty() && a[i][stk.back()] < a[i][j]) {
stk.pop_back();
}
L[i][j] = stk.empty() ? -1 : stk.back();
stk.push_back(j);
}
stk.clear();
for (int j = m - 1; j >= 0; j--) {
while (!stk.empty() && a[i][stk.back()] < a[i][j]) {
stk.pop_back();
}
R[i][j] = stk.empty() ? m : stk.back();
stk.push_back(j);
}
stk.clear();
}
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
while (!stk.empty() && a[stk.back()][j] < a[i][j]) {
stk.pop_back();
}
U[i][j] = stk.empty() ? -1 : stk.back();
stk.push_back(i);
}
stk.clear();
for (int i = n - 1; i >= 0; i--) {
while (!stk.empty() && a[stk.back()][j] < a[i][j]) {
stk.pop_back();
}
D[i][j] = stk.empty() ? n : stk.back();
stk.push_back(i);
}
stk.clear();
}
sparse_table SL[m], SR[m], SU[n], SD[n];
for (int i = 0; i < n; i++) {
vector<int> u(m), d(m);
for (int j = 0; j < m; j++) {
u[j] = U[i][j];
d[j] = D[i][j];
}
SU[i] = sparse_table(u, fmax, -1);
SD[i] = sparse_table(d, fmin, n);
}
for (int i = 0; i < m; i++) {
vector<int> l(n), r(n);
for (int j = 0; j < n; j++) {
l[j] = L[j][i];
r[j] = R[j][i];
}
SL[i] = sparse_table(l, fmax, -1);
SR[i] = sparse_table(r, fmin, m);
}
auto check = [&](int x1, int y1, int x2, int y2) -> int {
if (x1 > x2 || y1 > y2) {
return 0;
}
if (SD[x1 - 1].query(y1, y2 + 1) <= x2) {
return 0;
}
if (SU[x2 + 1].query(y1, y2 + 1) >= x1) {
return 0;
}
if (SR[y1 - 1].query(x1, x2 + 1) <= y2) {
return 0;
}
if (SL[y2 + 1].query(x1, x2 + 1) >= y1) {
return 0;
}
return 1;
};
int ans = 0;
if (subtask6) {
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) {
if (D[i - 1][j] != n && R[i][j - 1] != m) {
ans += check(i, j, D[i - 1][j] - 1, R[i][j - 1] - 1);
}
}
}
}
return ans;
}
vector<int> T[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (U[i][j] != -1) {
T[U[i][j]][j].push_back(i);
}
}
}
for (int i = 1; i < n - 1; i++) {
for (int j = 1; j < m - 1; j++) {
int y = R[i][j - 1] - 1;
if (y != m - 1) {
int x = D[i - 1][j] - 1;
if (x != n - 1) {
ans += check(i, j, x, y);
}
for (int k : T[i - 1][j]) {
if (a[i - 1][j] != a[k][j]) {
ans += check(i, j, k - 1, y);
}
}
}
y = L[i][j + 1] + 1;
if (y != 0 && a[i][j + 1] != a[i][y - 1]) {
int x = D[i - 1][j] - 1;
if (x != n - 1) {
ans += check(i, y, x, j);
}
for (int k : T[i - 1][j]) {
if (a[i - 1][j] != a[k][j]) {
ans += check(i, y, k - 1, j);
}
}
}
}
}
return ans;
}
Compilation message (stderr)
rect.cpp: In constructor 'sparse_table::sparse_table(std::vector<int>&, int (*)(int, int), int)':
rect.cpp:8:11: warning: 'sparse_table::f' will be initialized after [-Wreorder]
8 | int (*f)(int, int);
| ^
rect.cpp:7:12: warning: 'int sparse_table::e' [-Wreorder]
7 | int n, e;
| ^
rect.cpp:12:5: warning: when initialized here [-Wreorder]
12 | sparse_table(vector<int> &a, int (*_f)(int, int), int _e) : n(a.size()), f(_f), e(_e) {
| ^~~~~~~~~~~~
# | 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... |