This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of Allah
#include <bits/stdc++.h>
#include "rect.h"
using namespace std;
typedef		long long int			ll;
typedef		long double				ld;
typedef		pair<int, int>			pii;
typedef		pair<ll, ll>			pll;
typedef		complex<ld>				cld;
#define		all(x)					(x).begin(),(x).end()
#define		len(x)					((ll) (x).size())
#define		F						first
#define		S						second
#define		pb						push_back
#define		sep						' '
#define		endl					'\n'
#define		Mp						make_pair
#define		kill(x)					cout << x << '\n', exit(0)
#define		set_dec(x)				cout << fixed << setprecision(x);
#define		file_io(x,y)			freopen(x, "r", stdin); freopen(y, "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 2500 + 4;
int n, m;
int L1[maxn][maxn], R1[maxn][maxn];
int L2[maxn][maxn], R2[maxn][maxn];
vector<int> A1[maxn][maxn], A2[maxn][maxn];
vector<int> ls[maxn][maxn];
bool ok(int l1, int r1, int l2, int r2) {
	int i1 = lower_bound(all(A1[l1 - 1][r1 + 1]), l2) - A1[l1 - 1][r1 + 1].begin() + (r2 - l2);
	if (i1 >= len(A1[l1 - 1][r1 + 1]) || A1[l1 - 1][r1 + 1][i1] != r2) return 0;
	
	int i2 = lower_bound(all(A2[l2 - 1][r2 + 1]), l1) - A2[l2 - 1][r2 + 1].begin() + (r1 - l1);
	if (i2 >= len(A2[l2 - 1][r2 + 1]) || A2[l2 - 1][r2 + 1][i2] != r1) return 0;
	
	return 1;
}
ll count_rectangles(vector<vector<int>> A) {
	n = len(A); m = len(A[0]);
	
	for (int j = 0; j < m; j++) {
		for (int i = 0; i < n; i++) {
			for (L1[i][j] = i - 1; L1[i][j] != -1 && A[i][j] > A[L1[i][j]][j]; L1[i][j] = L1[L1[i][j]][j]);
		}
		for (int i = n - 1; i >= 0; i--) {
			for (R1[i][j] = i + 1; R1[i][j] != n && A[i][j] > A[R1[i][j]][j]; R1[i][j] = R1[R1[i][j]][j]);
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			for (L2[i][j] = j - 1; L2[i][j] != -1 && A[i][j] > A[i][L2[i][j]]; L2[i][j] = L2[i][L2[i][j]]);
		}
		for (int j = m - 1; j >= 0; j--) {
			for (R2[i][j] = j + 1; R2[i][j] != m && A[i][j] > A[i][R2[i][j]]; R2[i][j] = R2[i][R2[i][j]]);
		}
	}
	
	for (int j = 0; j < m; j++) {
		for (int i = 0; i < n; i++) {
			if (L1[i][j] != -1 && L1[i][j] + 1 <= i - 1) {
				A1[L1[i][j]][i].pb(j);
			}
			if (R1[i][j] != n && i + 1 <= R1[i][j] - 1 && A[i][j] != A[R1[i][j]][j]) {
				A1[i][R1[i][j]].pb(j);
			}
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (L2[i][j] != -1 && L2[i][j] + 1 <= j - 1) {
				A2[L2[i][j]][j].pb(i);
			}
			if (R2[i][j] != m && j + 1 <= R2[i][j] - 1 && A[i][j] != A[i][R2[i][j]]) {
				A2[j][R2[i][j]].pb(i);
			}
		}
	}
	
	ll ans = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (L1[i][j] != -1 && L1[i][j] + 1 <= i - 1) {
				ls[L1[i][j]][j].pb(i);
			}
			if (R1[i][j] != n && i + 1 <= R1[i][j] - 1 && A[i][j] != A[R1[i][j]][j]) {
				ls[i][j].pb(R1[i][j]);
			}
		}
	}
	for (int i = 1; i < n - 1; i++) {
		for (int j = 0; j < m; j++) {
			if (L2[i][j] != -1 && L2[i][j] + 1 <= j - 1) {
				for (int x : ls[i - 1][j - 1]) {
					ans += ok(i, x - 1, L2[i][j] + 1, j - 1);
				}
			}
			if (R2[i][j] != m && j + 1 <= R2[i][j] - 1 && A[i][j] != A[i][R2[i][j]]) {
				for (int x : ls[i - 1][j + 1]) {
					ans += ok(i, x - 1, j + 1, R2[i][j] - 1);
				}
			}
		}
	}
	
	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... |