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++;
}
}
}
return ans;
}
} // namespace s1
struct PSA {
vector<int> psum;
PSA(const vector<int>& arr) : psum(sz(arr) + 1) {
partial_sum(begin(arr), end(arr), psum.begin() + 1);
}
int query(int l, int r) const {
return psum[r] - psum[l];
}
};
vector<pair<int, int>> solve1(const vector<int>& arr) {
int n = sz(arr);
vector<pair<int, int>> ans;
for (int i = 1; i < n - 1; i++) {
int cmx = 0;
for (int j = i; j < n - 1; j++) {
cmx = max(cmx, arr[j]);
if (arr[i - 1] > cmx && arr[j + 1] > cmx) {
ans.emplace_back(i, j);
}
}
}
assert(sz(ans) <= 2 * n + 10);
sort(begin(ans), end(ans));
return ans;
}
template <typename T>
vector<T> set_inter(const vector<T>& a, const vector<T>& b) {
vector<T> out;
set_intersection(begin(a), end(a), begin(b), end(b), back_inserter(out));
return out;
}
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);
}
}
vector<pair<int, int>> r_res[n], c_res[m];
for (int i = 0; i < n; i++) {
r_res[i] = solve1(arr[i]);
}
for (int i = 0; i < m; i++) {
vector<int> tmp;
for (int j = 0; j < n; j++) {
tmp.push_back(arr[j][i]);
}
c_res[i] = solve1(tmp);
}
vector<int> r_good[m][m];
for (int i = 0; i < n; i++) {
for (auto& [ql, qr] : r_res[i]) {
r_good[ql][qr].push_back(i);
}
}
long ans = 0;
for (int cl = m - 2; cl >= 1; cl--) {
vector<pair<int, int>> c_cur = c_res[cl];
for (int cr = cl; cr < m - 1; cr++) {
c_cur = set_inter(c_cur, c_res[cr]);
vector<int> c_bad(n, 1);
for (auto& a : r_good[cl][cr]) {
c_bad[a] = 0;
}
PSA p_bad(c_bad);
for (auto& [ql, qr] : c_cur) {
if (!p_bad.query(ql, qr + 1)) {
ans++;
}
}
}
}
return ans;
}
Compilation message (stderr)
rect.cpp: In function 'int64_t s1::solve(const std::vector<std::vector<int> >&)':
rect.cpp:61:10: warning: variable 'c2' set but not used [-Wunused-but-set-variable]
61 | auto c2 = [&](long n) -> long { return n * (n - 1) / 2; };
| ^~
# | 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... |