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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define pb push_back
#define mp make_pair
#define taskname "A"
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const int maxn = 2505;
vector<int> valA[maxn][maxn] , valB[maxn][maxn];
int m , n;
int h[maxn][maxn];
int L[maxn][maxn] , R[maxn][maxn] , U[maxn][maxn] , D[maxn][maxn];
void init(){
vector<int> s;
for(int i = 1 ; i <= m ; ++i){
s.clear();
for(int j = 1 ; j <= n ; ++j){
while(s.size() && h[i][s.back()] < h[i][j])s.pop_back();
if(s.size())L[i][j] = s.back();
else L[i][j] = 0;
s.pb(j);
}
s.clear();
for(int j = n ; j >= 1 ; --j){
while(s.size() && h[i][s.back()] < h[i][j])s.pop_back();
if(s.size())R[i][j] = s.back();
else R[i][j] = n + 1;
s.pb(j);
}
for(int j = 1 ; j <= n ; ++j){
if(L[i][j] != 0)valA[L[i][j]][j].pb(i);
if(R[i][j] != n + 1 && L[i][R[i][j]] != j)valA[j][R[i][j]].pb(i);
}
}
for(int j = 1 ; j <= n ; ++j){
s.clear();
for(int i = 1 ; i <= m ; ++i){
while(s.size() && h[s.back()][j] < h[i][j])s.pop_back();
if(s.size())U[i][j] = s.back();
else U[i][j] = 0;
s.pb(i);
}
s.clear();
for(int i = m ; i >= 1 ; --i){
while(s.size() && h[s.back()][j] < h[i][j])s.pop_back();
if(s.size())D[i][j] = s.back();
else D[i][j] = m + 1;
s.pb(i);
}
for(int i = 1 ; i <= m ; ++i){
if(U[i][j] != 0)valB[U[i][j]][i].pb(j);
if(D[i][j] != m + 1 && U[i][D[i][j]] != i)valB[i][D[i][j]].pb(j);
}
}
}
long long count_rectangles(std::vector<std::vector<int> > a) {
m = a.size();n = a[0].size();
for(int i = 1 ; i <= m ; ++i)for(int j = 1 ; j <= n ; ++j)h[i][j] = a[i - 1][j - 1];
init();
vector<array<int,4>> val;
for(int i = 1 ; i <= m ; ++i){
for(int j = 1 ; j <= n ; ++j){
array<int,4> cur = {L[i][j] , R[i][j] , U[i][j] , D[i][j]};
if(L[i][j] && U[i][j] && R[i][j] <= n && D[i][j] <= m)val.pb(cur);
}
}
sort(val.begin(),val.end());
int res = 0;
for(int i = 0 ; i < val.size() ; ++i){
if(i > 0 && val[i] == val[i - 1])continue;
int L = val[i][0] , R = val[i][1] , U = val[i][2] , D = val[i][3];
auto check = [&](vector<int> & val , int L , int R)->int{
auto it = lower_bound(val.begin(),val.end(),L);
auto it1 = lower_bound(val.begin(),val.end(),R);
if(it1 == val.end() || *it != L || *it1 != R)return 0;
return (it1 - it) == R - L;
};
// cout << L << " " << R << " " << U << " " << D << endl;
res += check(valA[L][R] , U + 1 , D - 1) && check(valB[U][D] , L + 1 , R - 1);
}
return res;
}
#ifdef LOCAL
#include "rect.h"
#include <cstdio>
#include <unistd.h>
#include <cassert>
#include <string>
using namespace std;
class InputReader {
private:
static const int SIZE = 4096;
int inputFileDescriptor;
char buf[SIZE];
int curChar;
int numChars;
public:
inline InputReader(int _inputFileDescriptor):
inputFileDescriptor(_inputFileDescriptor),
curChar(0),
numChars(0) {
}
inline void close() {
::close(inputFileDescriptor);
}
inline char read() {
assert(numChars != -1);
if (curChar >= numChars) {
curChar = 0;
numChars = ::read(inputFileDescriptor, buf, SIZE);
if (numChars == -1)
return -1;
}
return buf[curChar++];
}
inline int readInt() {
int c = eatWhite();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
assert(c >= '0' && c <= '9');
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
inline string readString() {
char c = eatWhite();
string res;
do {
res += c;
c = read();
} while (!isSpaceChar(c));
return res;
}
inline string readLine() {
string res;
while (true) {
char c = read();
if (c == '\n' || c == '\r' || c == -1)
break;
res += c;
}
return res;
}
inline char eatWhite() {
char c = read();
while (isSpaceChar(c))
c = read();
return c;
}
static inline bool isSpaceChar(char c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen(taskname".INP","r")){
freopen(taskname".INP", "r",stdin);
freopen(taskname".OUT", "w",stdout);
}
InputReader inputReader(STDIN_FILENO);
int n, m;
n = inputReader.readInt();
m = inputReader.readInt();
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = inputReader.readInt();
}
}
inputReader.close();
long long result = count_rectangles(a);
printf("%lld\n", result);
fclose(stdout);
return 0;
}
#endif // LOCAL
Compilation message (stderr)
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:83:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(int i = 0 ; i < val.size() ; ++i){
| ~~^~~~~~~~~~~~
# | 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... |