이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int c=5002;
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];
vector<pair<pair<int, int>, pair<int, int> > > ch;
void add(int a, int b, int c, int d) {ch.push_back({{a, b}, {c, d}});}
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]=bal[i-1][pos];
cjobb[i][j]++;
}
pos=bal[i][j];
if (pos!=-1) {
if (bal[i-1][j]==pos) cbal[i][j]=bal[i-1][j];
if (jobb[i-1][pos]==j) cbal[i][j]=jobb[i-1][pos];
cbal[i][j]++;
}
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]=fel[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]=le[pos][j-1];
cfel[i][j]++;
}
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
int ba=bal[i][j], jo=jobb[i][j], fe=fel[i][j], l=le[i][j];
if (i==3 && j==3) {
//cout << "fontos " << i << " " << j << "\n" << "iranyok " << ba << " " << jo << " " << fe << " " << l << "\n";
}
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]);
}
}
sort(ch.begin(), ch.end());
for (int i=0; i<ch.size(); i++) if (i==0 || ch[i]!=ch[i-1]) {
ks=ch[i].first.first, ko=ch[i].first.second, ns=ch[i].second.first, no=ch[i].second.second, mini=min({ks, ko, ns, no});
if (mini!=-1 && ks && ko && ns<n-1 && no<m-1 && ks<=ns && ko<=no) {
if (ks==3) {
//cout << "proba " << ks << " " << ko << " " << ns << " " << no << "\n";
}
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) {
//cout << "fontos " << ks << " " << ko << " " << ns << " " << no << "\n";
ans++;
}
}
}
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;
}
*/
/*
6 5
4 8 7 5 6
7 4 10 3 5
9 7 20 14 2
9 14 7 3 6
5 7 5 2 7
4 5 13 5 6
*/
컴파일 시 표준 에러 (stderr) 메시지
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:102:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for (int i=0; i<ch.size(); i++) if (i==0 || ch[i]!=ch[i-1]) {
| ~^~~~~~~~~~
rect.cpp:109:42: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
109 | bool josor=(jobb[ns][ko]==no && cjobb[ns][ko]>=sdb || bal[ns][no]==ko && cbal[ns][no]>=sdb);
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
rect.cpp:110:43: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
110 | 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... |