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"
using namespace std;
using ll = long long;
int n, m;
struct Node
{
int sz, par, v, x, xx, y, yy;
Node() { sz = par = v = x = xx = y = yy = -1; }
Node(int i, int j) {
v = m*i + j;
par = v;
sz = 1;
x = xx = i;
y = yy = j;
}
bool is_rect(void) {
assert(par == v);
assert(xx >= x and yy >= y);
// cout <<
return (xx - x + 1) * (yy - y + 1) == sz;
}
};
struct DSU
{
vector<Node> dsu;
DSU() {
dsu.resize(n*m);
}
int find(int x) {
if(dsu[x].par == x) {
return x;
}
return dsu[x].par = find(dsu[x].par);
}
bool merge(int x, int y) {
x = find(x);
y = find(y);
if(x == y) {
return false;
}
if(dsu[x].sz < dsu[y].sz) {
swap(x, y);
}
dsu[x].sz += dsu[y].sz;
dsu[y].par = x;
dsu[x].x = min(dsu[x].x, dsu[y].x);
dsu[x].xx = max(dsu[x].xx, dsu[y].xx);
dsu[x].y = min(dsu[x].y, dsu[y].y);
dsu[x].yy = max(dsu[x].yy, dsu[y].yy);
return true;
}
};
long long count_rectangles(std::vector<std::vector<int> > a) {
n = a.size();
m = a[0].size();
const int dx[] = {0, +1, 0, -1},
dy[] = {+1, 0, -1, 0};
vector<vector<bool> > in_dsu(n, vector<bool>(m));
DSU dsu;
for(int i = 1; i + 1 < n; ++i) {
for(int j = 1; j + 1 < m; ++j) {
if(a[i][j] == 1) {
continue;
}
in_dsu[i][j] = true;
dsu.dsu[m*i + j] = Node(i, j);
for(int d = 0; d < 4; ++d) {
int ii = i + dx[d], jj = j + dy[d];
if(in_dsu[ii][jj]) {
dsu.merge(m*i + j, m*ii + jj);
}
}
}
}
ll ans = 0;
for(int i = 0; i< n; ++i) {
for(int j = 0; j < m; ++j) {
const int vn = m*i + j;
if(!in_dsu[i][j] or dsu.dsu[vn].par != vn) {
continue;
}
// cout << "hi : " << i << ' ' << j << ' ' << dsu.dsu[vn].sz << ' ' << dsu.dsu[vn].y << ' ' << dsu.dsu[vn].yy << (dsu.dsu[vn].is_rect() ? " Yep " : " Nope ") << endl;
// cout << in_dsu[i][j] << ' ';
ans += dsu.dsu[vn].is_rect();
}
// cout << endl;
}
return ans;
}
/*
5 5
1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1
*/
# | 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... |