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<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << " "
#define nl cout << "\n"
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int st[2500+10], barr[2500+10];
long long count_rectangles(vector<vector<int>> a){
    int N = SZ(a), M = SZ(a[0]);
    vector<int> rt[N][M], dw[N][M];
    rep(i, 0, N - 1){
        vector<int> st;
        per(j, M - 1, 0){
            while(!st.empty() && a[i][j] > a[i][st.back()]){
                if(j + 1 < st.back()) rt[i][j].pb(st.back());
                st.pop_back();
            }
            if(!st.empty() && j + 1 < st.back()) rt[i][j].pb(st.back());
            if(!st.empty() && a[i][j] == a[i][st.back()]) st.pop_back();
            st.pb(j);
        }
    }
    rep(j, 0, M - 1){
        vector<int> st;
        per(i, N - 1, 0){
            while(!st.empty() && a[i][j] > a[st.back()][j]){
                if(i + 1 < st.back()) dw[i][j].pb(st.back());
                st.pop_back();
            }
            if(!st.empty() && i + 1 < st.back()) dw[i][j].pb(st.back());
            if(!st.empty() && a[i][j] == a[st.back()][j]) st.pop_back();
            st.pb(i);
        }
    }
    ll ans = 0;
    vector<pii> vt[M][M];
    rep(i, 0, N - 1){
        rep(j, 0, M - 1){
            for(int j2 : rt[i][j]){
                if(SZ(vt[j][j2]) && vt[j][j2].back().ss == i - 1)
                    vt[j][j2].back().ss = i;
                else vt[j][j2].pb({i, i});
            }
        }
    }
    rep(i, 1, N - 2){
        rep(j, 0, M - 1){
            int curj = j + 1;
            int stsz = 0, barrsz;
            for(int i2 : dw[i - 1][j + 1]) st[stsz++] = i2;
            for(int j2 : rt[i][j]){
                if(!stsz) break;
                while(curj < j2){
                    barrsz = 0;
                    int a = 0, b = 0;
                    while(a < SZ(dw[i - 1][curj]) && b < stsz){
                        if(dw[i - 1][curj][a] == st[b]){
                            barr[barrsz++] = st[b]; a++, b++;
                        } else if(dw[i - 1][curj][a] < st[b]) a++;
                        else b++;
                    }
                    swap(st, barr);
                    stsz = barrsz;
                    curj++;
                }
                int a = 0, b = 0;
                while(a < SZ(vt[j][j2]) && b < stsz){
                    int i2 = st[b];
                    if(vt[j][j2][a].ff <= i && i2 - 1 <= vt[j][j2][a].ss) ans++, b++;
                    else if(vt[j][j2][a].ss < i2 - 1) a++;
                    else b++;
                }
            }
        }
    }
    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... |