This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Ignut
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 123;
ll count_rectangles(vector<vector<int>> a) {
    int n = a.size(), m = a[0].size();
    int min_h[n][m], max_h[n][m], min_v[n][m], max_v[n][m];
    for (int i = 0; i < n; i ++) {
        for (int j = 0; j < m; j ++) {
            for (int ii = i; ii >= 0; ii --) {
                if (a[ii][j] > a[i][j]) break;
                min_v[i][j] = ii;
            }
            for (int ii = i; ii < n; ii ++) {
                if (a[ii][j] > a[i][j]) break;
                max_v[i][j] = ii;
            }
            for (int jj = j; jj >= 0; jj --) {
                if (a[i][jj] > a[i][j]) break;
                min_h[i][j] = jj;
            }
            for (int jj = j; jj < m; jj ++) {
                if (a[i][jj] > a[i][j]) break;
                max_h[i][j] = jj;
            }
        }
    }
    int minH[m][n][n], maxH[m][n][n], minV[m][n][n], maxV[m][n][n];
    for (int j = 0; j < m; j ++) {
        for (int l = 0; l < n; l ++) {
            minH[j][l][l] = min_h[l][j];
            maxH[j][l][l] = max_h[l][j];
            minV[j][l][l] = min_v[l][j];
            maxV[j][l][l] = max_v[l][j];
            for (int r = l + 1; r < n; r ++) {
                minH[j][l][r] = min(minH[j][l][r - 1], min_h[r][j]);
                maxH[j][l][r] = max(maxH[j][l][r - 1], max_h[r][j]);
                minV[j][l][r] = min(minV[j][l][r - 1], min_v[r][j]);
                maxV[j][l][r] = max(maxV[j][l][r - 1], max_v[r][j]);
            }
        }
    }
    int res = 0;
    for (int u = 1; u < n - 1; u ++) {
        for (int d = u; d < n - 1; d ++) {
            for (int l = 1; l < m - 1; l ++) {
                int mnh = INF, mxh = -INF, mnv = INF, mxv = -INF;
                for (int r = l; r < m - 1; r ++) {
                    mnh = min(mnh, minH[r][u][d]);
                    mxh = max(mxh, maxH[r][u][d]);
                    mnv = min(mnv, minV[r][u][d]);
                    mxv = max(mxv, maxV[r][u][d]);
                    if (mnh < l) break;
                    if (mnv < u) break;
                    if (mxv > d) break;
                    res += (mxh == r);
                }
            }
        } 
    }
    return res;
}
| # | 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... |