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;
 
#define SIZE 4096
 
vector<vector<ll>> gridSegCol;
vector<vector<ll>> gridSegRow;
 
 
bool isRowValid(int left, int right, int rowNum) {
	ll mx = -1;
	int l = left + SIZE;
	int r = right + SIZE;
	
	while (l <= r) {
		if (l%2 == 1) {
			mx = max(mx,gridSegRow[rowNum][l++]);
		}
		if (r%2 == 0) {
			mx = max(mx,gridSegRow[rowNum][r--]);
		}
		l/=2;
		r/=2;
	}
 
	//cerr << endl;
	//cerr << "VALUES FOR " << left << " " << right << " FOR ROW: " << rowNum << endl;
	//cerr << "MX: " << mx << " LEFT AND RIGHT: " << gridSegRow[rowNum][left+SIZE-1] << " " << gridSegRow[rowNum][right+SIZE+1] << endl;
	//cerr << "VALID?: " << ( (mx < gridSegRow[rowNum][left+SIZE-1]) && (mx < gridSegRow[rowNum][right+SIZE+1])) << endl;
 
	return ( (mx < gridSegRow[rowNum][left+SIZE-1]) && (mx < gridSegRow[rowNum][right+SIZE+1]));
}
 
bool isColValid(int left, int right, int colNum) {
 
	ll mx = -1;
	int l = left + SIZE;
	int r = right + SIZE;
	
	while (l <= r) {
		if (l%2 == 1) {
			mx = max(mx,gridSegCol[colNum][l++]);
		}
		if (r%2 == 0) {
			mx = max(mx,gridSegCol[colNum][r--]);
		}
		l/=2;
		r/=2;
	}
 
	//cerr << endl;
	//cerr << "VALUES FOR " << left << " " << right << " FOR COL: " << colNum << endl;
	//cerr << "MX: " << mx << " LEFT AND RIGHT: " << gridSegCol[colNum][left+SIZE-1] << " " << gridSegCol[colNum][right+SIZE+1] << endl;
	//cerr << "VALID?: " << ((mx < gridSegCol[colNum][left+SIZE-1]) && (mx < gridSegCol[colNum][right+SIZE+1])) << endl;
 
	return ( (mx < gridSegCol[colNum][left+SIZE-1]) && (mx < gridSegCol[colNum][right+SIZE+1]) );
}
 
// 1 1 1 2
// 1 1 1 3
// 1 1 2 1
 
bool isBoxValid(int topRow, int leftCol, int botRow, int rightCol) {
	for (int i = topRow; i <= botRow; i++) {
		if (!isRowValid(leftCol, rightCol, i)) {
			return false;
		}
	}
 
	for (int i = leftCol; i <= rightCol; i++) {
		if (!isColValid(topRow, botRow, i)) {
			return false;
		}
	}
 
	return true;
	
}
 
ll count_rectangles(std::vector<std::vector<int> > a) {
 
	//cout << "ROWS A.size: " << a.size() << endl;
	//cout << "COLS a[0].size: " << a[0].size() << endl;
 
	ll cnt = 0;
 
	vector<ll> temp;
	temp.resize(SIZE*2);
 
	//vector<vector<vector<bool>>> checkCol;
	//vector<vector<vector<bool>>> checkRow;
 
	gridSegRow.resize(a.size(), temp);
	gridSegCol.resize(a[0].size(), temp);
 
	//cerr << "HERE" << endl;
 
	for (int i = 0; i < a.size(); i++) {
		for (int j = 0; j < a[0].size(); j++) {
			gridSegRow[i][j+SIZE] = a[i][j];
			gridSegCol[j][i+SIZE] = a[i][j];
		}
	}
 
	//cerr << "Done" << endl;
 
	for (int i = 0; i < a.size(); i++) {
		for (int j = SIZE-1; j > 0; j--) {
			gridSegRow[i][j] = max( gridSegRow[i][j*2], gridSegRow[i][j*2+1]);
		}
	}
 
	for (int i = 0; i < a[0].size(); i++) {
		for (int j = SIZE-1; j > 0; j--) {
			gridSegCol[i][j] = max( gridSegCol[i][j*2], gridSegCol[i][j*2+1]);
		}
	}
 
	//cerr << gridSegCol[2][3+SIZE] << " SHOULD BE " << a[2][3];
	
	for (int i = 1; i < a.size()-1; i++) {
		for (int j = 1; j < a[0].size()-1; j++) {
			for (int k = i; k < a.size()-1; k++) {
				for (int l = j; l < a[0].size()-1; l++) {
 
					//cerr << endl;
					//cerr << endl;
					//cerr << "Testing: " << i << " " << j << " " << k << " " << l << endl;
					bool isValid = isBoxValid(i, j, k, l);
					if (isValid) {
						//cerr << "BOX DIMENSIONS: " << i << " " << j << " " << k << " " << l << endl;
						cnt++;
					}
				}
			}
		}
	}
 
	return cnt;
 
}
Compilation message (stderr)
rect.cpp: In function 'll count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:101:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |  for (int i = 0; i < a.size(); i++) {
      |                  ~~^~~~~~~~~~
rect.cpp:102:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |   for (int j = 0; j < a[0].size(); j++) {
      |                   ~~^~~~~~~~~~~~~
rect.cpp:110:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |  for (int i = 0; i < a.size(); i++) {
      |                  ~~^~~~~~~~~~
rect.cpp:116:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |  for (int i = 0; i < a[0].size(); i++) {
      |                  ~~^~~~~~~~~~~~~
rect.cpp:124:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |  for (int i = 1; i < a.size()-1; i++) {
      |                  ~~^~~~~~~~~~~~
rect.cpp:125:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |   for (int j = 1; j < a[0].size()-1; j++) {
      |                   ~~^~~~~~~~~~~~~~~
rect.cpp:126:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  126 |    for (int k = i; k < a.size()-1; k++) {
      |                    ~~^~~~~~~~~~~~
rect.cpp:127:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  127 |     for (int l = j; l < a[0].size()-1; l++) {
      |                     ~~^~~~~~~~~~~~~~~| # | 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... |