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>
using namespace std;
const int c=2550;
bool v[c][c], t[c][c];
int n, m, ans, ks, ko, ns, no, mini, db, fel[c][c], le[c][c], bal[c][c], jobb[c][c], cfel[c][c], cle[c][c], cbal[c][c], cjobb[c][c];
set<pair<pair<int, int>, pair<int, int> > > sol;
void add(int ks, int ko, int ns, int no) {
int mini=min({ks, ko, ns, no});
if (mini!=-1 && ks && ko && ns<n-1 && no<m-1 && ks<=ns && ko<=no) {
int sdb=ns-ks+1, odb=no-ko+1;
bool josor=(jobb[ns][ko]==no && cjobb[ns][ko]>=sdb || bal[ns][no]==ko && cbal[ns][no]>=sdb);
bool jooszlop=(le[ks][no]==ns && cle[ks][no]>=odb || fel[ns][no]==ks && cfel[ns][no]>=odb);
if (josor && jooszlop) {
pair<pair<int, int>, pair<int, int> > p={{ks, ko}, {ns, no}};
if (sol.find(p)==sol.end()) {
ans++;
sol.insert(p);
}
}
}
}
long long count_rectangles(vector<vector<int>> sz) {
n=sz.size(), m=sz[0].size();
vector<int> s;
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
while(s.size()>0 && sz[i][j]>=sz[i][s.back()]) jobb[i][1+s.back()]=j-1, s.pop_back();
s.push_back(j);
}
while(s.size()>0) jobb[i][1+s.back()]=-1, s.pop_back();
for (int j=m-1; j>=0; j--) {
while(s.size()>0 && sz[i][j]>=sz[i][s.back()]) bal[i][-1+s.back()]=j+1, s.pop_back();
if (j) s.push_back(j);
}
while(s.size()>0) bal[i][-1+s.back()]=-1, s.pop_back();
}
for (int j=0; j<m; j++) {
for (int i=0; i<n; i++) {
while(s.size()>0 && sz[i][j]>=sz[s.back()][j]) le[1+s.back()][j]=i-1, s.pop_back();
s.push_back(i);
}
while(s.size()>0) le[1+s.back()][j]=-1, s.pop_back();
for (int i=n-1; i>=0; i--) {
while(s.size()>0 && sz[i][j]>=sz[s.back()][j]) fel[-1+s.back()][j]=i+1, s.pop_back();
if (i) s.push_back(i);
}
while(s.size()>0) fel[-1+s.back()][j]=-1, s.pop_back();
}
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
if (i==0 || j==0 || i==n-1 || j==m-1) {
bal[i][j]=jobb[i][j]=le[i][j]=fel[i][j]=-c;
}
}
}
for (int i=1; i<n-1; i++) {
for (int j=1; j<m-1; j++) {
int pos;
pos=jobb[i][j];
if (pos!=-1) {
if (jobb[i-1][j]==pos) cjobb[i][j]=cjobb[i-1][j];
if (bal[i-1][pos]==j) cjobb[i][j]=cbal[i-1][pos];
cjobb[i][j]++;
}
pos=bal[i][j];
if (pos!=-1) {
if (bal[i-1][j]==pos) cbal[i][j]=cbal[i-1][j];
if (jobb[i-1][pos]==j) cbal[i][j]=cjobb[i-1][pos];
cbal[i][j]++;
}
}
}
for (int j=1; j<m-1; j++) {
for (int i=1; i<n-1; i++) {
int pos;
pos=le[i][j];
if (pos!=-1) {
if (le[i][j-1]==pos) cle[i][j]=cle[i][j-1];
if (fel[pos][j-1]==i) cle[i][j]=cfel[pos][j-1];
cle[i][j]++;
}
pos=fel[i][j];
if (pos!=-1) {
if (fel[i][j-1]==pos) cfel[i][j]=cfel[i][j-1];
if (le[pos][j-1]==i) cfel[i][j]=cle[pos][j-1];
cfel[i][j]++;
}
}
}
for (int i=1; i<n-1; i++) {
for (int j=1; j<m-1; j++) {
int ba=bal[i][j], jo=jobb[i][j], fe=fel[i][j], l=le[i][j];
add(i, j, l, jo), add(i, ba, l, j), add(fe, j, i, jo), add(fe, ba, i, j);
if (jo!=-1) add(i, j, le[i][jo], jo);
if (l!=-1) add(i, j, l, jobb[l][j]);
}
}
return ans;
}
/*
int b1, b2;
vector<vector<int>> b3;
int main()
{
cin >> b1 >> b2;
b3.resize(b1);
for (int i=0; i<b1; i++) for (int j=0; j<b2; j++) {
int x; cin >> x;
b3[i].push_back(x);
}
cout << count_rectangles(b3);
return 0;
}
*/
Compilation message (stderr)
rect.cpp: In function 'void add(int, int, int, int)':
rect.cpp:13:38: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
13 | bool josor=(jobb[ns][ko]==no && cjobb[ns][ko]>=sdb || bal[ns][no]==ko && cbal[ns][no]>=sdb);
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
rect.cpp:14:39: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
14 | bool jooszlop=(le[ks][no]==ns && cle[ks][no]>=odb || fel[ns][no]==ks && cfel[ns][no]>=odb);
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
# | 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... |