#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2500;
int N, M, A[MAXN+10][MAXN+10];
int L[MAXN+10][MAXN+10], R[MAXN+10][MAXN+10], U[MAXN+10][MAXN+10], D[MAXN+10][MAXN+10];
struct Line
{
int y, x1, x2;
bool operator < (const Line &p) const
{
if(y!=p.y) return y<p.y;
if(x1!=p.x1) return x1<p.x1;
return x2<p.x2;
}
bool operator == (const Line &p) { return y==p.y && x1==p.x1 && x2==p.x2; }
};
vector<Line> H, V;
int lowH[MAXN*MAXN+10], lowV[MAXN*MAXN+10];
ll count_rectangles(vector<vector<int>> _A)
{
int i, j;
N=_A.size(); M=_A[0].size();
for(i=1; i<=N; i++) for(j=1; j<=M; j++) A[i][j]=_A[i-1][j-1];
for(i=1; i<=N; i++)
{
vector<int> S;
S.clear();
for(j=1; j<=M; j++) L[i][j]=0;
for(j=1; j<=M; j++)
{
while(!S.empty() && A[i][S.back()]<=A[i][j]) S.pop_back();
if(!S.empty()) L[i][j]=S.back();
S.push_back(j);
}
S.clear();
for(j=1; j<=M; j++) R[i][j]=M+1;
for(j=M; j>=1; j--)
{
while(!S.empty() && A[i][S.back()]<=A[i][j]) S.pop_back();
if(!S.empty()) R[i][j]=S.back();
S.push_back(j);
}
}
for(i=1; i<=M; i++)
{
vector<int> S;
S.clear();
for(j=1; j<=N; j++) U[j][i]=0;
for(j=1; j<=N; j++)
{
while(!S.empty() && A[S.back()][i]<=A[j][i]) S.pop_back();
if(!S.empty()) U[j][i]=S.back();
S.push_back(j);
}
S.clear();
for(j=1; j<=N; j++) D[j][i]=N+1;
for(j=N; j>=1; j--)
{
while(!S.empty() && A[S.back()][i]<=A[j][i]) S.pop_back();
if(!S.empty()) D[j][i]=S.back();
S.push_back(j);
}
}
for(i=1; i<=N; i++)
{
for(j=1; j<=M; j++)
{
if(L[i][j]==0) continue;
if(R[i][j]==M+1) continue;
H.push_back({i, L[i][j]+1, R[i][j]-1});
}
for(j=1; j<=M; j++)
{
if(U[i][j]==0) continue;
if(D[i][j]==N+1) continue;
V.push_back({j, U[i][j]+1, D[i][j]-1});
}
}
sort(H.begin(), H.end());
H.erase(unique(H.begin(), H.end()), H.end());
sort(V.begin(), V.end());
V.erase(unique(V.begin(), V.end()), V.end());
for(i=H.size()-1; i>=0; i--)
{
lowH[i]=H[i].y;
Line t={H[i].y+1, H[i].x1, H[i].x2};
if(binary_search(H.begin(), H.end(), t)) lowH[i]=lowH[lower_bound(H.begin(), H.end(), t)-H.begin()];
}
for(i=V.size()-1; i>=0; i--)
{
lowV[i]=V[i].y;
Line t={V[i].y+1, V[i].x1, V[i].x2};
if(binary_search(V.begin(), V.end(), t)) lowV[i]=lowV[lower_bound(V.begin(), V.end(), t)-V.begin()];
}
vector<tuple<int, int, int, int>> ans;
for(i=1; i<=N; i++) for(j=1; j<=M; j++)
{
//printf("%d %d : %d %d %d %d\n", i, j, L[i][j], R[i][j], U[i][j], D[i][j]);
if(L[i][j]==0) continue;
if(R[i][j]==M+1) continue;
if(U[i][j]==0) continue;
if(D[i][j]==N+1) continue;
Line tu={U[i][j]+1, L[i][j]+1, R[i][j]-1};
if(!binary_search(H.begin(), H.end(), tu)) continue;
Line tl={L[i][j]+1, U[i][j]+1, D[i][j]-1};
if(!binary_search(V.begin(), V.end(), tl)) continue;
if(lowH[lower_bound(H.begin(), H.end(), tu)-H.begin()]<D[i][j]-1) continue;
if(lowV[lower_bound(V.begin(), V.end(), tl)-V.begin()]<R[i][j]-1) continue;
ans.push_back(make_tuple(L[i][j]+1, R[i][j]-1, U[i][j]+1, D[i][j]-1));
}
sort(ans.begin(), ans.end());
ans.erase(unique(ans.begin(), ans.end()), ans.end());
return ans.size();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
1016 KB |
Output is correct |
3 |
Correct |
3 ms |
1016 KB |
Output is correct |
4 |
Correct |
13 ms |
1144 KB |
Output is correct |
5 |
Correct |
4 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1016 KB |
Output is correct |
7 |
Correct |
3 ms |
1016 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
9 ms |
1016 KB |
Output is correct |
10 |
Correct |
3 ms |
1016 KB |
Output is correct |
11 |
Correct |
3 ms |
1016 KB |
Output is correct |
12 |
Correct |
3 ms |
1016 KB |
Output is correct |
13 |
Correct |
1 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
0 ms |
376 KB |
Output is correct |
17 |
Correct |
3 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
964 KB |
Output is correct |
20 |
Correct |
3 ms |
888 KB |
Output is correct |
21 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
1016 KB |
Output is correct |
3 |
Correct |
3 ms |
1016 KB |
Output is correct |
4 |
Correct |
13 ms |
1144 KB |
Output is correct |
5 |
Correct |
4 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1016 KB |
Output is correct |
7 |
Correct |
3 ms |
1016 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
9 ms |
1016 KB |
Output is correct |
10 |
Correct |
3 ms |
1016 KB |
Output is correct |
11 |
Correct |
3 ms |
1016 KB |
Output is correct |
12 |
Correct |
3 ms |
1016 KB |
Output is correct |
13 |
Correct |
1 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
0 ms |
376 KB |
Output is correct |
17 |
Correct |
10 ms |
2752 KB |
Output is correct |
18 |
Correct |
10 ms |
2680 KB |
Output is correct |
19 |
Correct |
9 ms |
2680 KB |
Output is correct |
20 |
Correct |
6 ms |
2424 KB |
Output is correct |
21 |
Correct |
6 ms |
2468 KB |
Output is correct |
22 |
Correct |
8 ms |
2424 KB |
Output is correct |
23 |
Correct |
8 ms |
2552 KB |
Output is correct |
24 |
Correct |
5 ms |
2168 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
964 KB |
Output is correct |
28 |
Correct |
3 ms |
888 KB |
Output is correct |
29 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
1016 KB |
Output is correct |
3 |
Correct |
3 ms |
1016 KB |
Output is correct |
4 |
Correct |
13 ms |
1144 KB |
Output is correct |
5 |
Correct |
4 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1016 KB |
Output is correct |
7 |
Correct |
3 ms |
1016 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
9 ms |
1016 KB |
Output is correct |
10 |
Correct |
3 ms |
1016 KB |
Output is correct |
11 |
Correct |
3 ms |
1016 KB |
Output is correct |
12 |
Correct |
3 ms |
1016 KB |
Output is correct |
13 |
Correct |
1 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
0 ms |
376 KB |
Output is correct |
17 |
Correct |
10 ms |
2752 KB |
Output is correct |
18 |
Correct |
10 ms |
2680 KB |
Output is correct |
19 |
Correct |
9 ms |
2680 KB |
Output is correct |
20 |
Correct |
6 ms |
2424 KB |
Output is correct |
21 |
Correct |
6 ms |
2468 KB |
Output is correct |
22 |
Correct |
8 ms |
2424 KB |
Output is correct |
23 |
Correct |
8 ms |
2552 KB |
Output is correct |
24 |
Correct |
5 ms |
2168 KB |
Output is correct |
25 |
Correct |
49 ms |
8424 KB |
Output is correct |
26 |
Correct |
56 ms |
8556 KB |
Output is correct |
27 |
Correct |
49 ms |
8556 KB |
Output is correct |
28 |
Correct |
26 ms |
6624 KB |
Output is correct |
29 |
Correct |
41 ms |
7276 KB |
Output is correct |
30 |
Correct |
42 ms |
7400 KB |
Output is correct |
31 |
Correct |
43 ms |
7720 KB |
Output is correct |
32 |
Correct |
43 ms |
7336 KB |
Output is correct |
33 |
Correct |
3 ms |
376 KB |
Output is correct |
34 |
Correct |
2 ms |
376 KB |
Output is correct |
35 |
Correct |
3 ms |
964 KB |
Output is correct |
36 |
Correct |
3 ms |
888 KB |
Output is correct |
37 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
1016 KB |
Output is correct |
3 |
Correct |
3 ms |
1016 KB |
Output is correct |
4 |
Correct |
13 ms |
1144 KB |
Output is correct |
5 |
Correct |
4 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1016 KB |
Output is correct |
7 |
Correct |
3 ms |
1016 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
9 ms |
1016 KB |
Output is correct |
10 |
Correct |
3 ms |
1016 KB |
Output is correct |
11 |
Correct |
3 ms |
1016 KB |
Output is correct |
12 |
Correct |
3 ms |
1016 KB |
Output is correct |
13 |
Correct |
1 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
0 ms |
376 KB |
Output is correct |
17 |
Correct |
10 ms |
2752 KB |
Output is correct |
18 |
Correct |
10 ms |
2680 KB |
Output is correct |
19 |
Correct |
9 ms |
2680 KB |
Output is correct |
20 |
Correct |
6 ms |
2424 KB |
Output is correct |
21 |
Correct |
6 ms |
2468 KB |
Output is correct |
22 |
Correct |
8 ms |
2424 KB |
Output is correct |
23 |
Correct |
8 ms |
2552 KB |
Output is correct |
24 |
Correct |
5 ms |
2168 KB |
Output is correct |
25 |
Correct |
49 ms |
8424 KB |
Output is correct |
26 |
Correct |
56 ms |
8556 KB |
Output is correct |
27 |
Correct |
49 ms |
8556 KB |
Output is correct |
28 |
Correct |
26 ms |
6624 KB |
Output is correct |
29 |
Correct |
41 ms |
7276 KB |
Output is correct |
30 |
Correct |
42 ms |
7400 KB |
Output is correct |
31 |
Correct |
43 ms |
7720 KB |
Output is correct |
32 |
Correct |
43 ms |
7336 KB |
Output is correct |
33 |
Correct |
160 ms |
38628 KB |
Output is correct |
34 |
Correct |
160 ms |
38796 KB |
Output is correct |
35 |
Correct |
152 ms |
38808 KB |
Output is correct |
36 |
Correct |
135 ms |
38944 KB |
Output is correct |
37 |
Correct |
669 ms |
58712 KB |
Output is correct |
38 |
Correct |
662 ms |
58948 KB |
Output is correct |
39 |
Correct |
668 ms |
59144 KB |
Output is correct |
40 |
Correct |
617 ms |
56212 KB |
Output is correct |
41 |
Correct |
216 ms |
36460 KB |
Output is correct |
42 |
Correct |
295 ms |
39984 KB |
Output is correct |
43 |
Correct |
531 ms |
51440 KB |
Output is correct |
44 |
Correct |
520 ms |
53576 KB |
Output is correct |
45 |
Correct |
252 ms |
33808 KB |
Output is correct |
46 |
Correct |
243 ms |
26796 KB |
Output is correct |
47 |
Correct |
538 ms |
53368 KB |
Output is correct |
48 |
Correct |
548 ms |
54188 KB |
Output is correct |
49 |
Correct |
3 ms |
376 KB |
Output is correct |
50 |
Correct |
2 ms |
376 KB |
Output is correct |
51 |
Correct |
3 ms |
964 KB |
Output is correct |
52 |
Correct |
3 ms |
888 KB |
Output is correct |
53 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
888 KB |
Output is correct |
2 |
Correct |
5 ms |
860 KB |
Output is correct |
3 |
Correct |
3 ms |
632 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
888 KB |
Output is correct |
6 |
Correct |
6 ms |
888 KB |
Output is correct |
7 |
Correct |
4 ms |
888 KB |
Output is correct |
8 |
Correct |
6 ms |
888 KB |
Output is correct |
9 |
Correct |
9 ms |
1016 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
1351 ms |
134892 KB |
Output is correct |
3 |
Correct |
3395 ms |
281720 KB |
Output is correct |
4 |
Correct |
3222 ms |
283420 KB |
Output is correct |
5 |
Correct |
3242 ms |
283196 KB |
Output is correct |
6 |
Correct |
326 ms |
91256 KB |
Output is correct |
7 |
Correct |
727 ms |
180856 KB |
Output is correct |
8 |
Correct |
741 ms |
184500 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
964 KB |
Output is correct |
12 |
Correct |
3 ms |
888 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
1016 KB |
Output is correct |
3 |
Correct |
3 ms |
1016 KB |
Output is correct |
4 |
Correct |
13 ms |
1144 KB |
Output is correct |
5 |
Correct |
4 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1016 KB |
Output is correct |
7 |
Correct |
3 ms |
1016 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
9 ms |
1016 KB |
Output is correct |
10 |
Correct |
3 ms |
1016 KB |
Output is correct |
11 |
Correct |
3 ms |
1016 KB |
Output is correct |
12 |
Correct |
3 ms |
1016 KB |
Output is correct |
13 |
Correct |
1 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
0 ms |
376 KB |
Output is correct |
17 |
Correct |
10 ms |
2752 KB |
Output is correct |
18 |
Correct |
10 ms |
2680 KB |
Output is correct |
19 |
Correct |
9 ms |
2680 KB |
Output is correct |
20 |
Correct |
6 ms |
2424 KB |
Output is correct |
21 |
Correct |
6 ms |
2468 KB |
Output is correct |
22 |
Correct |
8 ms |
2424 KB |
Output is correct |
23 |
Correct |
8 ms |
2552 KB |
Output is correct |
24 |
Correct |
5 ms |
2168 KB |
Output is correct |
25 |
Correct |
49 ms |
8424 KB |
Output is correct |
26 |
Correct |
56 ms |
8556 KB |
Output is correct |
27 |
Correct |
49 ms |
8556 KB |
Output is correct |
28 |
Correct |
26 ms |
6624 KB |
Output is correct |
29 |
Correct |
41 ms |
7276 KB |
Output is correct |
30 |
Correct |
42 ms |
7400 KB |
Output is correct |
31 |
Correct |
43 ms |
7720 KB |
Output is correct |
32 |
Correct |
43 ms |
7336 KB |
Output is correct |
33 |
Correct |
160 ms |
38628 KB |
Output is correct |
34 |
Correct |
160 ms |
38796 KB |
Output is correct |
35 |
Correct |
152 ms |
38808 KB |
Output is correct |
36 |
Correct |
135 ms |
38944 KB |
Output is correct |
37 |
Correct |
669 ms |
58712 KB |
Output is correct |
38 |
Correct |
662 ms |
58948 KB |
Output is correct |
39 |
Correct |
668 ms |
59144 KB |
Output is correct |
40 |
Correct |
617 ms |
56212 KB |
Output is correct |
41 |
Correct |
216 ms |
36460 KB |
Output is correct |
42 |
Correct |
295 ms |
39984 KB |
Output is correct |
43 |
Correct |
531 ms |
51440 KB |
Output is correct |
44 |
Correct |
520 ms |
53576 KB |
Output is correct |
45 |
Correct |
252 ms |
33808 KB |
Output is correct |
46 |
Correct |
243 ms |
26796 KB |
Output is correct |
47 |
Correct |
538 ms |
53368 KB |
Output is correct |
48 |
Correct |
548 ms |
54188 KB |
Output is correct |
49 |
Correct |
6 ms |
888 KB |
Output is correct |
50 |
Correct |
5 ms |
860 KB |
Output is correct |
51 |
Correct |
3 ms |
632 KB |
Output is correct |
52 |
Correct |
3 ms |
376 KB |
Output is correct |
53 |
Correct |
6 ms |
888 KB |
Output is correct |
54 |
Correct |
6 ms |
888 KB |
Output is correct |
55 |
Correct |
4 ms |
888 KB |
Output is correct |
56 |
Correct |
6 ms |
888 KB |
Output is correct |
57 |
Correct |
9 ms |
1016 KB |
Output is correct |
58 |
Correct |
3 ms |
504 KB |
Output is correct |
59 |
Correct |
3 ms |
732 KB |
Output is correct |
60 |
Correct |
2 ms |
632 KB |
Output is correct |
61 |
Correct |
1351 ms |
134892 KB |
Output is correct |
62 |
Correct |
3395 ms |
281720 KB |
Output is correct |
63 |
Correct |
3222 ms |
283420 KB |
Output is correct |
64 |
Correct |
3242 ms |
283196 KB |
Output is correct |
65 |
Correct |
326 ms |
91256 KB |
Output is correct |
66 |
Correct |
727 ms |
180856 KB |
Output is correct |
67 |
Correct |
741 ms |
184500 KB |
Output is correct |
68 |
Correct |
2278 ms |
328380 KB |
Output is correct |
69 |
Correct |
3542 ms |
328444 KB |
Output is correct |
70 |
Correct |
2099 ms |
328316 KB |
Output is correct |
71 |
Correct |
1988 ms |
328384 KB |
Output is correct |
72 |
Execution timed out |
5046 ms |
429064 KB |
Time limit exceeded |
73 |
Halted |
0 ms |
0 KB |
- |