Submission #540746

#TimeUsernameProblemLanguageResultExecution timeMemory
540746alextodoranRectangles (IOI19_rect)C++17
100 / 100
3137 ms879516 KiB
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int NM_MAX = 2500;

short int L[NM_MAX][NM_MAX];
short int R[NM_MAX][NM_MAX];
short int U[NM_MAX][NM_MAX];
short int D[NM_MAX][NM_MAX];

vector <pair <short int, short int>> DProw[NM_MAX][NM_MAX];
vector <pair <short int, short int>> DPcol[NM_MAX][NM_MAX];

int bins (vector <pair <short int, short int>> &v, int key) {
    if (v.empty() == true) {
        return 0;
    }
    int l = 0, r = (int) v.size() - 1;
    while (l < r) {
        int mid = (l + r) / 2;
        if (v[mid].first < key) {
            l = mid + 1;
        } else {
            r = mid;
        }
    }
    if (v[l].first == key) {
        return v[l].second;
    } else {
        return 0;
    }
}

vector <pair <pair <short int, short int>, pair <short int, short int>>> cand;

ll count_rectangles (vector <vector <int>> h) {
    int n = (int) h.size();
    int m = (int) h[0].size();

    for (int i = 0; i < n; i++) {
        vector <int> st;
        for (int j = 0; j < m; j++) {
            while (st.empty() == false && h[i][st.back()] <= h[i][j]) {
                st.pop_back();
            }
            L[i][j] = (st.empty() == false ? st.back() : -1);
            st.push_back(j);
        }
        st.clear();
        for (int j = m - 1; j >= 0; j--) {
            while (st.empty() == false && h[i][st.back()] <= h[i][j]) {
                st.pop_back();
            }
            R[i][j] = (st.empty() == false ? st.back() : -1);
            st.push_back(j);
        }
    }
    for (int j = 0; j < m; j++) {
        vector <int> st;
        for (int i = 0; i < n; i++) {
            while (st.empty() == false && h[st.back()][j] <= h[i][j]) {
                st.pop_back();
            }
            U[i][j] = (st.empty() == false ? st.back() : -1);
            st.push_back(i);
        }
        st.clear();
        for (int i = n - 1; i >= 0; i--) {
            while (st.empty() == false && h[st.back()][j] <= h[i][j]) {
                st.pop_back();
            }
            D[i][j] = (st.empty() == false ? st.back() : -1);
            st.push_back(i);
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            int i1 = U[i][j], i2 = D[i][j];
            int j1 = L[i][j], j2 = R[i][j];
            if (i1 != -1 && i2 != -1) {
                DProw[i1][j].push_back({i2, 1});
            }
            if (j1 != -1 && j2 != -1) {
                DPcol[i][j1].push_back({j2, 1});
            }
            if (i1 != -1 && j1 != -1 && i2 != -1 && j2 != -1) {
                cand.push_back({{i1, j1}, {i2, j2}});
            }
        }
    }
    sort(cand.begin(), cand.end());
    cand.resize(unique(cand.begin(), cand.end()) - cand.begin());

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            sort(DProw[i][j].begin(), DProw[i][j].end());
            if (j > 0) {
                for (pair <short int, short int> &t : DProw[i][j]) {
                    t.second = bins(DProw[i][j - 1], t.first) + 1;
                }
            }
        }
    }
    for (int j = 0; j < m; j++) {
        for (int i = 0; i < n; i++) {
            sort(DPcol[i][j].begin(), DPcol[i][j].end());
            if (i > 0) {
                for (pair <short int, short int> &t : DPcol[i][j]) {
                    t.second = bins(DPcol[i - 1][j], t.first) + 1;
                }
            }
        }
    }

    int answer = 0;
    for (pair <pair <int, int>, pair <int, int>> c : cand) {
        int i1, j1; tie(i1, j1) = c.first;
        int i2, j2; tie(i2, j2) = c.second;
        if (bins(DProw[i1][j2 - 1], i2) >= j2 - j1 - 1
         && bins(DPcol[i2 - 1][j1], j2) >= i2 - i1 - 1) {
             answer++;
        }
    }

    return answer;
}
#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...