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;
using ll = int;
using lll = long long;
const ll maxn = 2500;
long long count_rectangles(std::vector<std::vector<int> > a) {
	ll n = a.size(), m = a[0].size();
	if (n<3||m<3) return 0;
	ll sub6 = 1;
	for(auto &o : a) for(ll &i : o) if (i>1) sub6 = 0;
	if (0&&sub6){
		//cerr << "sub6\n";
		ll ans = 0;
		//cerr << "sub6\n";
		for(ll w = 1;w<n-1;w++){
			for(ll i = 1;i<n-w;i++){
				ll c = 0;
				for(ll j = 1;j<m-1;j++){
					if(a[i][j])ans+=c*(c+1),c=0;
					else c++;
					a[i][j] |= a[i+1][j];
				}
				ans+=c*(c+1);
			}
			//cerr << ans << "\n";
		}
		return ans/2;
	}
	vector<vector<pair<ll,ll>>> row(n),col(m);
	for(ll i = 0;i<n;i++){
		vector<pair<ll,ll>> st;
		for(ll j = 0;j<m;j++){
			while(st.size()&&st.back().first<a[i][j]){
				if(j>st.back().second+1)row[i].push_back({st.back().second,j});
				st.pop_back();
			}
			if(st.size()){
				if(j>st.back().second+1)row[i].push_back({st.back().second,j});
			}
			if (st.size()&&st.back().first==a[i][j]) st.pop_back();
			st.push_back({a[i][j],j});
		}
	}
	for(ll j = 0;j<m;j++){
		vector<pair<ll,ll>> st;
		for(ll i = 0;i<n;i++){
			while(st.size()&&st.back().first<a[i][j]){
				if(i>st.back().second+1)col[j].push_back({st.back().second,i});
				st.pop_back();
			}
			if(st.size()){
				if(i>st.back().second+1)col[j].push_back({st.back().second,i});
			}
			if (st.size()&&st.back().first==a[i][j]) st.pop_back();
			st.push_back({a[i][j],i});
		}
	}
	for(auto &o : row) sort(o.begin(),o.end());
	for(auto &o : col) sort(o.begin(),o.end());
	vector<vector<ll>> down(n,vector<ll>(m,0)),up(n,vector<ll>(m,n));
	for(ll i = 0;i<m;i++){
		for(auto [l,r] : col[i]){
			down[l][i] = r;
			up[l][i] = min(up[l][i],r);
		}
	}
	lll ans = 0;
	for(ll r1 = 1;r1<n-1;r1++){
		vector<pair<ll,ll>> cur = row[r1];
		vector<pair<ll,ll>> nw;
		for(ll r2 = r1;r2<n-1;r2++){
			if(r2>r1){
				for(auto &o : cur){
					if (binary_search(row[r2].begin(),row[r2].end(),o)){
						nw.push_back(o);
					}
				}
				nw.swap(cur);
				nw.clear();
			}
			pair<ll,ll> t = {r1-1,r2+1};
			vector<ll> ok(m,0);
			for(ll i = 0;i<m;i++) ok[i] = down[r1-1][i]>r2&&up[r1-1][i]-1<=r2&&binary_search(col[i].begin(),col[i].end(),t);
			vector<ll> p(m+1,0);
			for(ll i = 0;i<m;i++) p[i+1] = p[i]+ok[i];
			for(auto &o : cur){
				if (p[o.second]-p[o.first+1]==(o.second-o.first-1)) ans++;
			}
		}
	}
	return ans;
}
| # | 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... |