Submission #1009512

# Submission time Handle Problem Language Result Execution time Memory
1009512 2024-06-27T15:17:22 Z boris_mihov Rectangles (IOI19_rect) C++17
0 / 100
187 ms 494112 KB
#include "rect.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <stack>
#include <bitset>

typedef long long llong;
const int MAXLOG = 12;
const int MAXN = 2500 + 10;
const int INF  = 1e8;

int n, m;
int getLOG[MAXN];
struct SparseMIN
{
    std::bitset <MAXLOG> sparse[MAXN];
    int len;

    void build(int a[], int length)
    {
        len = length;
        for (int i = 1 ; i <= len ; ++i)
        {
            sparse[i][0] = a[i]; 
        }

        for (int lg = 1 ; (1 << lg) <= len ; ++lg)
        {
            for (int i = 1 ; i + (1 << lg) - 1 <= len ; ++i)
            {
                sparse[i][lg] = std::min(sparse[i][lg - 1], sparse[i + (1 << lg - 1)][lg - 1]);
            }
        }
    }

    int findMIN(int l, int r)
    {
        int lg = getLOG[r - l + 1];
        return std::min(sparse[l][lg], sparse[r - (1 << lg) + 1][lg]);
    }
};

struct SparseMAX
{
    std::bitset <MAXLOG> sparse[MAXN];
    int len;

    void build(int a[], int length)
    {
        len = length;
        for (int i = 1 ; i <= len ; ++i)
        {
            sparse[i][0] = a[i]; 
        }

        for (int lg = 1 ; (1 << lg) <= len ; ++lg)
        {
            for (int i = 1 ; i + (1 << lg) - 1 <= len ; ++i)
            {
                sparse[i][lg] = std::max(sparse[i][lg - 1], sparse[i + (1 << lg - 1)][lg - 1]);
            }
        }
    }

    int findMAX(int l, int r)
    {
        int lg = getLOG[r - l + 1];
        return std::max(sparse[l][lg], sparse[r - (1 << lg) + 1][lg]);
    }
};

int a[MAXN][MAXN]; // >
int b[MAXN][MAXN]; // v
int c[MAXN][MAXN]; // <
int d[MAXN][MAXN]; // ^
int t[MAXN][MAXN];
int cpy[MAXN];

SparseMIN sparseRD[MAXN];
SparseMAX sparseRU[MAXN];
SparseMIN sparseCL[MAXN];
SparseMAX sparseCR[MAXN];
std::vector <int> possibleColE[MAXN][MAXN];
std::vector <int> possibleRowE[MAXN][MAXN];

bool isOK(int rowB, int colB, int rowE, int colE)
{
    rowB--; colB--; rowE++; colE++;
    if (sparseRD[rowB].findMIN(colB + 1, colE - 1) < rowE) return false;
    if (sparseRU[rowE].findMAX(colB + 1, colE - 1) > rowB) return false;
    if (sparseCL[colB].findMIN(rowB + 1, rowE - 1) < colE) return false;
    if (sparseCR[colE].findMAX(rowB + 1, rowE - 1) > colB) return false;
    return true;
}

