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());
}
namespace Sub7{
vector<array<short, 3>> generate_pair(vector<vector<int>> a){
int n = a.size(), m = a[0].size();
vector<array<short, 3>> what;
for(int i = 1; i+1<n; ++i){
vector<int> st; st.reserve(m);
for(int j = m-1; j>=0; --j){
int best = -1;
while(st.size() && a[i][st.back()] <= a[i][j]){
int r = st.back();
maximize(best, a[i][r]);
if (r != j + 1) what.push_back({{i, r, j}});
st.pop_back();
}
if (st.size() && best < a[i][j] && st.back() != j + 1){
what.push_back({{i, j, st.back()}});
}
st.push_back(j);
}
// sort(ALL(what[i]));
}
return what;
}
ll solve(vector<vector<int>> a){
int n, m;
n = a.size(), m = a[0].size();
ll ans = 0;
vector<vector<int>> b(m, vector<int> (n));
for(int i = 0; i<n; ++i) for(int j = 0; j < m; ++j) b[j][i] = a[i][j];
vector<array<short, 3>> pair_row = generate_pair(a), pair_col = generate_pair(b);
const int N = pair_row.size() + pair_col.size();
vector<short> candidate_row[n][m], candidate_col[n][m];
int l_col[n][n], r_col[n][n], l_row[m][m], r_row[m][m];
memset(l_col, 0, sizeof l_col);
memset(r_col, 0, sizeof r_col);
memset(l_row, 0, sizeof l_row);
memset(r_row, 0, sizeof r_row);
short tape[N];
for(auto &j: pair_row){
int i = j[0];
if (j[1] <= j[2]){
candidate_row[i][j[1] + 1].push_back(j[2] - 1);
}
else{
candidate_row[i][j[1] - 1].push_back(j[2] + 1);
swap(j[1], j[2]);
}
l_row[j[1]][j[2]]++;
}
// block_of_code{
// // check
// bool check = true;
// for(int i = 0; i < n; ++i) for(int j: a[i]) if (j > 1) check = false;
// if (check == false && min(n, m) >= 2000) return 0;
// }
for(auto &i: pair_col){
int j = i[0];
if (i[1] <= i[2]){
candidate_col[i[1] + 1][j].push_back(i[2] - 1);
}
else{
candidate_col[i[1] - 1][j].push_back(i[2] + 1);
swap(i[1], i[2]);
}
l_col[i[1]][i[2]]++;
}
int sum = 0;
for(int i = 0; i<n; ++i) for(int j = i; j<n; ++j) {
r_col[i][j] = sum;
sum += l_col[i][j];
l_col[i][j] = r_col[i][j];
}
for(int i = 0; i<m; ++i) for(int j = i; j<m; ++j) {
r_row[i][j] = sum;
sum += l_row[i][j];
l_row[i][j] = r_row[i][j];
}
for(array<short, 3> j: pair_row){
int i = j[0];
tape[r_row[j[1]][j[2]]++] = i;
}
for(array<short, 3> i: pair_col){
int j = i[0];
tape[r_col[i[1]][i[2]]++] = j;
}
for(int i = 1; i+1<n; ++i)
for(int j = 1; j+1<m; ++j) if (candidate_row[i][j].size() && candidate_col[i][j].size()){
for(short _j: candidate_row[i][j]) for(short _i: candidate_col[i][j]){
// check the rectangle (i, j, x, y)
// printArr(idx_row[j-1][y+1]);
int u = i, v = j, x = _i, y = _j;
if (u >= x) swap(u, x);
if (v >= y) swap(v, y);
#define get1(x, y) tape + l_row[x][y], tape + r_row[x][y]
#define get2(x, y) tape + l_col[x][y], tape + r_col[x][y]
int cnt1 = upper_bound(get1(v-1, y+1), x) - lower_bound(get1(v-1, y+1), u);
if (cnt1 != x - u + 1) continue;
int cnt2 = upper_bound(get2(u-1, x+1), y) - lower_bound(get2(u-1, x+1), v);
if (cnt2 != y - v + 1) continue;
ans++;
}
}
return ans;
}
}
ll count_rectangles(vector<vector<int>> a) {
return Sub7::solve(a);
}
// int main(void){
// ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
// clock_t start = clock();
// int n,m; cin >> n >> m;
// vector<vector<int>> a(n, vector<int>(m));
// for(int i = 0; i<n; ++i) for(int j = 0; j<m; ++j) cin >> a[i][j];
// cout << count_rectangles(a) << "\n";
// cerr << "Time elapsed: " << clock() - start << " ms\n";
// return 0;
// }
Compilation message (stderr)
rect.cpp: In function 'std::vector<std::array<short int, 3> > Sub7::generate_pair(std::vector<std::vector<int> >)':
rect.cpp:69:54: warning: narrowing conversion of 'i' from 'int' to 'short int' [-Wnarrowing]
69 | if (r != j + 1) what.push_back({{i, r, j}});
| ^
rect.cpp:69:57: warning: narrowing conversion of 'r' from 'int' to 'short int' [-Wnarrowing]
69 | if (r != j + 1) what.push_back({{i, r, j}});
| ^
rect.cpp:69:60: warning: narrowing conversion of 'j' from 'int' to 'short int' [-Wnarrowing]
69 | if (r != j + 1) what.push_back({{i, r, j}});
| ^
rect.cpp:73:38: warning: narrowing conversion of 'i' from 'int' to 'short int' [-Wnarrowing]
73 | what.push_back({{i, j, st.back()}});
| ^
rect.cpp:73:41: warning: narrowing conversion of 'j' from 'int' to 'short int' [-Wnarrowing]
73 | what.push_back({{i, j, st.back()}});
| ^
rect.cpp:73:51: warning: narrowing conversion of 'st.std::vector<int>::back()' from '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} to 'short int' [-Wnarrowing]
73 | what.push_back({{i, j, st.back()}});
| ~~~~~~~^~
# | 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... |