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/extc++.h"
using namespace std;
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t;
((cerr << " | " << u), ...);
cerr << endl;
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
using ll = long long;
#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))
namespace s1 {
constexpr int DIRS[4][2] {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
int n, m;
vector<vector<char>> vis;
vector<vector<int>> arr;
bool ibs(int x, int y) {
return 0 <= x && x < n && 0 <= y && y < m;
}
template <typename Cb>
void dfs(int x, int y, const Cb& cb) {
if (!ibs(x, y) || vis[x][y] || arr[x][y]) {
return;
}
cb(x, y);
vis[x][y] = true;
for (auto& [dx, dy] : DIRS) {
dfs(x + dx, y + dy, cb);
}
}
long solve(const vector<vector<int>>& _arr) {
arr = _arr;
n = sz(arr);
m = sz(arr[0]);
vis.resize(n, vector<char>(m));
long ans = 0;
auto c2 = [&](long n) -> long { return n * (n - 1) / 2; };
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (vis[i][j] || arr[i][j]) {
continue;
}
int mnx = 1e9, mxx = 0, mny = 1e9, mxy = 0, cnt = 0;
dfs(i, j, [&](int x, int y) -> void {
mnx = min(mnx, x);
mxx = max(mxx, x);
mny = min(mny, y);
mxy = max(mxy, y);
cnt++;
});
if (cnt == (mxx - mnx + 1) * (mxy - mny + 1) && mnx && mny &&
mxx < n - 1 && mxy < m - 1) {
ans += c2(mxx - mnx + 2) * c2(mxy - mny + 2);
}
}
}
return ans;
}
} // namespace s1
ll count_rectangles(vector<vector<int>> arr) {
int n = sz(arr), m = sz(arr[0]);
{
bool ok = true;
for (auto& a : arr) {
for (auto& b : a) {
ok &= 0 <= b && b <= 1;
}
}
if (ok) {
return s1::solve(arr);
}
}
}
Compilation message (stderr)
rect.cpp: In function 'll count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:90:9: warning: unused variable 'n' [-Wunused-variable]
90 | int n = sz(arr), m = sz(arr[0]);
| ^
rect.cpp:90:22: warning: unused variable 'm' [-Wunused-variable]
90 | int n = sz(arr), m = sz(arr[0]);
| ^
rect.cpp:103:1: warning: control reaches end of non-void function [-Wreturn-type]
103 | }
| ^
# | 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... |