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"
#define all(x) x.begin(),x.end()
#define PB push_back
#define sz(x) ((int)x.size())
using namespace std;
typedef long long ll;
const int N = 2510;
const int oo = 2e9;
const int PW = 13;
stack<int> stk;
bool mrk[N][N], cor[N];
int n, m, kol = 0, mx, my, Mx, My, lf[N][N], rt[N][N], up[N][N], down[N][N], spar[4][N][N][PW], po[N];
ll ans = 0, big = 0;
void dfs(int x, int y){
if (mrk[x][y]) return;
kol++;
mrk[x][y] = 1;
mx = min(mx, x);
my = min(my, y);
Mx = max(Mx, x);
My = max(My, y);
if (x > 0) dfs(x - 1, y);
if (y > 0) dfs(x, y - 1);
if (y < m - 1) dfs(x, y + 1);
if (x < n - 1) dfs(x + 1, y);
}
int get(int tp, int i, int l, int r){
assert(tp & 1);
int len = r - l + 1;
int pw = po[len];
if (tp == 3)
return min(spar[tp][i][l][pw], spar[tp][i][r - (1 << pw) + 1][pw]);
else return max(spar[tp][i][l][pw], spar[tp][i][r - (1 << pw) + 1][pw]);
}
long long count_rectangles(std::vector<std::vector<int> > a) {
n = sz(a);
m = sz(a[0]);
if (n < 3 || m < 3) return 0;
bool ok = 1;
for (int i = 0; i < n && ok; i++)
for (int j = 0; j < m && ok; j++) {
ok &= bool(a[i][j] < 2);
mrk[i][j] = a[i][j];
}
if (ok){
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!mrk[i][j]){
mx = my = oo;
Mx = My = -oo;
kol = 0;
dfs(i, j);
if ((Mx - mx + 1) * (My - my + 1) == kol && mx > 0 && my > 0 && Mx < n - 1 && My < m - 1)
ans++;
}
return ans;
}
for (int i = 0; i < n; i++){
while (sz(stk)) stk.pop();
stk.push(-1);
for (int j = 0; j < m; j++){
while (sz(stk) > 1 && a[i][stk.top()] < a[i][j])
stk.pop();
lf[i][j] = stk.top();
stk.push(j);
}
while (sz(stk)) stk.pop();
stk.push(m);
for (int j = m - 1; j >= 0; j--){
while (sz(stk) > 1 && a[i][stk.top()] < a[i][j])
stk.pop();
rt[i][j] = stk.top();
stk.push(j);
}
}
for (int j = 0; j < m; j++){
while (sz(stk)) stk.pop();
stk.push(-1);
for (int i = 0; i < n; i++){
while (sz(stk) > 1 && a[stk.top()][j] < a[i][j])
stk.pop();
up[i][j] = stk.top();
stk.push(i);
}
while (sz(stk)) stk.pop();
stk.push(n);
for (int i = n - 1; i >= 0; i--){
while (sz(stk) > 1 && a[stk.top()][j] < a[i][j])
stk.pop();
down[i][j] = stk.top();
stk.push(i);
}
}
po[0] = -1;
for (int i = 1; i <= max(n, m); i++)
po[i] = po[i >> 1] + 1;
for (int i = 1; i < n - 1; i++){
for (int j = 0; j < m; j++){
spar[1][i][j][0] = up[i + 1][j];
spar[3][i][j][0] = down[i - 1][j];
}
for (int pw = 1; pw < PW; pw++)
for (int j = 0; j + (1 << pw) - 1 < m; j++){
spar[1][i][j][pw] = max(spar[1][i][j][pw - 1], spar[1][i][j + (1 << (pw - 1))][pw - 1]);
spar[3][i][j][pw] = min(spar[3][i][j][pw - 1], spar[3][i][j + (1 << (pw - 1))][pw - 1]);
}
}
for (int c1 = 1; c1 < m - 1; c1++)
for (int c2 = c1; c2 < m - 1; c2++){
for (int i = 0; i < n; i++)
cor[i] = (rt[i][c1 - 1] > c2 && lf[i][c2 + 1] < c1);
for (int r1 = 1; r1 < n - 1; r1++){
if (!cor[r1]) continue;
int bd = min(n - 1, get(3, r1, c1, c2));
for (int r2 = r1; r2 < bd; r2++) {
if (!cor[r2]) break;
if (get(1, r2, c1, c2) < r1)
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... |