Submission #291115

#TimeUsernameProblemLanguageResultExecution timeMemory
291115IgorIRectangles (IOI19_rect)C++17
25 / 100
5056 ms123456 KiB
#include <bits/stdc++.h>

using namespace std;

const int INF = 88888888;
const int N = 2502;

typedef vector<vector<int> > vvi;
typedef vector<int> vi;

#define all(x) (x).begin(), (x).end()

int le[N][N], ri[N][N], up[N][N], down[N][N];
int go_le[2][N][N], go_ri[2][N][N], go_up[2][N][N], go_down[2][N][N];

long long count_rectangles(vector<vector<int> > a)
{
    int n = a.size(), m = a[0].size();
    for (int i = 0; i < n; i++)
    {
        vector<pair<int, int> > st;
        st.push_back({-1, INF});
        for (int j = 0; j < m; j++)
        {
            while (st.back().second < a[i][j]) st.pop_back();
            le[i][j] = st.back().first;
            st.push_back({j, a[i][j]});
        }
    }
    for (int i = 0; i < n; i++)
    {
        vector<pair<int, int> > st;
        st.push_back({m, INF});
        for (int j = m - 1; j >= 0; j--)
        {
            while (st.back().second < a[i][j]) st.pop_back();
            ri[i][j] = st.back().first;
            st.push_back({j, a[i][j]});
        }
    }
    for (int j = 0; j < m; j++)
    {
        vector<pair<int, int> > st;
        st.push_back({-1, INF});
        for (int i = 0; i < n; i++)
        {
            while (st.back().second < a[i][j]) st.pop_back();
            up[i][j] = st.back().first;
            st.push_back({i, a[i][j]});
        }
    }
    for (int j = 0; j < m; j++)
    {
        vector<pair<int, int> > st;
        st.push_back({n, INF});
        for (int i = n - 1; i >= 0; i--)
        {
            while (st.back().second < a[i][j]) st.pop_back();
            down[i][j] = st.back().first;
            st.push_back({i, a[i][j]});
        }
    }
    for (int i = 1; i < n; i++)
    {
        vector<pair<int, int> > st;
        st.push_back({-1, INF});
        vector<int> pack;
        pack.push_back(n);
        for (int j = 0; j < m; j++)
        {
            int unpack = n;
            while (st.back().second < a[i][j])
            {
                unpack = min(unpack, down[i - 1][st.back().first]);
                st.pop_back();
                unpack = min(unpack, pack.back());
                pack.pop_back();
            }
            st.push_back({j, a[i][j]});
            pack.push_back(unpack);
            go_down[0][i][j] = unpack;
        }
    }
    for (int i = 1; i < n; i++)
    {
        vector<pair<int, int> > st;
        st.push_back({m, INF});
        vector<int> pack;
        pack.push_back(m);
        for (int j = m - 1; j >= 0; j--)
        {
            int unpack = n;
            while (st.back().second < a[i][j])
            {
                unpack = min(unpack, down[i - 1][st.back().first]);
                st.pop_back();
                unpack = min(unpack, pack.back());
                pack.pop_back();
            }
            st.push_back({j, a[i][j]});
            pack.push_back(unpack);
            go_down[1][i][j] = unpack;
        }
    }
    for (int i = 0; i + 1 < n; i++)
    {
        vector<pair<int, int> > st;
        st.push_back({-1, INF});
        vector<int> pack;
        pack.push_back(-1);
        for (int j = 0; j < m; j++)
        {
            int unpack = -1;
            while (st.back().second < a[i][j])
            {
                unpack = max(unpack, up[i + 1][st.back().first]);
                st.pop_back();
                unpack = max(unpack, pack.back());
                pack.pop_back();
            }
            st.push_back({j, a[i][j]});
            pack.push_back(unpack);
            go_up[0][i][j] = unpack;
        }
    }
    for (int i = 0; i + 1 < n; i++)
    {
        vector<pair<int, int> > st;
        st.push_back({-1, INF});
        vector<int> pack;
        pack.push_back(-1);
        for (int j = m - 1; j >= 0; j--)
        {
            int unpack = -1;
            while (st.back().second < a[i][j])
            {
                unpack = max(unpack, up[i + 1][st.back().first]);
                st.pop_back();
                unpack = max(unpack, pack.back());
                pack.pop_back();
            }
            st.push_back({j, a[i][j]});
            pack.push_back(unpack);
            go_up[1][i][j] = unpack;
        }
    }
    int ans = 0;
    for (int x1 = 1; x1 <= n - 2; x1++)
    {
        for (int x2 = x1; x2 <= n - 2; x2++)
        {
            for (int y1 = 1; y1 <= m - 2; y1++)
            {
                for (int y2 = y1; y2 <= m - 2; y2++)
                {
                    int t = 1;
                    for (int r = x1; r <= x2; r++) if (ri[r][y1 - 1] <= y2) t = 0;
                    for (int r = x1; r <= x2; r++) if (le[r][y2 + 1] >= y1) t = 0;
                    int D = 0;
                    if (a[x1][y1 - 1] <= a[x1][y2 + 1])
                    {
                        D = go_down[1][x1][y1 - 1];
                    }
                    else
                    {
                        D = go_down[0][x1][y2 + 1];
                    }
                    int U = 0;
                    if (a[x2][y1 - 1] <= a[x2][y2 + 1])
                    {
                        U = go_up[1][x2][y1 - 1];
                    }
                    else
                    {
                        U = go_up[0][x2][y2 + 1];
                    }
                    if (D <= x2 || x1 <= U) t = 0;
                    ans += t;
                }
            }
        }
    }
    return ans;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...