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>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
int n, m;
namespace Sub1{
ll solve(vector<vector<int>> a){
ll ans = 0;
bool row_pref[n][n][m];
short col_pref[m][m][n];
memset(row_pref, 0, sizeof row_pref);
memset(col_pref, 0, sizeof col_pref);
for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j){
int ma = 0;
for(int k = i + 1; k + 1 < n; ++k){
maximize(ma, a[k][j]);
if (ma >= a[i][j]) break;
if (ma < a[k+1][j]){
row_pref[i][k+1][j] = 1;
}
}
ma = 0;
for(int k = j + 1; k + 1 < m; ++k){
maximize(ma, a[i][k]);
if (ma >= a[i][j]) break;
if (ma < a[i][k+1]){
col_pref[j][k+1][i] = 1;
}
}
}
// for(int i = 0; i<n; ++i) for(int j = i + 2; j < n; ++j) for(int k = 1; k < m; ++k) row_pref[i][j][k] += row_pref[i][j][k-1];
for(int i = 0; i<m; ++i) for(int j = i + 2; j < m; ++j) for(int k = 1; k < n; ++k) col_pref[i][j][k] += col_pref[i][j][k-1];
for(int i = 0; i<n; ++i) for(int x = i + 2; x < n; ++x) for(int j = 1; j + 1 < m; ++j) {
for(int k = j; k + 1 < m; ++k) {
if (!row_pref[i][x][k]) break;
int cnt2 = col_pref[j-1][k+1][x-1] - col_pref[j-1][k+1][i];
if (cnt2 == x - i - 1) ans++;
}
}
ll poopoo = 3LL * max(n, m) * max(n, m);
if (ans > poopoo) exit(-1);
return ans;
}
}
namespace Sub6{
bool check(vector<vector<int>> a){
for(int i= 0; i<n; ++i) for(int j = 0; j<m; ++j) if (a[i][j] > 1) return false;
return true;
}
ll solve(vector<vector<int>> a){
ll ans = 0;
vector<int> cnt0(m, 1e9), cnt1(m);
for(int i = 1; i<n; ++i){
for(int j = 0; j < m; ++j){
if (a[i-1][j] == 0){
cnt0[j]++;
cnt1[j] = 0;
}
else{
cnt0[j] = 0;
cnt1[j]++;
}
}
if (i == 1) continue;
vector<int> pillar; pillar.reserve(m);
for(int j = 0; j < m; ++j) if (cnt1[j]) pillar.push_back(j);
for(int j = 1; j < pillar.size(); ++j){
int l = pillar[j-1], r = pillar[j];
if (l + 1 == r) continue;
bool check = true;
for(int k = l + 1; k < r; ++k) if (!a[i][k]) check = false;
for(int k = l + 2; k < r; ++k) if (cnt0[k] != cnt0[k-1]) check = false;
if (check && cnt0[l+1] <= min(cnt1[l], cnt1[r]))
ans++;
}
}
return ans;
}
}
ll count_rectangles(vector<vector<int>> a) {
n = a.size(), m = a[0].size();
if (Sub6::check(a)) return Sub6::solve(a);
return Sub1::solve(a);
}
Compilation message (stderr)
rect.cpp: In function 'll Sub6::solve(std::vector<std::vector<int> >)':
rect.cpp:131:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
131 | for(int j = 1; j < pillar.size(); ++j){
| ~~^~~~~~~~~~~~~~~
# | 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... |