Submission #291128

# Submission time Handle Problem Language Result Execution time Memory
291128 2020-09-04T17:39:10 Z IgorI Rectangles (IOI19_rect) C++17
Compilation error
0 ms 0 KB
#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 n, m;
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];

bool check_rect(int x1, int y1, int x2, int y2, vector<vector<int> > &a)
{
    if (x1 <= 0 || x2 >= n - 1 || y1 <= 0 || y2 >= m - 1) return 0;
    if (x1 > x2 || y1 > y2) return 0;
    if (!(down[x1 - 1][y1] == x2 + 1 || up[x2 + 1][y1] == x1 - 1)) return 0;
    if (!(down[x1 - 1][y2] == x2 + 1 || up[x2 + 1][y2] == x1 - 1)) return 0;
    if (!(ri[x1][y1 - 1] == y2 + 1 || le[x1][y2 + 1] == y1 - 1)) return 0;
    if (!(ri[x2][y1 - 1] == y2 + 1 || le[x2][y2 + 1] == y1 - 1)) return 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];
    }
    int R = 0;
    if (a[x1 - 1][y1] <= a[x2 + 1][y1])
    {
        R = go_ri[1][x1 - 1][y1];
    }
    else
    {
        R = go_ri[0][x2 + 1][y1];
    }
    int L = 0;
    if (a[x1 - 1][y2] <= a[x2 + 1][y2])
    {
        L = go_le[1][x1 - 1][y2];
    }
    else
    {
        L = go_le[0][x2 + 1][y2];
    }
    return (x2 < D && U < x1 && y2 < R && L < y1);
}

long long count_rectangles(vector<vector<int> > a)
{
    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(n);
        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({m, 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;
        }
    }
    for (int j = 1; j < m; j++)
    {
        vector<pair<int, int> > st;
        st.push_back({-1, INF});
        vector<int> pack;
        pack.push_back(m);
        for (int i = 0; i < n; i++)
        {
            int unpack = m;
            while (st.back().second < a[i][j])
            {
                unpack = min(unpack, ri[st.back().first][j - 1]);
                st.pop_back();
                unpack = min(unpack, pack.back());
                pack.pop_back();
            }
            st.push_back({i, a[i][j]});
            pack.push_back(unpack);
            go_ri[0][i][j] = unpack;
        }
    }
    for (int j = 1; j < m; j++)
    {
        vector<pair<int, int> > st;
        st.push_back({n, INF});
        vector<int> pack;
        pack.push_back(m);
        for (int i = n - 1; i >= 0; i--)
        {
            int unpack = m;
            while (st.back().second < a[i][j])
            {
                unpack = min(unpack, ri[st.back().first][j - 1]);
                st.pop_back();
                unpack = min(unpack, pack.back());
                pack.pop_back();
            }
            st.push_back({i, a[i][j]});
            pack.push_back(unpack);
            go_ri[1][i][j] = unpack;
        }
    }
    for (int j = 0; j + 1 < m; j++)
    {
        vector<pair<int, int> > st;
        st.push_back({-1, INF});
        vector<int> pack;
        pack.push_back(-1);
        for (int i = 0; i < n; i++)
        {
            int unpack = -1;
            while (st.back().second < a[i][j])
            {
                unpack = max(unpack, le[st.back().first][j + 1]);
                st.pop_back();
                unpack = max(unpack, pack.back());
                pack.pop_back();
            }
            st.push_back({i, a[i][j]});
            pack.push_back(unpack);
            go_le[0][i][j] = unpack;
        }
    }
    for (int j = 0; j + 1 < m; j++)
    {
        vector<pair<int, int> > st;
        st.push_back({n, INF});
        vector<int> pack;
        pack.push_back(-1);
        for (int i = n - 1; i >= 0; i--)
        {
            int unpack = -1;
            while (st.back().second < a[i][j])
            {
                unpack = max(unpack, le[st.back().first][j + 1]);
                st.pop_back();
                unpack = max(unpack, pack.back());
                pack.pop_back();
            }
            st.push_back({i, a[i][j]});
            pack.push_back(unpack);
            go_le[1][i][j] = unpack;
        }
    }
    vector<vector<vector<pair<int, int> > > > rooted(n, vector<vector<pair<int, int> > >(m));
    for (int i = 1; i + 1 < n; i++)
    {
        for (int j = 1; j + 1 < m; j++)
        {
            {
                int r = ri[i][j - 1] - 1;
                int u0 = up[i + 1][j] + 1;
                int u1 = up[i + 1][r] + 1;
                int d0 = down[i - 1][j] - 1;
                int d1 = down[i - 1][r] - 1;
                if (check_rect(u0, j, i, r, a))
                rooted[u0][j].push_back({i, r});
                if (check_rect(u1, j, i, r, a))
                rooted[u1][j].push_back({i, r});
                if (check_rect(i, j, d0, r, a))
                rooted[i][j].push_back({d0, r});
                if (check_rect(i, j, d1, r, a))
                rooted[i][j].push_back({d1, r});
            }
            {
                int l = le[i][j + 1] + 1;
                int u0 = up[i + 1][l] + 1;
                int u1 = up[i + 1][j] + 1;
                int d0 = down[i - 1][l] - 1;
                int d1 = down[i - 1][j] - 1;
                if (check_rect(u0, l, i, j, a))
                rooted[u0][l].push_back({i, j});
                if (check_rect(u1, l, i, j, a))
                rooted[u1][l].push_back({i, j});
                if (check_rect(i, l, d0, j, a))
                rooted[i][l].push_back({d0, j});
                if (check_rect(i, l, d1, j, a))
                rooted[i][l].push_back({d1, j});
            }
        }
    }
    int ans = 0;
    for (int i = 1; i < n; i++)
    {
        for (int j = 1; j < m; j++)
        {
            sort(all(rooted[i][j]));
            rooted[i][j].resize(unique(all(rooted[i][j])) - rooted[i][j].begin());
            ans += rooted[i][j].size();
        }
    }
    return ans;
}

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int> > a(n, vector<int>(m));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            a[i][j] = rand() % 2;
            cin >> a[i][j];
        }
    }
    cout << count_rectangles(a) << "\n";
}

Compilation message

/tmp/ccuhKHaZ.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/ccYmjr90.o:rect.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status