Submission #143524

#TimeUsernameProblemLanguageResultExecution timeMemory
143524imeimi2000Rectangles (IOI19_rect)C++17
100 / 100
4256 ms373552 KiB
#include "rect.h"
#include <algorithm>
#include <vector>
#include <set>
 
using namespace std;
typedef long long llong;
 
const short inf = 1e4;
int n, m;
short L[2501][2501];
short R[2501][2501];
short U[2501][2501];
short D[2501][2501];
short Lm[2501][2501];
short Rm[2501][2501];
short Um[2501][2501];
short Dm[2501][2501];
 
struct idxtree {
    const static int sz = 1 << 12;
    short seg[sz << 1];
    void init(vector<short> v) {
        for (int i = 1; i <= v.size(); ++i) seg[i + sz] = v[i - 1];
        for (int i = sz; i--; ) seg[i] = min(seg[i << 1], seg[i << 1 | 1]);
    }
    short query(int x, int y) {
        x += sz;
        y += sz;
        short ret = inf;
        while (x <= y) {
            if ((x & 1) == 1) ret = min(ret, seg[x++]);
            if ((y & 1) == 0) ret = min(ret, seg[y--]);
            x >>= 1;
            y >>= 1;
        }
        return ret;
    }
} Li[2501], Ri[2501], Ui[2501], Di[2501];
 
llong count_rectangles(vector<vector<int>> a) {
    n = a.size();
    m = a[0].size();
    //step 1
    for (int i = 1; i <= n; ++i) {
        vector<short> st;
        st.push_back(0);
        for (int j = 1; j <= m; ++j) {
            while (st.size() > 1 && a[i - 1][st.back() - 1] <= a[i - 1][j - 1])
                st.pop_back();
            L[i][j] = st.back();
            st.push_back(j);
        }
    }
    for (int i = 1; i <= n; ++i) {
        vector<short> st;
        st.push_back(m + 1);
        for (int j = m; j > 0; --j) {
            while (st.size() > 1 && a[i - 1][st.back() - 1] <= a[i - 1][j - 1])
                st.pop_back();
            R[i][j] = st.back();
            st.push_back(j);
        }
    }
    for (int i = 1; i <= m; ++i) {
        vector<short> st;
        st.push_back(0);
        for (int j = 1; j <= n; ++j) {
            while (st.size() > 1 && a[st.back() - 1][i - 1] <= a[j - 1][i - 1])
                st.pop_back();
            U[j][i] = st.back();
            st.push_back(j);
        }
    }
    for (int i = 1; i <= m; ++i) {
        vector<short> st;
        st.push_back(n + 1);
        for (int j = n; j > 0; --j) {
            while (st.size() > 1 && a[st.back() - 1][i - 1] <= a[j - 1][i - 1])
                st.pop_back();
            D[j][i] = st.back();
            st.push_back(j);
        }
    }
    //step 2
    for (int i = 1; i <= n; ++i) {
        vector<short> st;
        st.push_back(0);
        for (int j = 1; j <= m; ++j) {
            while (st.size() > 1 && a[i - 1][st.back() - 1] < a[i - 1][j - 1])
                st.pop_back();
            Lm[i][j] = st.back();
            st.push_back(j);
        }
    }
    for (int i = 1; i <= n; ++i) {
        vector<short> st;
        st.push_back(m + 1);
        for (int j = m; j > 0; --j) {
            while (st.size() > 1 && a[i - 1][st.back() - 1] < a[i - 1][j - 1])
                st.pop_back();
            Rm[i][j] = st.back();
            st.push_back(j);
        }
    }
    for (int i = 1; i <= m; ++i) {
        vector<short> st;
        st.push_back(0);
        for (int j = 1; j <= n; ++j) {
            while (st.size() > 1 && a[st.back() - 1][i - 1] < a[j - 1][i - 1])
                st.pop_back();
            Um[j][i] = st.back();
            st.push_back(j);
        }
    }
    for (int i = 1; i <= m; ++i) {
        vector<short> st;
        st.push_back(n + 1);
        for (int j = n; j > 0; --j) {
            while (st.size() > 1 && a[st.back() - 1][i - 1] < a[j - 1][i - 1])
                st.pop_back();
            Dm[j][i] = st.back();
            st.push_back(j);
        }
    }
    //step 3
    for (int i = 1; i <= m; ++i) {
        vector<short> Ls, Rs;
        for (int j = 1; j <= n; ++j) {
            Ls.push_back(-Lm[j][i]);
            Rs.push_back(Rm[j][i]);
        }
        Li[i].init(Ls);
        Ri[i].init(Rs);
    }
    for (int i = 1; i <= n; ++i) {
        vector<short> Us, Ds;
        for (int j = 1; j <= m; ++j) {
            Us.push_back(-Um[i][j]);
            Ds.push_back(Dm[i][j]);
        }
        Ui[i].init(Us);
        Di[i].init(Ds);
    }
    vector<llong> ans;
    for (int i = 2; i < n; ++i) {
        for (int j = 2; j < m; ++j) {
            int Lx = L[i][j] + 1;
            if (Lx == 1) continue;
            int Rx = R[i][j] - 1;
            if (Rx == m) continue;
            int Ux = U[i][j] + 1;
            if (Ux == 1) continue;
            int Dx = D[i][j] - 1;
            if (Dx == n) continue;
            if (Ri[Lx - 1].query(Ux, Dx) - 1 < Rx) continue;
            if (-Li[Rx + 1].query(Ux, Dx) + 1 > Lx) continue;
            if (Di[Ux - 1].query(Lx, Rx) - 1 < Dx) continue;
            if (-Ui[Dx + 1].query(Lx, Rx) + 1 > Ux) continue;
            llong idx = ((llong)(Lx << 12 | Rx)) << 24 | (Ux << 12 | Dx);
            ans.push_back(idx);
        }
    }
    sort(ans.begin(), ans.end());
    ans.erase(unique(ans.begin(), ans.end()), ans.end());
    return ans.size();
}

Compilation message (stderr)

rect.cpp: In member function 'void idxtree::init(std::vector<short int>)':
rect.cpp:24:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 1; i <= v.size(); ++i) seg[i + sz] = v[i - 1];
                         ~~^~~~~~~~~~~
#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...