답안 #1009458

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1009458 2024-06-27T14:29:40 Z boris_mihov Rectangles (IOI19_rect) C++17
0 / 100
5000 ms 857356 KB
#include "rect.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <stack>

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

int n, m;
struct SparseMIN
{
    int sparse[MAXLOG][MAXN];
    int getLOG[MAXN];
    int len;

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

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

        for (int i = 1 ; i <= len ; ++i)
        {
            getLOG[i] = getLOG[i - 1];
            if ((1 << getLOG[i] + 1) < i) getLOG[i]++;
        }
    }

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

struct SparseMAX
{
    int sparse[MAXLOG][MAXN];
    int getLOG[MAXN];
    int len;

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

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

        for (int i = 1 ; i <= len ; ++i)
        {
            getLOG[i] = getLOG[i - 1];
            if ((1 << getLOG[i] + 1) < i) getLOG[i]++;
        }
    }

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

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 sparseA[MAXN];
SparseMIN sparseB[MAXN];
SparseMAX sparseC[MAXN];
SparseMAX sparseD[MAXN];

bool isOK(int rowB, int colB, int rowE, int colE)
{
    rowB--; colB--; rowE++; colE++;
    if (sparseA[rowB].findMIN(colB + 1, colE - 1) < rowE) return false;
    if (sparseC[rowE].findMAX(colB + 1, colE - 1) > rowB) return false;
    if (sparseB[colB].findMIN(rowB + 1, rowE - 1) < colE) return false;
    if (sparseD[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 <= n ; ++i)
    {
        sparseA[i].build(b[i], m);
        sparseC[i].build(d[i], m);
    }

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

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

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

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

    int answer = 0;
    for (int rowB = 2 ; rowB < n ; ++rowB)
    {
        for (int colB = 2 ; colB < m ; ++colB)
        {
            for (int rowE = rowB ; rowE < n ; ++rowE)
            {
                for (int colE = colB ; colE < m ; ++colE)
                {
                    answer += isOK(rowB, colB, rowE, colE);
                }
            }
        }
    }

    return answer;
}

Compilation message

rect.cpp: In member function 'void SparseMIN::build(int*, int)':
rect.cpp:33:89: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   33 |                 sparse[lg][i] = std::min(sparse[lg][i - 1], sparse[lg - 1][i + (1 << lg - 1)]);
      |                                                                                      ~~~^~~
rect.cpp:40:33: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   40 |             if ((1 << getLOG[i] + 1) < i) getLOG[i]++;
      |                       ~~~~~~~~~~^~~
rect.cpp: In member function 'void SparseMAX::build(int*, int)':
rect.cpp:69:89: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   69 |                 sparse[lg][i] = std::max(sparse[lg][i - 1], sparse[lg - 1][i + (1 << lg - 1)]);
      |                                                                                      ~~~^~~
rect.cpp:76:33: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   76 |             if ((1 << getLOG[i] + 1) < i) getLOG[i]++;
      |                       ~~~~~~~~~~^~~
rect.cpp: In function 'llong count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:133:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  133 |         while (st.size()) st.pop(); st.push(m + 1);
      |         ^~~~~
rect.cpp:133:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  133 |         while (st.size()) st.pop(); st.push(m + 1);
      |                                     ^~
rect.cpp:145:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  145 |         while (st.size()) st.pop(); st.push(0);
      |         ^~~~~
rect.cpp:145:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  145 |         while (st.size()) st.pop(); st.push(0);
      |                                     ^~
rect.cpp:160:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  160 |         while (st.size()) st.pop(); st.push(n + 1);
      |         ^~~~~
rect.cpp:160:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  160 |         while (st.size()) st.pop(); st.push(n + 1);
      |                                     ^~
rect.cpp:172:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  172 |         while (st.size()) st.pop(); st.push(0);
      |         ^~~~~
rect.cpp:172:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  172 |         while (st.size()) st.pop(); st.push(0);
      |                                     ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 20824 KB Output is correct
2 Incorrect 5 ms 43352 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 20824 KB Output is correct
2 Incorrect 5 ms 43352 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 20824 KB Output is correct
2 Incorrect 5 ms 43352 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 20824 KB Output is correct
2 Incorrect 5 ms 43352 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 39 ms 123152 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 22876 KB Output is correct
2 Execution timed out 5105 ms 857356 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 20824 KB Output is correct
2 Incorrect 5 ms 43352 KB Output isn't correct
3 Halted 0 ms 0 KB -