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>
int presum[3000][3000];
using namespace std;
long long count_rectangles(std::vector<std::vector<int> > a) {
int n = a.size();
int m = a[0].size();
long long ans = 0;
for (int i = 0 ; i < n ; i++){
for (int j = 0 ; j < m ; j++){
if (j == 0)presum[i][j] = a[i][j];
else presum[i][j] = presum[i][j-1] + a[i][j];
}
}
for (int j = 0 ; j < m ; j++){
for (int i = 1 ; i < n ; i++){
presum[i][j] = presum[i - 1][j] + presum[i][j];
}
}
vector<pair<int,int>> zeros;
for (int i = 1 ; i < n - 1 ; i++){
for (int j = 1 ; j < m - 1 ; j++){
if (a[i][j] == 0)zeros.push_back({i,j});
}
}
for (int i = 0 ; i < zeros.size() ; i++){
for (int j = 0 ; j < zeros.size() ; j++){
int x0,y0,x1,y1;
x0 = zeros[i].first; x1 = zeros[j].first; y0 = zeros[i].second ; y1 = zeros[j].second;
if (x1 < x0 || y1 < y0)continue;
int big0, small0, rec01, rec02, sum0;
big0 = presum[x1][y1];
small0 = presum[x0 - 1][y0 - 1];
rec01 = presum[x1][y0 - 1];
rec02 = presum[x0 - 1][y1];
sum0 = big0 - rec01 - rec02 + small0;
if (sum0 != 0)continue;
big0 = presum[x1 + 1][y1 + 1];
small0 = (x0 == 1 || y0 == 1 ? 0 : presum[x0 - 2][y0 - 2]);
rec01 = (y0 == 1 ? 0 : presum[x1 + 1][y0 - 2]);
rec02 = (x0 == 1 ? 0 : presum[x0 - 2][y1 + 1]);
sum0 = big0 - rec01 - rec02 + small0;
int atfirst = sum0;
sum0-= (a[x1 + 1][y1 + 1] == 1 ? 1 : 0);
sum0-= (a[x0 - 1][y0 - 1] == 1 ? 1 : 0);
sum0-= (a[x1 + 1][y0 - 1] == 1? 1 : 0);
sum0-= (a[x0 - 1][y1 + 1] == 1? 1 : 0);
int should = x1 - x0 + 1;
should+= (y1 - y0 + 1);
should*= 2;
if (sum0 == should){
ans++;
// cout << "from (" << x0 << "," << y0 << ") to (" << x1 << "," << y1 << endl;
// cout << "should =" << should << endl;
// cout << "at first = " << atfirst << endl;
}
}
}
return ans;
}
Compilation message (stderr)
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:41:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0 ; i < zeros.size() ; i++){
~~^~~~~~~~~~~~~~
rect.cpp:42:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0 ; j < zeros.size() ; j++){
~~^~~~~~~~~~~~~~
rect.cpp:66:17: warning: unused variable 'atfirst' [-Wunused-variable]
int atfirst = sum0;
^~~~~~~
# | 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... |