Submission #1009591

#TimeUsernameProblemLanguageResultExecution timeMemory
1009591boris_mihovRectangles (IOI19_rect)C++17
100 / 100
2183 ms1048576 KiB
#include "rect.h" #include <algorithm> #include <iostream> #include <numeric> #include <cassert> #include <vector> #include <stack> typedef long long llong; const short MAXLOG = 4; 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 * 3)) <= len ; ++lg) { for (short i = 1 ; i + (1 << (lg * 3)) - 1 <= len ; ++i) { sparse[i][lg] = sparse[i][lg - 1]; if (sparse[i][lg] > sparse[i + (1 << ((lg - 1) * 3))][lg - 1]) sparse[i][lg] = sparse[i + (1 << ((lg - 1) * 3))][lg - 1]; if (sparse[i][lg] > sparse[i + 2 * (1 << ((lg - 1) * 3))][lg - 1]) sparse[i][lg] = sparse[i + 2 * (1 << ((lg - 1) * 3))][lg - 1]; if (sparse[i][lg] > sparse[i + 3 * (1 << ((lg - 1) * 3))][lg - 1]) sparse[i][lg] = sparse[i + 3 * (1 << ((lg - 1) * 3))][lg - 1]; if (sparse[i][lg] > sparse[i + 4 * (1 << ((lg - 1) * 3))][lg - 1]) sparse[i][lg] = sparse[i + 4 * (1 << ((lg - 1) * 3))][lg - 1]; if (sparse[i][lg] > sparse[i + 5 * (1 << ((lg - 1) * 3))][lg - 1]) sparse[i][lg] = sparse[i + 5 * (1 << ((lg - 1) * 3))][lg - 1]; if (sparse[i][lg] > sparse[i + 6 * (1 << ((lg - 1) * 3))][lg - 1]) sparse[i][lg] = sparse[i + 6 * (1 << ((lg - 1) * 3))][lg - 1]; if (sparse[i][lg] > sparse[i + 7 * (1 << ((lg - 1) * 3))][lg - 1]) sparse[i][lg] = sparse[i + 7 * (1 << ((lg - 1) * 3))][lg - 1]; } } } inline 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 * 3)) + 1][lg] < val) return false; if (r - 2 * (1 << (lg * 3)) + 1 >= l && sparse[r - 2 * (1 << (lg * 3)) + 1][lg] < val) return false; if (r - 3 * (1 << (lg * 3)) + 1 >= l && sparse[r - 3 * (1 << (lg * 3)) + 1][lg] < val) return false; if (r - 4 * (1 << (lg * 3)) + 1 >= l && sparse[r - 4 * (1 << (lg * 3)) + 1][lg] < val) return false; if (r - 5 * (1 << (lg * 3)) + 1 >= l && sparse[r - 5 * (1 << (lg * 3)) + 1][lg] < val) return false; if (r - 6 * (1 << (lg * 3)) + 1 >= l && sparse[r - 6 * (1 << (lg * 3)) + 1][lg] < val) return false; if (r - 7 * (1 << (lg * 3)) + 1 >= l && sparse[r - 7 * (1 << (lg * 3)) + 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 * 3)) <= len ; ++lg) { for (short i = 1 ; i + (1 << (lg * 3)) - 1 <= len ; ++i) { sparse[i][lg] = sparse[i][lg - 1]; if (sparse[i][lg] < sparse[i + (1 << ((lg - 1) * 3))][lg - 1]) sparse[i][lg] = sparse[i + (1 << ((lg - 1) * 3))][lg - 1]; if (sparse[i][lg] < sparse[i + 2 * (1 << ((lg - 1) * 3))][lg - 1]) sparse[i][lg] = sparse[i + 2 * (1 << ((lg - 1) * 3))][lg - 1]; if (sparse[i][lg] < sparse[i + 3 * (1 << ((lg - 1) * 3))][lg - 1]) sparse[i][lg] = sparse[i + 3 * (1 << ((lg - 1) * 3))][lg - 1]; if (sparse[i][lg] < sparse[i + 4 * (1 << ((lg - 1) * 3))][lg - 1]) sparse[i][lg] = sparse[i + 4 * (1 << ((lg - 1) * 3))][lg - 1]; if (sparse[i][lg] < sparse[i + 5 * (1 << ((lg - 1) * 3))][lg - 1]) sparse[i][lg] = sparse[i + 5 * (1 << ((lg - 1) * 3))][lg - 1]; if (sparse[i][lg] < sparse[i + 6 * (1 << ((lg - 1) * 3))][lg - 1]) sparse[i][lg] = sparse[i + 6 * (1 << ((lg - 1) * 3))][lg - 1]; if (sparse[i][lg] < sparse[i + 7 * (1 << ((lg - 1) * 3))][lg - 1]) sparse[i][lg] = sparse[i + 7 * (1 << ((lg - 1) * 3))][lg - 1]; } } } inline short 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 * 3)) + 1][lg] > val) return false; if (r - 2 * (1 << (lg * 3)) + 1 >= l && sparse[r - 2 * (1 << (lg * 3)) + 1][lg] > val) return false; if (r - 3 * (1 << (lg * 3)) + 1 >= l && sparse[r - 3 * (1 << (lg * 3)) + 1][lg] > val) return false; if (r - 4 * (1 << (lg * 3)) + 1 >= l && sparse[r - 4 * (1 << (lg * 3)) + 1][lg] > val) return false; if (r - 5 * (1 << (lg * 3)) + 1 >= l && sparse[r - 5 * (1 << (lg * 3)) + 1][lg] > val) return false; if (r - 6 * (1 << (lg * 3)) + 1 >= l && sparse[r - 6 * (1 << (lg * 3)) + 1][lg] > val) return false; if (r - 7 * (1 << (lg * 3)) + 1 >= l && sparse[r - 7 * (1 << (lg * 3)) + 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]; int answer = 0; bool result; void isRowOK(short &rowB, short &colB, short &rowE, short &colE) { colB++; colE--; if (result && !sparseRD[rowB].findMIN(colB, colE, rowE)) result = false; if (result && !sparseRU[rowE].findMAX(colB, colE, rowB)) result = false; colB--; colE++; rowE--; rowB++; if (result && !sparseCL[colB].findMIN(rowB, rowE, colE)) result = false; } void isColOK(short &rowB, short &colB, short &rowE, short &colE) { if (result && !sparseCR[colE].findMAX(rowB, rowE, colB)) result = false; rowE++; rowB--; } 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) { // max = std::max(max, a[i][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) * 3)) < 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 - 1][j - 1].empty() || possibleRowE[i - 1][j - 1].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 - 1][j - 1].empty() || possibleColE[i - 1][j - 1].back() != a[i][j - 1])) possibleColE[i - 1][j - 1].push_back(a[i][j - 1]); } } 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]) { result = true; isRowOK(rowB, colB, rowE, colE); if (!result) { isColOK(rowB, colB, rowE, colE); break; } isColOK(rowB, colB, rowE, colE); answer += result; } } } } return answer; }

Compilation message (stderr)

rect.cpp: In function 'llong count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:161:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  161 |         while (st.size()) st.pop(); st.push(m + 1);
      |         ^~~~~
rect.cpp:161:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  161 |         while (st.size()) st.pop(); st.push(m + 1);
      |                                     ^~
rect.cpp:173:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  173 |         while (st.size()) st.pop(); st.push(0);
      |         ^~~~~
rect.cpp:173:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  173 |         while (st.size()) st.pop(); st.push(0);
      |                                     ^~
rect.cpp:188:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  188 |         while (st.size()) st.pop(); st.push(n + 1);
      |         ^~~~~
rect.cpp:188:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  188 |         while (st.size()) st.pop(); st.push(n + 1);
      |                                     ^~
rect.cpp:200:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  200 |         while (st.size()) st.pop(); st.push(0);
      |         ^~~~~
rect.cpp:200:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  200 |         while (st.size()) st.pop(); st.push(0);
      |                                     ^~
#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...