llong count_rectangles(std::vector <std::vector<int>> _a)
{
    n = _a.size(); m = _a[0].size();
    for (int i = 0 ; i < n ; ++i)
    {
        for (int j = 0 ; j < m ; ++j)
        {
            t[i + 1][j + 1] = _a[i][j];
        }
    }

    for (int i = 0 ; i <= n + 1 ; ++i)
    {
        t[i][0] = t[i][m + 1] = INF;
    }

    for (int i = 0 ; i <= m + 1 ; ++i)
    {
        t[0][i] = t[n + 1][i] = INF;
    }

    std::stack <int> st;
    for (int i = 1 ; i <= n ; ++i)
    {
        while (st.size()) st.pop(); st.push(m + 1);
        for (int j = m ; j >= 1 ; --j)
        {
            while (t[i][j] > t[i][st.top()])
            {
                st.pop();
            }

            a[i][j] = st.top();
            st.push(j);
        }

        while (st.size()) st.pop(); st.push(0);
        for (int j = 1 ; j <= m ; ++j)
        {
            while (t[i][j] > t[i][st.top()])
            {
                st.pop();
            }

            c[i][j] = st.top();
            st.push(j);
        }
    }

    for (int i = 1 ; i <= m ; ++i)
    {
        while (st.size()) st.pop(); st.push(n + 1);
        for (int j = n ; j >= 1 ; --j)
        {
            while (t[j][i] > t[st.top()][i])
            {
                st.pop();
            }

            b[j][i] = st.top();
            st.push(j);
        }

        while (st.size()) st.pop(); st.push(0);
        for (int j = 1 ; j <= n ; ++j)
        {
            while (t[j][i] > t[st.top()][i])
            {
                st.pop();
            }

            d[j][i] = st.top();
            st.push(j);
        }
    }

    for (int i = 1 ; i <= std::max(n, m) ; ++i)
    {
        getLOG[i] = getLOG[i - 1];
        if ((1 << getLOG[i] + 1) < i) getLOG[i]++;
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        sparseRD[i].build(b[i], m);
        sparseRU[i].build(d[i], m);
    }

    for (int i = 1 ; i <= m ; ++i)
    {
        for (int j = 1 ; j <= n ; ++j)
        {
            cpy[j] = a[j][i];
        }

        sparseCL[i].build(cpy, n);

        for (int j = 1 ; j <= n ; ++j)
        {
            cpy[j] = c[j][i];
        }

        sparseCR[i].build(cpy, n);
    }

    for (int i = 2 ; i <= n ; ++i)
    {
        for (int j = 2 ; j <= m ; ++j)
        {
            if (d[i][j] < i - 1) possibleRowE[d[i][j] + 1][j].push_back(i - 1);
            if (c[i][j] < j - 1) possibleColE[i][c[i][j] + 1].push_back(j - 1);
        }
    }

    for (int i = 2 ; i <= n ; ++i)
    {
        for (int j = 2 ; j <= m ; ++j)
        {
            if (i < b[i - 1][j] && b[i - 1][j] != n + 1 && (possibleRowE[i][j].empty() || possibleRowE[i][j].back() != b[i - 1][j] - 1)) possibleRowE[i][j].push_back(b[i - 1][j] - 1);
            if (j < a[i][j - 1] && a[i][j - 1] != m + 1 && (possibleColE[i][j].empty() || possibleColE[i][j].back() != a[i][j - 1] - 1)) possibleColE[i][j].push_back(a[i][j - 1] - 1);
        }
    }

    int answer = 0;
    for (int rowB = 2 ; rowB < n ; ++rowB)
    {
        for (int colB = 2 ; colB < m ; ++colB)
        {
            for (const int &rowE : possibleRowE[rowB][colB])
            {
                for (const int &colE : possibleColE[rowB][colB])
                {
                    answer += isOK(rowB, colB, rowE, colE);
                }
            }
        }
    }

    return answer;
}

Compilation message

rect.cpp: In member function 'void SparseMIN::build(int*, int)':
rect.cpp:34:81: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   34 |                 sparse[i][lg] = std::min(sparse[i][lg - 1], sparse[i + (1 << lg - 1)][lg - 1]);
      |                                                                              ~~~^~~
rect.cpp: In member function 'void SparseMAX::build(int*, int)':
rect.cpp:63:81: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   63 |                 sparse[i][lg] = std::max(sparse[i][lg - 1], sparse[i + (1 << lg - 1)][lg - 1]);
      |                                                                              ~~~^~~
rect.cpp: In function 'llong count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:123:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  123 |         while (st.size()) st.pop(); st.push(m + 1);
      |         ^~~~~
rect.cpp:123:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  123 |         while (st.size()) st.pop(); st.push(m + 1);
      |                                     ^~
rect.cpp:135:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  135 |         while (st.size()) st.pop(); st.push(0);
      |         ^~~~~
rect.cpp:135:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  135 |         while (st.size()) st.pop(); st.push(0);
      |                                     ^~
rect.cpp:150:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  150 |         while (st.size()) st.pop(); st.push(n + 1);
      |         ^~~~~
rect.cpp:150:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  150 |         while (st.size()) st.pop(); st.push(n + 1);
      |                                     ^~
rect.cpp:162:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  162 |         while (st.size()) st.pop(); st.push(0);
      |         ^~~~~
rect.cpp:162:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  162 |         while (st.size()) st.pop(); st.push(0);
      |                                     ^~
rect.cpp:178:29: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  178 |         if ((1 << getLOG[i] + 1) < i) getLOG[i]++;
      |                   ~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Incorrect 181 ms 493648 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 181 ms 493648 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 181 ms 493648 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 181 ms 493648 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 187 ms 494112 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 180 ms 493648 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 181 ms 493648 KB Output isn't correct
2 Halted 0 ms 0 KB -