#include "rect.h"
#include<bits/stdc++.h>
using namespace std;
long long count_rectangles(vector<vector<int> > a)
{
int n = a.size();
int m = a[0].size();
int max_val = 0;
int min_val = (1<<30);
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
{
max_val = max(max_val, a[i][j]);
min_val = min(min_val, a[i][j]);
}
if(max_val == min_val)
return 0;
long long ans = 0;
if(max_val == 1)
{
vector<vector<int> >viz(n, vector<int>(m));
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
viz[i][j] = 0;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
{
if(a[i][j] == 0 && !viz[i][j])
{
int minix = i, maxix = i, miniy = j, maxiy = j;
int cnt = 1;
viz[i][j] = 1;
deque<pair<int, int> >d;
d.push_back({i, j});
while(!d.empty())
{
pair<int, int> nod = d[0];
d.pop_front();
for(int ox = -1; ox <= 1; ++ox)
for(int oy = -1; oy <= 1; ++oy)
{
if(ox && oy)
continue;
if(!ox && !oy)
continue;
int new_x = nod.first + ox;
int new_y = nod.second + oy;
if(new_x >= 0 && new_y >= 0 && new_x < n && new_y < m)
{
if(a[new_x][new_y] == 0 && !viz[new_x][new_y])
{
viz[new_x][new_y] = 1;
d.push_back({new_x, new_y});
minix = min(minix, new_x);
maxix = max(maxix, new_x);
miniy = min(miniy, new_y);
maxiy = max(maxiy, new_y);
++cnt;
}
}
}
}
if(minix && miniy && maxix < n-1 && maxiy < m-1)
{
if(cnt == (maxix - minix + 1) * (maxiy - miniy + 1))
++ans;
}
}
}
}
else
{
int maxst[2502][2502], maxdr[2502][2502], maxup[2502][2502], maxdwn[2502][2502];
deque<int> d;
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < m; ++j)
{
while(!d.empty() && a[i][j] > a[i][d.back()])
d.pop_back();
if(d.empty())
maxst[i][j] = -1;
else
maxst[i][j] = d.back();
d.push_back(j);
}
d.clear();
for(int j = m - 1; j >= 0; --j)
{
while(!d.empty() && a[i][j] > a[i][d.back()])
d.pop_back();
if(d.empty())
maxdr[i][j] = m;
else
maxdr[i][j] = d.back();
d.push_back(j);
}
d.clear();
}
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
while(!d.empty() && a[j][i] > a[d.back()][i])
d.pop_back();
if(d.empty())
maxup[j][i] = -1;
else
maxup[j][i] = d.back();
d.push_back(j);
}
d.clear();
for(int j = n-1; j >= 0; --j)
{
while(!d.empty() && a[j][i] > a[d.back()][i])
d.pop_back();
if(d.empty())
maxdwn[j][i] = n;
else
maxdwn[j][i] = d.back();
d.push_back(j);
}
d.clear();
}
int rmq[2502][15][2502];
int rmq2[2502][15][2502];
int Logg[2502] = {0};
for(int i = 2; i <= 2500; ++i)
Logg[i] = Logg[i/2] + 1;
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
rmq[i][0][j] = maxst[j][i];
for(int stp = 1; (1<<stp) <= n; ++stp)
for(int j = 0; j + (1<<stp) - 1 < n; ++j)
rmq[i][stp][j] = max(rmq[i][stp-1][j], rmq[i][stp-1][j + (1<<(stp - 1))]);
for(int j = 0; j < n; ++j)
rmq2[i][0][j] = maxdr[j][i];
for(int stp = 1; (1<<stp) <= n; ++stp)
for(int j = 0; j + (1<<stp) - 1 < n; ++j)
rmq2[i][stp][j] = min(rmq2[i][stp-1][j], rmq2[i][stp-1][j + (1<<(stp - 1))]);
}
for(int borderup = 0; borderup < n; ++borderup)
for(int borderdown = borderup + 2; borderdown < n; ++borderdown)
{
for(int i = 0; i + 2 < m; ++i)
{
int lg = Logg[borderdown - borderup - 1];
int mx = min(rmq2[i][lg][borderup + 1], rmq2[i][lg][(borderdown - 1) - (1<<lg) + 1]);
bool ok = 1;
for(int j = i+1; j < mx && ok; ++j)
{
ok &= (maxdwn[borderup][j] >= borderdown && maxup[borderdown][j] <= borderup);
if(ok && j+1 < m)
{
int mx2 = max(rmq[j+1][lg][borderup + 1], rmq[j+1][lg][(borderdown - 1) - (1<<lg) + 1]);
if(mx2 <= i)
++ans;
}
}
}
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
6 ms |
2040 KB |
Output is correct |
3 |
Correct |
6 ms |
2040 KB |
Output is correct |
4 |
Correct |
6 ms |
2168 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
6 ms |
2040 KB |
Output is correct |
7 |
Correct |
5 ms |
1400 KB |
Output is correct |
8 |
Correct |
5 ms |
1528 KB |
Output is correct |
9 |
Correct |
6 ms |
2044 KB |
Output is correct |
10 |
Correct |
6 ms |
2040 KB |
Output is correct |
11 |
Correct |
5 ms |
2040 KB |
Output is correct |
12 |
Correct |
6 ms |
2040 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
256 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
6 ms |
2040 KB |
Output is correct |
3 |
Correct |
6 ms |
2040 KB |
Output is correct |
4 |
Correct |
6 ms |
2168 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
6 ms |
2040 KB |
Output is correct |
7 |
Correct |
5 ms |
1400 KB |
Output is correct |
8 |
Correct |
5 ms |
1528 KB |
Output is correct |
9 |
Correct |
6 ms |
2044 KB |
Output is correct |
10 |
Correct |
6 ms |
2040 KB |
Output is correct |
11 |
Correct |
5 ms |
2040 KB |
Output is correct |
12 |
Correct |
6 ms |
2040 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
11 ms |
6648 KB |
Output is correct |
18 |
Correct |
10 ms |
6520 KB |
Output is correct |
19 |
Correct |
10 ms |
6648 KB |
Output is correct |
20 |
Correct |
10 ms |
6648 KB |
Output is correct |
21 |
Correct |
10 ms |
6520 KB |
Output is correct |
22 |
Correct |
10 ms |
6524 KB |
Output is correct |
23 |
Correct |
10 ms |
6520 KB |
Output is correct |
24 |
Correct |
7 ms |
4088 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
256 KB |
Output is correct |
27 |
Correct |
5 ms |
376 KB |
Output is correct |
28 |
Correct |
5 ms |
376 KB |
Output is correct |
29 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
6 ms |
2040 KB |
Output is correct |
3 |
Correct |
6 ms |
2040 KB |
Output is correct |
4 |
Correct |
6 ms |
2168 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
6 ms |
2040 KB |
Output is correct |
7 |
Correct |
5 ms |
1400 KB |
Output is correct |
8 |
Correct |
5 ms |
1528 KB |
Output is correct |
9 |
Correct |
6 ms |
2044 KB |
Output is correct |
10 |
Correct |
6 ms |
2040 KB |
Output is correct |
11 |
Correct |
5 ms |
2040 KB |
Output is correct |
12 |
Correct |
6 ms |
2040 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
11 ms |
6648 KB |
Output is correct |
18 |
Correct |
10 ms |
6520 KB |
Output is correct |
19 |
Correct |
10 ms |
6648 KB |
Output is correct |
20 |
Correct |
10 ms |
6648 KB |
Output is correct |
21 |
Correct |
10 ms |
6520 KB |
Output is correct |
22 |
Correct |
10 ms |
6524 KB |
Output is correct |
23 |
Correct |
10 ms |
6520 KB |
Output is correct |
24 |
Correct |
7 ms |
4088 KB |
Output is correct |
25 |
Correct |
51 ms |
19448 KB |
Output is correct |
26 |
Correct |
53 ms |
19448 KB |
Output is correct |
27 |
Correct |
58 ms |
19448 KB |
Output is correct |
28 |
Correct |
37 ms |
19576 KB |
Output is correct |
29 |
Correct |
37 ms |
19448 KB |
Output is correct |
30 |
Correct |
38 ms |
19544 KB |
Output is correct |
31 |
Correct |
38 ms |
19576 KB |
Output is correct |
32 |
Correct |
38 ms |
19320 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
5 ms |
256 KB |
Output is correct |
35 |
Correct |
5 ms |
376 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
6 ms |
2040 KB |
Output is correct |
3 |
Correct |
6 ms |
2040 KB |
Output is correct |
4 |
Correct |
6 ms |
2168 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
6 ms |
2040 KB |
Output is correct |
7 |
Correct |
5 ms |
1400 KB |
Output is correct |
8 |
Correct |
5 ms |
1528 KB |
Output is correct |
9 |
Correct |
6 ms |
2044 KB |
Output is correct |
10 |
Correct |
6 ms |
2040 KB |
Output is correct |
11 |
Correct |
5 ms |
2040 KB |
Output is correct |
12 |
Correct |
6 ms |
2040 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
11 ms |
6648 KB |
Output is correct |
18 |
Correct |
10 ms |
6520 KB |
Output is correct |
19 |
Correct |
10 ms |
6648 KB |
Output is correct |
20 |
Correct |
10 ms |
6648 KB |
Output is correct |
21 |
Correct |
10 ms |
6520 KB |
Output is correct |
22 |
Correct |
10 ms |
6524 KB |
Output is correct |
23 |
Correct |
10 ms |
6520 KB |
Output is correct |
24 |
Correct |
7 ms |
4088 KB |
Output is correct |
25 |
Correct |
51 ms |
19448 KB |
Output is correct |
26 |
Correct |
53 ms |
19448 KB |
Output is correct |
27 |
Correct |
58 ms |
19448 KB |
Output is correct |
28 |
Correct |
37 ms |
19576 KB |
Output is correct |
29 |
Correct |
37 ms |
19448 KB |
Output is correct |
30 |
Correct |
38 ms |
19544 KB |
Output is correct |
31 |
Correct |
38 ms |
19576 KB |
Output is correct |
32 |
Correct |
38 ms |
19320 KB |
Output is correct |
33 |
Correct |
1022 ms |
112376 KB |
Output is correct |
34 |
Correct |
1187 ms |
112252 KB |
Output is correct |
35 |
Correct |
1108 ms |
112376 KB |
Output is correct |
36 |
Correct |
1058 ms |
112376 KB |
Output is correct |
37 |
Correct |
1938 ms |
112376 KB |
Output is correct |
38 |
Correct |
1845 ms |
112104 KB |
Output is correct |
39 |
Correct |
1774 ms |
112376 KB |
Output is correct |
40 |
Correct |
1591 ms |
108284 KB |
Output is correct |
41 |
Correct |
36 ms |
6136 KB |
Output is correct |
42 |
Correct |
848 ms |
112248 KB |
Output is correct |
43 |
Correct |
888 ms |
112376 KB |
Output is correct |
44 |
Correct |
874 ms |
112376 KB |
Output is correct |
45 |
Correct |
424 ms |
61944 KB |
Output is correct |
46 |
Correct |
273 ms |
76920 KB |
Output is correct |
47 |
Correct |
918 ms |
112416 KB |
Output is correct |
48 |
Correct |
921 ms |
112248 KB |
Output is correct |
49 |
Correct |
5 ms |
376 KB |
Output is correct |
50 |
Correct |
5 ms |
256 KB |
Output is correct |
51 |
Correct |
5 ms |
376 KB |
Output is correct |
52 |
Correct |
5 ms |
376 KB |
Output is correct |
53 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
42104 KB |
Output is correct |
2 |
Correct |
45 ms |
35704 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
25 ms |
41976 KB |
Output is correct |
6 |
Correct |
25 ms |
41980 KB |
Output is correct |
7 |
Correct |
25 ms |
41976 KB |
Output is correct |
8 |
Correct |
25 ms |
41976 KB |
Output is correct |
9 |
Correct |
24 ms |
41976 KB |
Output is correct |
10 |
Correct |
15 ms |
21880 KB |
Output is correct |
11 |
Correct |
23 ms |
41848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
188 ms |
34044 KB |
Output is correct |
3 |
Correct |
400 ms |
73592 KB |
Output is correct |
4 |
Correct |
408 ms |
73976 KB |
Output is correct |
5 |
Correct |
404 ms |
73976 KB |
Output is correct |
6 |
Correct |
239 ms |
36856 KB |
Output is correct |
7 |
Correct |
517 ms |
69368 KB |
Output is correct |
8 |
Correct |
585 ms |
74104 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
6 ms |
2040 KB |
Output is correct |
3 |
Correct |
6 ms |
2040 KB |
Output is correct |
4 |
Correct |
6 ms |
2168 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
6 ms |
2040 KB |
Output is correct |
7 |
Correct |
5 ms |
1400 KB |
Output is correct |
8 |
Correct |
5 ms |
1528 KB |
Output is correct |
9 |
Correct |
6 ms |
2044 KB |
Output is correct |
10 |
Correct |
6 ms |
2040 KB |
Output is correct |
11 |
Correct |
5 ms |
2040 KB |
Output is correct |
12 |
Correct |
6 ms |
2040 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
11 ms |
6648 KB |
Output is correct |
18 |
Correct |
10 ms |
6520 KB |
Output is correct |
19 |
Correct |
10 ms |
6648 KB |
Output is correct |
20 |
Correct |
10 ms |
6648 KB |
Output is correct |
21 |
Correct |
10 ms |
6520 KB |
Output is correct |
22 |
Correct |
10 ms |
6524 KB |
Output is correct |
23 |
Correct |
10 ms |
6520 KB |
Output is correct |
24 |
Correct |
7 ms |
4088 KB |
Output is correct |
25 |
Correct |
51 ms |
19448 KB |
Output is correct |
26 |
Correct |
53 ms |
19448 KB |
Output is correct |
27 |
Correct |
58 ms |
19448 KB |
Output is correct |
28 |
Correct |
37 ms |
19576 KB |
Output is correct |
29 |
Correct |
37 ms |
19448 KB |
Output is correct |
30 |
Correct |
38 ms |
19544 KB |
Output is correct |
31 |
Correct |
38 ms |
19576 KB |
Output is correct |
32 |
Correct |
38 ms |
19320 KB |
Output is correct |
33 |
Correct |
1022 ms |
112376 KB |
Output is correct |
34 |
Correct |
1187 ms |
112252 KB |
Output is correct |
35 |
Correct |
1108 ms |
112376 KB |
Output is correct |
36 |
Correct |
1058 ms |
112376 KB |
Output is correct |
37 |
Correct |
1938 ms |
112376 KB |
Output is correct |
38 |
Correct |
1845 ms |
112104 KB |
Output is correct |
39 |
Correct |
1774 ms |
112376 KB |
Output is correct |
40 |
Correct |
1591 ms |
108284 KB |
Output is correct |
41 |
Correct |
36 ms |
6136 KB |
Output is correct |
42 |
Correct |
848 ms |
112248 KB |
Output is correct |
43 |
Correct |
888 ms |
112376 KB |
Output is correct |
44 |
Correct |
874 ms |
112376 KB |
Output is correct |
45 |
Correct |
424 ms |
61944 KB |
Output is correct |
46 |
Correct |
273 ms |
76920 KB |
Output is correct |
47 |
Correct |
918 ms |
112416 KB |
Output is correct |
48 |
Correct |
921 ms |
112248 KB |
Output is correct |
49 |
Correct |
68 ms |
42104 KB |
Output is correct |
50 |
Correct |
45 ms |
35704 KB |
Output is correct |
51 |
Correct |
5 ms |
376 KB |
Output is correct |
52 |
Correct |
5 ms |
376 KB |
Output is correct |
53 |
Correct |
25 ms |
41976 KB |
Output is correct |
54 |
Correct |
25 ms |
41980 KB |
Output is correct |
55 |
Correct |
25 ms |
41976 KB |
Output is correct |
56 |
Correct |
25 ms |
41976 KB |
Output is correct |
57 |
Correct |
24 ms |
41976 KB |
Output is correct |
58 |
Correct |
15 ms |
21880 KB |
Output is correct |
59 |
Correct |
23 ms |
41848 KB |
Output is correct |
60 |
Correct |
5 ms |
256 KB |
Output is correct |
61 |
Correct |
188 ms |
34044 KB |
Output is correct |
62 |
Correct |
400 ms |
73592 KB |
Output is correct |
63 |
Correct |
408 ms |
73976 KB |
Output is correct |
64 |
Correct |
404 ms |
73976 KB |
Output is correct |
65 |
Correct |
239 ms |
36856 KB |
Output is correct |
66 |
Correct |
517 ms |
69368 KB |
Output is correct |
67 |
Correct |
585 ms |
74104 KB |
Output is correct |
68 |
Execution timed out |
5107 ms |
715300 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |