Submission #540731

#TimeUsernameProblemLanguageResultExecution timeMemory
540731alextodoranRectangles (IOI19_rect)C++17
59 / 100
1358 ms1048576 KiB
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;


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

    vector <vector <int>> L, R, U, D;
    L = R = U = D = vector <vector <int>> (n, vector <int> (m));
    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);
        }
    }

    vector <pair <pair <int, int>, pair <int, int>>> cand;
    vector <vector <vector <int>>> Ds, Rs;
    Ds = Rs = vector <vector <vector <int>>> (n, vector <vector <int>> (m));
    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) {
                Ds[i1][j].push_back(i2);
            }
            if (j1 != -1 && j2 != -1) {
                Rs[i][j1].push_back(j2);
            }
            if (i1 != -1 && j1 != -1 && i2 != -1 && j2 != -1) {
                cand.push_back({{i1, j1}, {i2, j2}});
            }
        }
    }

    vector <vector <unordered_map <int, int>>> DProw (n, vector <unordered_map <int, int>> (m));
    vector <vector <unordered_map <int, int>>> DPcol (n, vector <unordered_map <int, int>> (m));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            for (int i2 : Ds[i][j]) {
                if (j > 0 && DProw[i][j - 1].count(i2) > 0) {
                    DProw[i][j][i2] = DProw[i][j - 1][i2] + 1;
                } else {
                    DProw[i][j][i2] = 1;
                }
            }
            for (int j2 : Rs[i][j]) {
                if (i > 0 && DPcol[i - 1][j].count(j2) > 0) {
                    DPcol[i][j][j2] = DPcol[i - 1][j][j2] + 1;
                } else {
                    DPcol[i][j][j2] = 1;
                }
            }
        }
    }

    set <pair <pair <int, int>, pair <int, int>>> rects;
    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;
        assert(i1 + 1 <= i2 - 1 && j1 + 1 <= j2 - 1);
        if (DProw[i1][j2 - 1][i2] >= j2 - j1 - 1
         && DPcol[i2 - 1][j1][j2] >= i2 - i1 - 1) {
            rects.insert({{i1 + 1, j1 + 1}, {i2 - 1, j2 - 1}});
        }
    }

    return (int) rects.size();
}
#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...