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;
typedef long long ll;
const int maxn=2510;
int n,m,d[maxn][maxn];
int top,st[maxn],N;
int shang[maxn][maxn];
int xia[maxn][maxn];
int zuo[maxn][maxn];
int you[maxn][maxn];
int Y[maxn][maxn]; // you: shang to the min
int Z[maxn][maxn]; // zuo: shang to the min
int X[maxn][maxn]; // xia: zuo to the min
int S[maxn][maxn]; // shang: zuo to the min
set<ll> s;
void add(int x1,int x2,int y1,int y2) {
if (x2<x1||y2<y1) return;
if ((you[x2][y1-1]-1==y2&&Y[x2][y1-1]<=x1)||(zuo[x2][y2+1]+1==y1&&Z[x2][y2+1]<=x1))
if ((xia[x1-1][y2]-1==x2&&X[x1-1][y2]<=y1)||(shang[x2+1][y2]+1==x1&&S[x2+1][y2]<=y1))
s.insert((ll)(x1-1)*N*N*N+(ll)(x2-1)*N*N+(y1-1)*N+y2-1);
}
ll count_rectangles(vector<vector<int> > a) {
n=a.size(); m=a[0].size(); N=max(n,m);
for (int i=0;i<n;i++)
for (int j=0;j<m;j++) d[i+1][j+1]=a[i][j];
for (int i=1;i<=n;i++) {
top=0;
for (int j=1;j<=m;j++) {
while (top&&d[i][j]>d[i][st[top]]) top--;
zuo[i][j]=st[top]; st[++top]=j;
}
top=0;
for (int j=m;j>=1;j--) {
while (top&&d[i][j]>d[i][st[top]]) top--;
you[i][j]=st[top]; st[++top]=j;
}
}
for (int j=1;j<=m;j++) {
top=0;
for (int i=1;i<=n;i++) {
while (top&&d[i][j]>d[st[top]][j]) top--;
shang[i][j]=st[top]; st[++top]=i;
}
top=0;
for (int i=n;i>=1;i--) {
while (top&&d[i][j]>d[st[top]][j]) top--;
xia[i][j]=st[top]; st[++top]=i;
}
}
for (int j=1;j<=m;j++)
for (int i=1;i<=n;i++) {
if (shang[i][j]) {
if (j>1&&shang[i][j]==shang[i][j-1]) S[i][j]=S[i][j-1];
else if (j>1&&i==xia[shang[i][j]][j-1]) S[i][j]=X[shang[i][j]][j-1];
else S[i][j]=j;
}
if (xia[i][j]) {
if (j>1&&xia[i][j]==xia[i][j-1]) X[i][j]=X[i][j-1];
else if (j>1&&i==shang[xia[i][j]][j-1]) X[i][j]=S[xia[i][j]][j-1];
else X[i][j]=j;
}
}
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++) {
if (zuo[i][j]) {
if (i>1&&zuo[i][j]==zuo[i-1][j]) Z[i][j]=Z[i-1][j];
else if (i>1&&j==you[i-1][zuo[i][j]]) Z[i][j]=Y[i-1][zuo[i][j]];
else Z[i][j]=i;
}
if (you[i][j]) {
if (i>1&&you[i][j]==you[i-1][j]) Y[i][j]=Y[i-1][j];
else if (i>1&&j==zuo[i-1][you[i][j]]) Y[i][j]=Z[i-1][you[i][j]];
else Y[i][j]=i;
}
}
for (int i=2;i<n;i++)
for (int j=2;j<m;j++) {
if (shang[i+1][j]&&zuo[i][j+1]) add(shang[i+1][j]+1,i,zuo[i][j+1]+1,j);
if (shang[i+1][j]&&you[i][j-1]) add(shang[i+1][j]+1,i,j,you[i][j-1]-1);
if (shang[i+1][j]&&you[shang[i+1][j]+1][j-1])
add(shang[i+1][j]+1,i,j,you[shang[i+1][j]+1][j-1]-1);
if (shang[i+1][j]&&zuo[shang[i+1][j]+1][j+1])
add(shang[i+1][j]+1,i,zuo[shang[i+1][j]+1][j+1]+1,j);
if (xia[i-1][j]&&zuo[i][j+1]) add(i,xia[i-1][j]-1,zuo[i][j+1]+1,j);
if (xia[i-1][j]&&you[i][j-1]) add(i,xia[i-1][j]-1,j,you[i][j-1]-1);
if (xia[i-1][j]&&you[xia[i-1][j]-1][j-1])
add(i,xia[i-1][j]-1,j,you[xia[i-1][j]-1][j-1]-1);
if (xia[i-1][j]&&zuo[xia[i-1][j]-1][j+1])
add(i,xia[i-1][j]-1,zuo[xia[i-1][j]-1][j+1]+1,j);
}
return s.size();
}
# | 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... |