Submission #1009540

#TimeUsernameProblemLanguageResultExecution timeMemory
1009540boris_mihovRectangles (IOI19_rect)C++17
Compilation error
0 ms0 KiB
#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; short getLOG[MAXN]; struct SparseMIN { short sparse[MAXN][MAXLOG]; short len; void build(short a[], short length) { len = length; for (short i = 1 ; i <= len ; ++i) { sparse[i][0] = a[i]; } for (short lg = 1 ; (1 << lg) <= len ; ++lg) { for (short i = 1 ; i + (1 << lg) - 1 <= len ; ++i) { sparse[i][lg] = std::min(sparse[i][lg - 1], sparse[i + (1 << lg - 1)][lg - 1]); } } } short findMIN(short &l, short &r, short &val) { short lg = getLOG[r - l + 1]; if (sparse[l][lg] < val) return false; if (sparse[r - (1 << lg) + 1][lg] < val) return false; return true; } }; struct SparseMAX { short sparse[MAXN][MAXLOG]; short len; void build(short a[], short length) { len = length; for (short i = 1 ; i <= len ; ++i) { sparse[i][0] = a[i]; } for (short lg = 1 ; (1 << lg) <= len ; ++lg) { for (short i = 1 ; i + (1 << lg) - 1 <= len ; ++i) { sparse[i][lg] = std::max(sparse[i][lg - 1], sparse[i + (1 << lg - 1)][lg - 1]); } } } bool findMAX(short &l, short &r, short &val) { short lg = getLOG[r - l + 1]; if (sparse[l][lg] > val) return false; if (sparse[r - (1 << lg) + 1][lg] > val) return false; return true; } }; 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) { 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 (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 <short> 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] + 1][j].push_back(i - 1); if (c[i][j] < j - 1) possibleColE[i][c[i][j] + 1].push_back(j - 1); } } 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] - 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 (short rowB = 2 ; rowB < n ; ++rowB) { for (short colB = 2 ; colB < m ; ++colB) { for (const short &rowE : possibleRowE[rowB][colB]) { for (const short &colE : possibleColE[rowB][colB]) { answer += isOK(rowB, colB, rowE, colE); } } } } return answer; }

Compilation message (stderr)

rect.cpp: In member function 'void SparseMIN::build(short int*, short 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*, short int)':
rect.cpp:64:81: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   64 |                 sparse[i][lg] = std::max(sparse[i][lg - 1], sparse[i + (1 << lg - 1)][lg - 1]);
      |                                                                              ~~~^~~
rect.cpp: In function 'bool isOK(short int, short int, short int, short int)':
rect.cpp:95:38: error: cannot bind non-const lvalue reference of type 'short int&' to an rvalue of type 'short int'
   95 |     if (!sparseRD[rowB].findMIN(colB + 1, colE - 1, rowE)) return false;
      |                                 ~~~~~^~~
rect.cpp:38:26: note:   initializing argument 1 of 'short int SparseMIN::findMIN(short int&, short int&, short int&)'
   38 |     short findMIN(short &l, short &r, short &val)
      |                   ~~~~~~~^
rect.cpp:96:38: error: cannot bind non-const lvalue reference of type 'short int&' to an rvalue of type 'short int'
   96 |     if (!sparseRU[rowE].findMAX(colB + 1, colE - 1, rowB)) return false;
      |                                 ~~~~~^~~
rect.cpp:69:25: note:   initializing argument 1 of 'bool SparseMAX::findMAX(short int&, short int&, short int&)'
   69 |     bool findMAX(short &l, short &r, short &val)
      |                  ~~~~~~~^
rect.cpp:97:38: error: cannot bind non-const lvalue reference of type 'short int&' to an rvalue of type 'short int'
   97 |     if (!sparseCL[colB].findMIN(rowB + 1, rowE - 1, colE)) return false;
      |                                 ~~~~~^~~
rect.cpp:38:26: note:   initializing argument 1 of 'short int SparseMIN::findMIN(short int&, short int&, short int&)'
   38 |     short findMIN(short &l, short &r, short &val)
      |                   ~~~~~~~^
rect.cpp:98:38: error: cannot bind non-const lvalue reference of type 'short int&' to an rvalue of type 'short int'
   98 |     if (!sparseCR[colE].findMAX(rowB + 1, rowE - 1, colB)) return false;
      |                                 ~~~~~^~~
rect.cpp:69:25: note:   initializing argument 1 of 'bool SparseMAX::findMAX(short int&, short int&, short int&)'
   69 |     bool findMAX(short &l, short &r, short &val)
      |                  ~~~~~~~^
rect.cpp: In function 'llong count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:126:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  126 |         while (st.size()) st.pop(); st.push(m + 1);
      |         ^~~~~
rect.cpp:126:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  126 |         while (st.size()) st.pop(); st.push(m + 1);
      |                                     ^~
rect.cpp:138:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  138 |         while (st.size()) st.pop(); st.push(0);
      |         ^~~~~
rect.cpp:138:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  138 |         while (st.size()) st.pop(); st.push(0);
      |                                     ^~
rect.cpp:153:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  153 |         while (st.size()) st.pop(); st.push(n + 1);
      |         ^~~~~
rect.cpp:153:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  153 |         while (st.size()) st.pop(); st.push(n + 1);
      |                                     ^~
rect.cpp:165:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  165 |         while (st.size()) st.pop(); st.push(0);
      |         ^~~~~
rect.cpp:165:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  165 |         while (st.size()) st.pop(); st.push(0);
      |                                     ^~
rect.cpp:181:29: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  181 |         if ((1 << getLOG[i] + 1) < i) getLOG[i]++;
      |                   ~~~~~~~~~~^~~