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, j, r}});
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);
if (pair_row.size() + pair_col.size() >= 1e7) return 0;
vector<short> candidate_row[n][m], candidate_col[n][m];
vector<short> idx_col[n][n], idx_row[m][m];
for(array<short, 3> j: pair_row){
int i = j[0];
candidate_row[i][j[1] + 1].push_back(j[2] - 1);
idx_row[j[1]][j[2]].push_back(i);
}
for(array<short, 3> i: pair_col){
int j = i[0];
candidate_col[i[1] + 1][j].push_back(i[2] - 1);
idx_col[i[1]][i[2]].push_back(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 y: candidate_row[i][j]) for(short x: candidate_col[i][j]){
// check the rectangle (i, j, x, y)
// printArr(idx_row[j-1][y+1]);
int cnt1 = upper_bound(ALL(idx_row[j-1][y+1]), x) - lower_bound(ALL(idx_row[j-1][y+1]), i);
if (cnt1 != x - i + 1) continue;
int cnt2 = upper_bound(ALL(idx_col[i-1][x+1]), y) - lower_bound(ALL(idx_col[i-1][x+1]), j);
if (cnt2 != y - j + 1) continue;
ans++;
}
}
return ans;
}
}
ll count_rectangles(vector<vector<int>> a) {
return Sub7::solve(a);
}
Compilation message (stderr)
rect.cpp: In function 'std::vector<std::array<short int, 3> > Sub7::generate_pair(std::vector<std::vector<int> >)':
rect.cpp:68:54: warning: narrowing conversion of 'i' from 'int' to 'short int' [-Wnarrowing]
68 | if (r != j + 1) what.push_back({{i, j, r}});
| ^
rect.cpp:68:57: warning: narrowing conversion of 'j' from 'int' to 'short int' [-Wnarrowing]
68 | if (r != j + 1) what.push_back({{i, j, r}});
| ^
rect.cpp:68:60: warning: narrowing conversion of 'r' from 'int' to 'short int' [-Wnarrowing]
68 | if (r != j + 1) what.push_back({{i, j, r}});
| ^
rect.cpp:72:38: warning: narrowing conversion of 'i' from 'int' to 'short int' [-Wnarrowing]
72 | what.push_back({{i, j, st.back()}});
| ^
rect.cpp:72:41: warning: narrowing conversion of 'j' from 'int' to 'short int' [-Wnarrowing]
72 | what.push_back({{i, j, st.back()}});
| ^
rect.cpp:72: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]
72 | 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... |