#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 lg = getLOG[r - l + 1];
return std::min(sparse[l][lg], sparse[r - (1 << lg) + 1][lg]);
}
};
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]);
}
}
}
short findMAX(short l, short r)
{
short 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)
{
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
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: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:122:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
122 | while (st.size()) st.pop(); st.push(m + 1);
| ^~~~~
rect.cpp:122:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
122 | while (st.size()) st.pop(); st.push(m + 1);
| ^~
rect.cpp:134:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
134 | while (st.size()) st.pop(); st.push(0);
| ^~~~~
rect.cpp:134:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
134 | while (st.size()) st.pop(); st.push(0);
| ^~
rect.cpp:149:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
149 | while (st.size()) st.pop(); st.push(n + 1);
| ^~~~~
rect.cpp:149:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
149 | while (st.size()) st.pop(); st.push(n + 1);
| ^~
rect.cpp:161:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
161 | while (st.size()) st.pop(); st.push(0);
| ^~~~~
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(0);
| ^~
rect.cpp:177:29: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
177 | if ((1 << getLOG[i] + 1) < i) getLOG[i]++;
| ~~~~~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
296528 KB |
Output is correct |
2 |
Correct |
105 ms |
297556 KB |
Output is correct |
3 |
Correct |
107 ms |
297552 KB |
Output is correct |
4 |
Correct |
121 ms |
297452 KB |
Output is correct |
5 |
Correct |
110 ms |
297488 KB |
Output is correct |
6 |
Correct |
119 ms |
297556 KB |
Output is correct |
7 |
Correct |
110 ms |
297240 KB |
Output is correct |
8 |
Correct |
106 ms |
296788 KB |
Output is correct |
9 |
Correct |
115 ms |
297500 KB |
Output is correct |
10 |
Correct |
125 ms |
297456 KB |
Output is correct |
11 |
Correct |
113 ms |
297416 KB |
Output is correct |
12 |
Correct |
103 ms |
297500 KB |
Output is correct |
13 |
Correct |
107 ms |
296276 KB |
Output is correct |
14 |
Correct |
136 ms |
296464 KB |
Output is correct |
15 |
Correct |
132 ms |
296528 KB |
Output is correct |
16 |
Correct |
103 ms |
296288 KB |
Output is correct |
17 |
Correct |
104 ms |
296248 KB |
Output is correct |
18 |
Correct |
133 ms |
296272 KB |
Output is correct |
19 |
Correct |
110 ms |
297596 KB |
Output is correct |
20 |
Correct |
111 ms |
297296 KB |
Output is correct |
21 |
Correct |
118 ms |
296532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
296528 KB |
Output is correct |
2 |
Correct |
105 ms |
297556 KB |
Output is correct |
3 |
Correct |
107 ms |
297552 KB |
Output is correct |
4 |
Correct |
121 ms |
297452 KB |
Output is correct |
5 |
Correct |
110 ms |
297488 KB |
Output is correct |
6 |
Correct |
119 ms |
297556 KB |
Output is correct |
7 |
Correct |
110 ms |
297240 KB |
Output is correct |
8 |
Correct |
106 ms |
296788 KB |
Output is correct |
9 |
Correct |
115 ms |
297500 KB |
Output is correct |
10 |
Correct |
125 ms |
297456 KB |
Output is correct |
11 |
Correct |
113 ms |
297416 KB |
Output is correct |
12 |
Correct |
103 ms |
297500 KB |
Output is correct |
13 |
Correct |
107 ms |
296276 KB |
Output is correct |
14 |
Correct |
136 ms |
296464 KB |
Output is correct |
15 |
Correct |
132 ms |
296528 KB |
Output is correct |
16 |
Correct |
103 ms |
296288 KB |
Output is correct |
17 |
Correct |
104 ms |
296248 KB |
Output is correct |
18 |
Correct |
133 ms |
296272 KB |
Output is correct |
19 |
Correct |
110 ms |
297596 KB |
Output is correct |
20 |
Correct |
111 ms |
297296 KB |
Output is correct |
21 |
Correct |
118 ms |
296532 KB |
Output is correct |
22 |
Correct |
113 ms |
300308 KB |
Output is correct |
23 |
Correct |
111 ms |
300264 KB |
Output is correct |
24 |
Correct |
112 ms |
300208 KB |
Output is correct |
25 |
Correct |
109 ms |
300112 KB |
Output is correct |
26 |
Correct |
109 ms |
299988 KB |
Output is correct |
27 |
Correct |
110 ms |
299960 KB |
Output is correct |
28 |
Correct |
104 ms |
300072 KB |
Output is correct |
29 |
Correct |
109 ms |
299344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
296528 KB |
Output is correct |
2 |
Correct |
105 ms |
297556 KB |
Output is correct |
3 |
Correct |
107 ms |
297552 KB |
Output is correct |
4 |
Correct |
121 ms |
297452 KB |
Output is correct |
5 |
Correct |
110 ms |
297488 KB |
Output is correct |
6 |
Correct |
119 ms |
297556 KB |
Output is correct |
7 |
Correct |
110 ms |
297240 KB |
Output is correct |
8 |
Correct |
106 ms |
296788 KB |
Output is correct |
9 |
Correct |
115 ms |
297500 KB |
Output is correct |
10 |
Correct |
125 ms |
297456 KB |
Output is correct |
11 |
Correct |
113 ms |
297416 KB |
Output is correct |
12 |
Correct |
103 ms |
297500 KB |
Output is correct |
13 |
Correct |
107 ms |
296276 KB |
Output is correct |
14 |
Correct |
136 ms |
296464 KB |
Output is correct |
15 |
Correct |
132 ms |
296528 KB |
Output is correct |
16 |
Correct |
103 ms |
296288 KB |
Output is correct |
17 |
Correct |
113 ms |
300308 KB |
Output is correct |
18 |
Correct |
111 ms |
300264 KB |
Output is correct |
19 |
Correct |
112 ms |
300208 KB |
Output is correct |
20 |
Correct |
109 ms |
300112 KB |
Output is correct |
21 |
Correct |
109 ms |
299988 KB |
Output is correct |
22 |
Correct |
110 ms |
299960 KB |
Output is correct |
23 |
Correct |
104 ms |
300072 KB |
Output is correct |
24 |
Correct |
109 ms |
299344 KB |
Output is correct |
25 |
Correct |
104 ms |
296248 KB |
Output is correct |
26 |
Correct |
133 ms |
296272 KB |
Output is correct |
27 |
Correct |
110 ms |
297596 KB |
Output is correct |
28 |
Correct |
111 ms |
297296 KB |
Output is correct |
29 |
Correct |
118 ms |
296532 KB |
Output is correct |
30 |
Correct |
117 ms |
310456 KB |
Output is correct |
31 |
Correct |
122 ms |
310852 KB |
Output is correct |
32 |
Correct |
122 ms |
310864 KB |
Output is correct |
33 |
Correct |
128 ms |
309080 KB |
Output is correct |
34 |
Correct |
117 ms |
309600 KB |
Output is correct |
35 |
Correct |
121 ms |
309584 KB |
Output is correct |
36 |
Correct |
118 ms |
309584 KB |
Output is correct |
37 |
Correct |
114 ms |
309332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
296528 KB |
Output is correct |
2 |
Correct |
105 ms |
297556 KB |
Output is correct |
3 |
Correct |
107 ms |
297552 KB |
Output is correct |
4 |
Correct |
121 ms |
297452 KB |
Output is correct |
5 |
Correct |
110 ms |
297488 KB |
Output is correct |
6 |
Correct |
119 ms |
297556 KB |
Output is correct |
7 |
Correct |
110 ms |
297240 KB |
Output is correct |
8 |
Correct |
106 ms |
296788 KB |
Output is correct |
9 |
Correct |
115 ms |
297500 KB |
Output is correct |
10 |
Correct |
125 ms |
297456 KB |
Output is correct |
11 |
Correct |
113 ms |
297416 KB |
Output is correct |
12 |
Correct |
103 ms |
297500 KB |
Output is correct |
13 |
Correct |
107 ms |
296276 KB |
Output is correct |
14 |
Correct |
136 ms |
296464 KB |
Output is correct |
15 |
Correct |
132 ms |
296528 KB |
Output is correct |
16 |
Correct |
103 ms |
296288 KB |
Output is correct |
17 |
Correct |
113 ms |
300308 KB |
Output is correct |
18 |
Correct |
111 ms |
300264 KB |
Output is correct |
19 |
Correct |
112 ms |
300208 KB |
Output is correct |
20 |
Correct |
109 ms |
300112 KB |
Output is correct |
21 |
Correct |
109 ms |
299988 KB |
Output is correct |
22 |
Correct |
110 ms |
299960 KB |
Output is correct |
23 |
Correct |
104 ms |
300072 KB |
Output is correct |
24 |
Correct |
109 ms |
299344 KB |
Output is correct |
25 |
Correct |
117 ms |
310456 KB |
Output is correct |
26 |
Correct |
122 ms |
310852 KB |
Output is correct |
27 |
Correct |
122 ms |
310864 KB |
Output is correct |
28 |
Correct |
128 ms |
309080 KB |
Output is correct |
29 |
Correct |
117 ms |
309600 KB |
Output is correct |
30 |
Correct |
121 ms |
309584 KB |
Output is correct |
31 |
Correct |
118 ms |
309584 KB |
Output is correct |
32 |
Correct |
114 ms |
309332 KB |
Output is correct |
33 |
Correct |
104 ms |
296248 KB |
Output is correct |
34 |
Correct |
133 ms |
296272 KB |
Output is correct |
35 |
Correct |
110 ms |
297596 KB |
Output is correct |
36 |
Correct |
111 ms |
297296 KB |
Output is correct |
37 |
Correct |
118 ms |
296532 KB |
Output is correct |
38 |
Correct |
202 ms |
394864 KB |
Output is correct |
39 |
Correct |
192 ms |
387188 KB |
Output is correct |
40 |
Correct |
196 ms |
387156 KB |
Output is correct |
41 |
Correct |
756 ms |
379972 KB |
Output is correct |
42 |
Correct |
229 ms |
408948 KB |
Output is correct |
43 |
Correct |
224 ms |
408656 KB |
Output is correct |
44 |
Correct |
241 ms |
409160 KB |
Output is correct |
45 |
Correct |
224 ms |
402004 KB |
Output is correct |
46 |
Correct |
230 ms |
384392 KB |
Output is correct |
47 |
Correct |
243 ms |
387036 KB |
Output is correct |
48 |
Correct |
274 ms |
392528 KB |
Output is correct |
49 |
Correct |
280 ms |
393808 KB |
Output is correct |
50 |
Correct |
206 ms |
355668 KB |
Output is correct |
51 |
Correct |
197 ms |
348084 KB |
Output is correct |
52 |
Correct |
276 ms |
391984 KB |
Output is correct |
53 |
Correct |
282 ms |
392428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
129 ms |
318008 KB |
Output is correct |
2 |
Correct |
116 ms |
314664 KB |
Output is correct |
3 |
Correct |
137 ms |
317780 KB |
Output is correct |
4 |
Correct |
112 ms |
296276 KB |
Output is correct |
5 |
Correct |
110 ms |
317780 KB |
Output is correct |
6 |
Correct |
127 ms |
317896 KB |
Output is correct |
7 |
Correct |
118 ms |
317920 KB |
Output is correct |
8 |
Correct |
117 ms |
317968 KB |
Output is correct |
9 |
Correct |
121 ms |
317780 KB |
Output is correct |
10 |
Correct |
123 ms |
317268 KB |
Output is correct |
11 |
Correct |
118 ms |
317544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
296248 KB |
Output is correct |
2 |
Correct |
133 ms |
296272 KB |
Output is correct |
3 |
Correct |
110 ms |
297596 KB |
Output is correct |
4 |
Correct |
111 ms |
297296 KB |
Output is correct |
5 |
Correct |
118 ms |
296532 KB |
Output is correct |
6 |
Correct |
114 ms |
296532 KB |
Output is correct |
7 |
Correct |
757 ms |
697696 KB |
Output is correct |
8 |
Runtime error |
1118 ms |
1048576 KB |
Execution killed with signal 9 |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
296528 KB |
Output is correct |
2 |
Correct |
105 ms |
297556 KB |
Output is correct |
3 |
Correct |
107 ms |
297552 KB |
Output is correct |
4 |
Correct |
121 ms |
297452 KB |
Output is correct |
5 |
Correct |
110 ms |
297488 KB |
Output is correct |
6 |
Correct |
119 ms |
297556 KB |
Output is correct |
7 |
Correct |
110 ms |
297240 KB |
Output is correct |
8 |
Correct |
106 ms |
296788 KB |
Output is correct |
9 |
Correct |
115 ms |
297500 KB |
Output is correct |
10 |
Correct |
125 ms |
297456 KB |
Output is correct |
11 |
Correct |
113 ms |
297416 KB |
Output is correct |
12 |
Correct |
103 ms |
297500 KB |
Output is correct |
13 |
Correct |
107 ms |
296276 KB |
Output is correct |
14 |
Correct |
136 ms |
296464 KB |
Output is correct |
15 |
Correct |
132 ms |
296528 KB |
Output is correct |
16 |
Correct |
103 ms |
296288 KB |
Output is correct |
17 |
Correct |
113 ms |
300308 KB |
Output is correct |
18 |
Correct |
111 ms |
300264 KB |
Output is correct |
19 |
Correct |
112 ms |
300208 KB |
Output is correct |
20 |
Correct |
109 ms |
300112 KB |
Output is correct |
21 |
Correct |
109 ms |
299988 KB |
Output is correct |
22 |
Correct |
110 ms |
299960 KB |
Output is correct |
23 |
Correct |
104 ms |
300072 KB |
Output is correct |
24 |
Correct |
109 ms |
299344 KB |
Output is correct |
25 |
Correct |
117 ms |
310456 KB |
Output is correct |
26 |
Correct |
122 ms |
310852 KB |
Output is correct |
27 |
Correct |
122 ms |
310864 KB |
Output is correct |
28 |
Correct |
128 ms |
309080 KB |
Output is correct |
29 |
Correct |
117 ms |
309600 KB |
Output is correct |
30 |
Correct |
121 ms |
309584 KB |
Output is correct |
31 |
Correct |
118 ms |
309584 KB |
Output is correct |
32 |
Correct |
114 ms |
309332 KB |
Output is correct |
33 |
Correct |
202 ms |
394864 KB |
Output is correct |
34 |
Correct |
192 ms |
387188 KB |
Output is correct |
35 |
Correct |
196 ms |
387156 KB |
Output is correct |
36 |
Correct |
756 ms |
379972 KB |
Output is correct |
37 |
Correct |
229 ms |
408948 KB |
Output is correct |
38 |
Correct |
224 ms |
408656 KB |
Output is correct |
39 |
Correct |
241 ms |
409160 KB |
Output is correct |
40 |
Correct |
224 ms |
402004 KB |
Output is correct |
41 |
Correct |
230 ms |
384392 KB |
Output is correct |
42 |
Correct |
243 ms |
387036 KB |
Output is correct |
43 |
Correct |
274 ms |
392528 KB |
Output is correct |
44 |
Correct |
280 ms |
393808 KB |
Output is correct |
45 |
Correct |
206 ms |
355668 KB |
Output is correct |
46 |
Correct |
197 ms |
348084 KB |
Output is correct |
47 |
Correct |
276 ms |
391984 KB |
Output is correct |
48 |
Correct |
282 ms |
392428 KB |
Output is correct |
49 |
Correct |
129 ms |
318008 KB |
Output is correct |
50 |
Correct |
116 ms |
314664 KB |
Output is correct |
51 |
Correct |
137 ms |
317780 KB |
Output is correct |
52 |
Correct |
112 ms |
296276 KB |
Output is correct |
53 |
Correct |
110 ms |
317780 KB |
Output is correct |
54 |
Correct |
127 ms |
317896 KB |
Output is correct |
55 |
Correct |
118 ms |
317920 KB |
Output is correct |
56 |
Correct |
117 ms |
317968 KB |
Output is correct |
57 |
Correct |
121 ms |
317780 KB |
Output is correct |
58 |
Correct |
123 ms |
317268 KB |
Output is correct |
59 |
Correct |
118 ms |
317544 KB |
Output is correct |
60 |
Correct |
114 ms |
296532 KB |
Output is correct |
61 |
Correct |
757 ms |
697696 KB |
Output is correct |
62 |
Runtime error |
1118 ms |
1048576 KB |
Execution killed with signal 9 |
63 |
Halted |
0 ms |
0 KB |
- |