/**
* Author: Tenjin
* Created: 27.04.2022 18:58
* Why am I so stupid? :c
* Slishkom slab
**/
#include <bits/stdc++.h>
// #pragma GCC optimize("inline")
// #pragma GCC optimize("-fgcse,-fgcse-lm")
// #pragma GCC optimize("-ftree-pre,-ftree-vrp")
// #pragma GCC optimize("-ffast-math")
// #pragma GCC optimize("-fipa-sra")
// #pragma GCC optimize("-fpeephole2")
// #pragma GCC optimize("-fsched-spec")
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("unroll-loops")
using namespace std;
#define ll long long
#define pr pair
#define vt vector
#define ff first
#define ss second
#define sz(a) (int)a.size()
#define pb push_back
#define pf push_front
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
template<typename T> T get_rand(T l, T r) {
random_device rd;
mt19937 gen(rd());
return uniform_int_distribution<T>(l, r)(gen);
}
template<class A> void read(vt<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) { cin >> x; }
void read(double& d) { string t; read(t); d = stod(t); }
void read(long double& d) { string t; read(t); d = stold(t); }
template<class H, class... T> void read(H& h, T&... t) { read(h); read(t...); }
template<class A> void read(vt<A>& x) { for (auto& a : x) read(a); }
template<class A, size_t S> void read(array<A, S>& x) { for (auto& a : x) read(a); }
string to_string(char c) { return string(1, c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char* s) { return string(s); }
string to_string(string s) { return s; }
string to_string(vt<bool> v) { string res; for (int i = 0; i < sz(v); ++i) res += char('0' + v[i]); return res; }
template<size_t S> string to_string(bitset<S> b) { string res; for (int i = 0; i < S; ++i) res += char('0' + b[i]); return res; }
template<class T> string to_string(T v) { bool f = 1; string res; for (auto x : v) { if (!f) res += ' '; f = 0; res += to_string(x); } return res; }
template<class A> void write(A x) { cout << to_string(x); }
template<class H, class... T> void write(const H& h, const T&... t) { write(h); write(t...); }
void print() { write("\n"); }
template<class H, class... T> void print(const H& h, const T&... t) { write(h); if (sizeof...(t)) write(' '); print(t...); }
void freop(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int MOD = 1e9 + 7;
const ll INF = 1e14;
const int M = 4e5 + 5;
const double long pi = acos(-1);
const double long eps = 1e-12;
const int sq = 700;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
/* ll vs int*/
const int N = 1e6 + 5;
const ll X = 3e3;
const ll Y = X * X * X;
bool valid[2501][2501];
ll count_rectangles(vt<vt<int>> a) {
int n = sz(a);
int m = sz(a[0]);
vt<vt<int>> L(n, vt<int> (m, -1)), R(n, vt<int> (m, -1));
vt<vt<int>> L1(n, vt<int> (m, -1)), R1(n, vt<int> (m, -1));
for(int i = 0; i < n; ++i) {
vt<pr<int, int> > st;
for(int j = 0; j < m; ++j) {
valid[i][j] = 1;
while(!st.empty() && st.back().ff <= a[i][j]) st.pop_back();
if(!st.empty()) L[i][j] = st.back().ss;
else L[i][j] = -1;
st.push_back({a[i][j], j});
}
st.clear();
for(int j = m - 1; j >= 0; --j) {
while(!st.empty() && st.back().ff <= a[i][j]) st.pop_back();
if(!st.empty()) R[i][j] = st.back().ss;
else R[i][j] = m;
st.push_back({a[i][j], j});
}
}
for(int i = 0; i < m; ++i) {
vt<pr<int, int> > st;
for(int j = 0; j < n; ++j) {
while(!st.empty() && st.back().ff <= a[j][i]) st.pop_back();
if(!st.empty()) L1[j][i] = st.back().ss;
else L1[j][i] = -1;
st.push_back({a[j][i], j});
}
st.clear();
for(int j = n - 1; j >= 0; --j) {
while(!st.empty() && st.back().ff <= a[j][i]) st.pop_back();
if(!st.empty()) R1[j][i] = st.back().ss;
else R1[j][i] = n;
st.push_back({a[j][i], j});
}
}
set<pr<int, int>> last;
for(int i = 0; i < n; ++i) {
set<pr<int, int>> nxt;
for(int j = 0; j < m; ++j) {
int limL = L1[i][j], limR = i;
// print(i, j, limL, limR);
if(limL == -1) {
valid[i][j] = 0;
continue;
}
int curL = L[i][j], curR = R[i][j];
// print(curL, curR);
if(curL == -1 || curR == m) {
valid[i][j] = 0;
continue;
}
curL *= n;
curL += curR;
auto fnd = last.lower_bound({curL, 0});
int cur = 0;
if(fnd != last.end() && (*fnd).ff == curL) {
cur = (*fnd).ss;
}
cur++;
//print(cur);
if(cur < (limR - limL)) {
valid[i][j] = 0;
}
//print(valid[i][j]);
nxt.insert({curL, cur});
}
swap(last, nxt);
}
last.clear();
for(int i = n - 1; i >= 0; --i) {
set<pr<int, int>> nxt;
for(int j = 0; j < m; ++j) {
int limL = i, limR = R1[i][j];
//print(i, j, limL, limR);
if(limR == n) {
valid[i][j] = 0;
continue;
}
int curL = L[i][j], curR = R[i][j];
// print(curL, curR);
if(curL == -1 || curR == m) {
valid[i][j] = 0;
continue;
}
curL *= n;
curL += curR;
auto fnd = last.lower_bound({curL, 0});
int cur = 0;
if(fnd != last.end() && (*fnd).ff == curL) {
cur = (*fnd).ss;
}
cur++;
//print(cur);
if(cur < (limR - limL)) {
valid[i][j] = 0;
}
//print(valid[i][j]);
nxt.insert({curL, cur});
}
swap(last, nxt);
}
last.clear();
for(int i = 0; i < m; ++i) {
set<pr<int, int>> nxt;
for(int j = 0; j < n; ++j) {
int limL = L[j][i], limR = i;
//print(j, i, limL, limR);
if(limL == -1) {
valid[j][i] = 0;
continue;
}
int curL = L1[j][i], curR = R1[j][i];
// print(curL, curR);
if(curL == -1 || curR == n) {
valid[j][i] = 0;
continue;
}
curL *= m;
curL += curR;
auto fnd = last.lower_bound({curL, 0});
int cur = 0;
if(fnd != last.end() && (*fnd).ff == curL) {
cur = (*fnd).ss;
}
cur++;
//print(cur);
if(cur < (limR - limL)) {
valid[j][i] = 0;
}
//print(valid[j][i]);
nxt.insert({curL, cur});
}
swap(last, nxt);
}
last.clear();
for(int i = m - 1; i >= 0; --i) {
set<pr<int, int>> nxt;
for(int j = 0; j < n; ++j) {
int limL = i, limR = R[j][i];
if(limR == m) {
valid[j][i] = 0;
continue;
}
int curL = L1[j][i], curR = R1[j][i];
if(curL == -1 || curR == n) {
valid[j][i] = 0;
continue;
}
curL *= m;
curL += curR;
auto fnd = last.lower_bound({curL, 0});
int cur = 0;
if(fnd != last.end() && (*fnd).ff == curL) {
cur = (*fnd).ss;
}
cur++;
if(cur < (limR - limL)) {
valid[j][i] = 0;
}
nxt.insert({curL, cur});
}
swap(last, nxt);
}
//vt<pr<int, int>> ord
// set<pr<int, int>> have;
int ans = 0;
vt<pr<ll, int>> an;
for(int i = 1; i < n - 1; ++i) {
for(int j = 1; j < m - 1; ++j) {
ll cur = L[i][j] * X + R[i][j];
ll cur1 = L1[i][j] * X + R1[i][j];
cur *= Y;
cur += cur1;
an.push_back({cur, valid[i][j]});
}
}
sort(all(an));
for(int i = 0; i < sz(an); ++i) {
if(i && an[i].ff == an[i - 1].ff) continue;
ans += an[i].ss;
}
/*int ans = 0;
for(auto cur : ord) ans += valid[cur.ff][cur.ss];*/
return ans;
}
int main() {
// freopen("in1.txt", "r", stdin);
int n, m;
read(n, m);
vector<vector<int> > a(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
read(a[i][j]);
}
}
long long result = count_rectangles(a);
printf("%lld\n", result);
return 0;
}
/*
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freop("");
int t = 1;
//read(t);
for (int i = 1; i <= t; ++i) {
//write("Case #" + to_string(i) + ": ");
solve();
}
return 0;
}*/
Compilation message
rect.cpp: In function 'void freop(std::string)':
rect.cpp:64:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rect.cpp:65:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccaESWde.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/cc1TpvYd.o:rect.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status