Submission #1009525

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

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

short n, m;
char getLOG[MAXN];
struct SparseMIN
{
    short sparse[MAXN][MAXLOG];
    int len;
 
    void build(short 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
{
    short sparse[MAXN][MAXLOG];
    int len;
 
    void build(short 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]);
    }
};

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

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

bool isOK(short &rowB, short &colB, short &rowE, short &colE)
{
    colB++; colE--;
    bool result = true;
    if (sparseRD[rowB].findMIN(colB, colE) < rowE) result = false;
    if (sparseRU[rowE].findMAX(colB, colE) > rowB) result = false;
    colB--; colE++;
    rowB++; rowE--;

    if (sparseCL[colB].findMIN(rowB, rowE) < colE) result = false;
    if (sparseCR[colE].findMAX(rowB, rowE) > colB) result = false;
    rowB--; rowE++;
    return result;
}

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

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

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

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

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

    for (short i = 1 ; i <= m ; ++i)
    {
        while (st.size()) st.pop(); st.push(n + 1);
        for (short 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 (short j = 1 ; j <= n ; ++j)
        {
            while (t[j][i] > t[st.top()][i])
            {
                st.pop();
            }

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

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

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

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

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

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

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

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

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

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

    return answer;
}

Compilation message

rect.cpp: In member function 'void SparseMIN::build(short int*, int)':
rect.cpp:33:81: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   33 |                 sparse[i][lg] = std::min(sparse[i][lg - 1], sparse[i + (1 << lg - 1)][lg - 1]);
      |                                                                              ~~~^~~
rect.cpp: In member function 'void SparseMAX::build(short int*, int)':
rect.cpp:62:81: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   62 |                 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:127:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  127 |         while (st.size()) st.pop(); st.push(m + 1);
      |         ^~~~~
rect.cpp:127:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  127 |         while (st.size()) st.pop(); st.push(m + 1);
      |                                     ^~
rect.cpp:139:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  139 |         while (st.size()) st.pop(); st.push(0);
      |         ^~~~~
rect.cpp:139:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  139 |         while (st.size()) st.pop(); st.push(0);
      |                                     ^~
rect.cpp:154:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  154 |         while (st.size()) st.pop(); st.push(n + 1);
      |         ^~~~~
rect.cpp:154:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  154 |         while (st.size()) st.pop(); st.push(n + 1);
      |                                     ^~
rect.cpp:166:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  166 |         while (st.size()) st.pop(); st.push(0);
      |         ^~~~~
rect.cpp:166:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  166 |         while (st.size()) st.pop(); st.push(0);
      |                                     ^~
rect.cpp:182:29: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  182 |         if ((1 << getLOG[i] + 1) < i) getLOG[i]++;
      |                   ~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 103 ms 296532 KB Output is correct
2 Correct 107 ms 297552 KB Output is correct
3 Correct 106 ms 297556 KB Output is correct
4 Correct 106 ms 297600 KB Output is correct
5 Correct 100 ms 297524 KB Output is correct
6 Incorrect 95 ms 297464 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 103 ms 296532 KB Output is correct
2 Correct 107 ms 297552 KB Output is correct
3 Correct 106 ms 297556 KB Output is correct
4 Correct 106 ms 297600 KB Output is correct
5 Correct 100 ms 297524 KB Output is correct
6 Incorrect 95 ms 297464 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 103 ms 296532 KB Output is correct
2 Correct 107 ms 297552 KB Output is correct
3 Correct 106 ms 297556 KB Output is correct
4 Correct 106 ms 297600 KB Output is correct
5 Correct 100 ms 297524 KB Output is correct
6 Incorrect 95 ms 297464 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 103 ms 296532 KB Output is correct
2 Correct 107 ms 297552 KB Output is correct
3 Correct 106 ms 297556 KB Output is correct
4 Correct 106 ms 297600 KB Output is correct
5 Correct 100 ms 297524 KB Output is correct
6 Incorrect 95 ms 297464 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 106 ms 318036 KB Output is correct
2 Correct 101 ms 314708 KB Output is correct
3 Correct 158 ms 317784 KB Output is correct
4 Correct 102 ms 296244 KB Output is correct
5 Incorrect 112 ms 317780 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 96 ms 296532 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 103 ms 296532 KB Output is correct
2 Correct 107 ms 297552 KB Output is correct
3 Correct 106 ms 297556 KB Output is correct
4 Correct 106 ms 297600 KB Output is correct
5 Correct 100 ms 297524 KB Output is correct
6 Incorrect 95 ms 297464 KB Output isn't correct
7 Halted 0 ms 0 KB -