#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#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, x;
Line(int y, int x1, int x2) : y(y), x(x1*3000+x2) {}
bool operator < (const Line &p) const
{
if(y!=p.y) return y<p.y;
return x<p.x;
}
bool operator == (const Line &p) { return y==p.y && x==p.x; }
bool operator != (const Line &p) { return !(y==p.y && x==p.x); }
};
vector<Line> H, V;
int lowH[MAXN*MAXN+10], lowV[MAXN*MAXN+10];
vector<pii> ans;
int posH[MAXN+10][MAXN+10], posV[MAXN+10][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(Line(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(Line(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, j=H.size()-1; i>=0; i--)
{
lowH[i]=H[i].y;
Line t=Line(H[i].y+1, H[i].x/3000, H[i].x%3000);
for(; j>=0 && t<H[j]; j--);
if(j>=0 && t==H[j]) lowH[i]=lowH[j];
}
for(i=V.size()-1, j=V.size()-1; i>=0; i--)
{
lowV[i]=V[i].y;
Line t=Line(V[i].y+1, V[i].x/3000, V[i].x%3000);
for(; j>=0 && t<V[j]; j--);
if(j>=0 && t==V[j]) lowV[i]=lowV[j];
}
vector<pair<Line, int>> HT, VT;
for(i=2; i<=N-1; i++) for(j=2; j<=M-1; 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};
Line tl={L[i][j]+1, U[i][j]+1, D[i][j]-1};
HT.push_back({tu, i*3000+j});
VT.push_back({tl, i*3000+j});
}
sort(HT.begin(), HT.end());
for(i=0, j=0; i<HT.size(); i++)
{
for(; j<H.size() && H[j]<HT[i].first; j++);
if(j<H.size() && HT[i].first==H[j]) posH[HT[i].second/3000][HT[i].second%3000]=j;
else posH[HT[i].second/3000][HT[i].second%3000]=-1;
}
sort(VT.begin(), VT.end());
for(i=0, j=0; i<VT.size(); i++)
{
for(; j<V.size() && V[j]<VT[i].first; j++);
if(j<V.size() && VT[i].first==V[j]) posV[VT[i].second/3000][VT[i].second%3000]=j;
else posV[VT[i].second/3000][VT[i].second%3000]=-1;
}
for(i=2; i<=N-1; i++) for(j=2; j<=M-1; 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;
int it=posH[i][j];
int jt=posV[i][j];
if(it==-1 || jt==-1) continue;
if(lowH[it]<D[i][j]-1) continue;
if(lowV[jt]<R[i][j]-1) continue;
ans.push_back({L[i][j]*3000+R[i][j], U[i][j]*3000+D[i][j]});
}
sort(ans.begin(), ans.end());
return unique(ans.begin(), ans.end())-ans.begin();
}
Compilation message
rect.cpp: In function 'll count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:142:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0, j=0; i<HT.size(); i++)
~^~~~~~~~~~
rect.cpp:144:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; j<H.size() && H[j]<HT[i].first; j++);
~^~~~~~~~~
rect.cpp:145:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(j<H.size() && HT[i].first==H[j]) posH[HT[i].second/3000][HT[i].second%3000]=j;
~^~~~~~~~~
rect.cpp:150:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0, j=0; i<VT.size(); i++)
~^~~~~~~~~~
rect.cpp:152:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; j<V.size() && V[j]<VT[i].first; j++);
~^~~~~~~~~
rect.cpp:153:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(j<V.size() && VT[i].first==V[j]) posV[VT[i].second/3000][VT[i].second%3000]=j;
~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
1276 KB |
Output is correct |
3 |
Correct |
3 ms |
1276 KB |
Output is correct |
4 |
Correct |
3 ms |
1272 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1272 KB |
Output is correct |
7 |
Correct |
3 ms |
1272 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
1272 KB |
Output is correct |
10 |
Correct |
3 ms |
1272 KB |
Output is correct |
11 |
Correct |
3 ms |
1272 KB |
Output is correct |
12 |
Correct |
3 ms |
1400 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
9 ms |
1272 KB |
Output is correct |
20 |
Correct |
3 ms |
888 KB |
Output is correct |
21 |
Correct |
2 ms |
476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
1276 KB |
Output is correct |
3 |
Correct |
3 ms |
1276 KB |
Output is correct |
4 |
Correct |
3 ms |
1272 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1272 KB |
Output is correct |
7 |
Correct |
3 ms |
1272 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
1272 KB |
Output is correct |
10 |
Correct |
3 ms |
1272 KB |
Output is correct |
11 |
Correct |
3 ms |
1272 KB |
Output is correct |
12 |
Correct |
3 ms |
1400 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
7 ms |
3320 KB |
Output is correct |
18 |
Correct |
7 ms |
3320 KB |
Output is correct |
19 |
Correct |
7 ms |
3320 KB |
Output is correct |
20 |
Correct |
7 ms |
3064 KB |
Output is correct |
21 |
Correct |
7 ms |
3236 KB |
Output is correct |
22 |
Correct |
8 ms |
3320 KB |
Output is correct |
23 |
Correct |
7 ms |
3192 KB |
Output is correct |
24 |
Correct |
3 ms |
2936 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
9 ms |
1272 KB |
Output is correct |
28 |
Correct |
3 ms |
888 KB |
Output is correct |
29 |
Correct |
2 ms |
476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
1276 KB |
Output is correct |
3 |
Correct |
3 ms |
1276 KB |
Output is correct |
4 |
Correct |
3 ms |
1272 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1272 KB |
Output is correct |
7 |
Correct |
3 ms |
1272 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
1272 KB |
Output is correct |
10 |
Correct |
3 ms |
1272 KB |
Output is correct |
11 |
Correct |
3 ms |
1272 KB |
Output is correct |
12 |
Correct |
3 ms |
1400 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
7 ms |
3320 KB |
Output is correct |
18 |
Correct |
7 ms |
3320 KB |
Output is correct |
19 |
Correct |
7 ms |
3320 KB |
Output is correct |
20 |
Correct |
7 ms |
3064 KB |
Output is correct |
21 |
Correct |
7 ms |
3236 KB |
Output is correct |
22 |
Correct |
8 ms |
3320 KB |
Output is correct |
23 |
Correct |
7 ms |
3192 KB |
Output is correct |
24 |
Correct |
3 ms |
2936 KB |
Output is correct |
25 |
Correct |
29 ms |
10144 KB |
Output is correct |
26 |
Correct |
30 ms |
10176 KB |
Output is correct |
27 |
Correct |
29 ms |
10164 KB |
Output is correct |
28 |
Correct |
25 ms |
8840 KB |
Output is correct |
29 |
Correct |
32 ms |
9636 KB |
Output is correct |
30 |
Correct |
33 ms |
9668 KB |
Output is correct |
31 |
Correct |
33 ms |
9704 KB |
Output is correct |
32 |
Correct |
31 ms |
9576 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
2 ms |
376 KB |
Output is correct |
35 |
Correct |
9 ms |
1272 KB |
Output is correct |
36 |
Correct |
3 ms |
888 KB |
Output is correct |
37 |
Correct |
2 ms |
476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
1276 KB |
Output is correct |
3 |
Correct |
3 ms |
1276 KB |
Output is correct |
4 |
Correct |
3 ms |
1272 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1272 KB |
Output is correct |
7 |
Correct |
3 ms |
1272 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
1272 KB |
Output is correct |
10 |
Correct |
3 ms |
1272 KB |
Output is correct |
11 |
Correct |
3 ms |
1272 KB |
Output is correct |
12 |
Correct |
3 ms |
1400 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
7 ms |
3320 KB |
Output is correct |
18 |
Correct |
7 ms |
3320 KB |
Output is correct |
19 |
Correct |
7 ms |
3320 KB |
Output is correct |
20 |
Correct |
7 ms |
3064 KB |
Output is correct |
21 |
Correct |
7 ms |
3236 KB |
Output is correct |
22 |
Correct |
8 ms |
3320 KB |
Output is correct |
23 |
Correct |
7 ms |
3192 KB |
Output is correct |
24 |
Correct |
3 ms |
2936 KB |
Output is correct |
25 |
Correct |
29 ms |
10144 KB |
Output is correct |
26 |
Correct |
30 ms |
10176 KB |
Output is correct |
27 |
Correct |
29 ms |
10164 KB |
Output is correct |
28 |
Correct |
25 ms |
8840 KB |
Output is correct |
29 |
Correct |
32 ms |
9636 KB |
Output is correct |
30 |
Correct |
33 ms |
9668 KB |
Output is correct |
31 |
Correct |
33 ms |
9704 KB |
Output is correct |
32 |
Correct |
31 ms |
9576 KB |
Output is correct |
33 |
Correct |
114 ms |
39400 KB |
Output is correct |
34 |
Correct |
134 ms |
39384 KB |
Output is correct |
35 |
Correct |
116 ms |
39392 KB |
Output is correct |
36 |
Correct |
117 ms |
39344 KB |
Output is correct |
37 |
Correct |
330 ms |
69240 KB |
Output is correct |
38 |
Correct |
332 ms |
69028 KB |
Output is correct |
39 |
Correct |
360 ms |
69368 KB |
Output is correct |
40 |
Correct |
346 ms |
64932 KB |
Output is correct |
41 |
Correct |
216 ms |
48320 KB |
Output is correct |
42 |
Correct |
260 ms |
56732 KB |
Output is correct |
43 |
Correct |
370 ms |
64824 KB |
Output is correct |
44 |
Correct |
379 ms |
64972 KB |
Output is correct |
45 |
Correct |
195 ms |
41404 KB |
Output is correct |
46 |
Correct |
182 ms |
31652 KB |
Output is correct |
47 |
Correct |
398 ms |
64392 KB |
Output is correct |
48 |
Correct |
379 ms |
64256 KB |
Output is correct |
49 |
Correct |
2 ms |
376 KB |
Output is correct |
50 |
Correct |
2 ms |
376 KB |
Output is correct |
51 |
Correct |
9 ms |
1272 KB |
Output is correct |
52 |
Correct |
3 ms |
888 KB |
Output is correct |
53 |
Correct |
2 ms |
476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
888 KB |
Output is correct |
2 |
Correct |
3 ms |
888 KB |
Output is correct |
3 |
Correct |
3 ms |
632 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
4 ms |
760 KB |
Output is correct |
6 |
Correct |
4 ms |
760 KB |
Output is correct |
7 |
Correct |
4 ms |
760 KB |
Output is correct |
8 |
Correct |
4 ms |
888 KB |
Output is correct |
9 |
Correct |
4 ms |
760 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
4 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
1341 ms |
189796 KB |
Output is correct |
3 |
Correct |
2799 ms |
360448 KB |
Output is correct |
4 |
Correct |
2804 ms |
362428 KB |
Output is correct |
5 |
Correct |
2793 ms |
362320 KB |
Output is correct |
6 |
Correct |
313 ms |
85388 KB |
Output is correct |
7 |
Correct |
685 ms |
169168 KB |
Output is correct |
8 |
Correct |
720 ms |
172408 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
9 ms |
1272 KB |
Output is correct |
12 |
Correct |
3 ms |
888 KB |
Output is correct |
13 |
Correct |
2 ms |
476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
1276 KB |
Output is correct |
3 |
Correct |
3 ms |
1276 KB |
Output is correct |
4 |
Correct |
3 ms |
1272 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1272 KB |
Output is correct |
7 |
Correct |
3 ms |
1272 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
1272 KB |
Output is correct |
10 |
Correct |
3 ms |
1272 KB |
Output is correct |
11 |
Correct |
3 ms |
1272 KB |
Output is correct |
12 |
Correct |
3 ms |
1400 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
7 ms |
3320 KB |
Output is correct |
18 |
Correct |
7 ms |
3320 KB |
Output is correct |
19 |
Correct |
7 ms |
3320 KB |
Output is correct |
20 |
Correct |
7 ms |
3064 KB |
Output is correct |
21 |
Correct |
7 ms |
3236 KB |
Output is correct |
22 |
Correct |
8 ms |
3320 KB |
Output is correct |
23 |
Correct |
7 ms |
3192 KB |
Output is correct |
24 |
Correct |
3 ms |
2936 KB |
Output is correct |
25 |
Correct |
29 ms |
10144 KB |
Output is correct |
26 |
Correct |
30 ms |
10176 KB |
Output is correct |
27 |
Correct |
29 ms |
10164 KB |
Output is correct |
28 |
Correct |
25 ms |
8840 KB |
Output is correct |
29 |
Correct |
32 ms |
9636 KB |
Output is correct |
30 |
Correct |
33 ms |
9668 KB |
Output is correct |
31 |
Correct |
33 ms |
9704 KB |
Output is correct |
32 |
Correct |
31 ms |
9576 KB |
Output is correct |
33 |
Correct |
114 ms |
39400 KB |
Output is correct |
34 |
Correct |
134 ms |
39384 KB |
Output is correct |
35 |
Correct |
116 ms |
39392 KB |
Output is correct |
36 |
Correct |
117 ms |
39344 KB |
Output is correct |
37 |
Correct |
330 ms |
69240 KB |
Output is correct |
38 |
Correct |
332 ms |
69028 KB |
Output is correct |
39 |
Correct |
360 ms |
69368 KB |
Output is correct |
40 |
Correct |
346 ms |
64932 KB |
Output is correct |
41 |
Correct |
216 ms |
48320 KB |
Output is correct |
42 |
Correct |
260 ms |
56732 KB |
Output is correct |
43 |
Correct |
370 ms |
64824 KB |
Output is correct |
44 |
Correct |
379 ms |
64972 KB |
Output is correct |
45 |
Correct |
195 ms |
41404 KB |
Output is correct |
46 |
Correct |
182 ms |
31652 KB |
Output is correct |
47 |
Correct |
398 ms |
64392 KB |
Output is correct |
48 |
Correct |
379 ms |
64256 KB |
Output is correct |
49 |
Correct |
4 ms |
888 KB |
Output is correct |
50 |
Correct |
3 ms |
888 KB |
Output is correct |
51 |
Correct |
3 ms |
632 KB |
Output is correct |
52 |
Correct |
2 ms |
376 KB |
Output is correct |
53 |
Correct |
4 ms |
760 KB |
Output is correct |
54 |
Correct |
4 ms |
760 KB |
Output is correct |
55 |
Correct |
4 ms |
760 KB |
Output is correct |
56 |
Correct |
4 ms |
888 KB |
Output is correct |
57 |
Correct |
4 ms |
760 KB |
Output is correct |
58 |
Correct |
2 ms |
504 KB |
Output is correct |
59 |
Correct |
4 ms |
632 KB |
Output is correct |
60 |
Correct |
3 ms |
632 KB |
Output is correct |
61 |
Correct |
1341 ms |
189796 KB |
Output is correct |
62 |
Correct |
2799 ms |
360448 KB |
Output is correct |
63 |
Correct |
2804 ms |
362428 KB |
Output is correct |
64 |
Correct |
2793 ms |
362320 KB |
Output is correct |
65 |
Correct |
313 ms |
85388 KB |
Output is correct |
66 |
Correct |
685 ms |
169168 KB |
Output is correct |
67 |
Correct |
720 ms |
172408 KB |
Output is correct |
68 |
Correct |
1567 ms |
265828 KB |
Output is correct |
69 |
Correct |
2155 ms |
265816 KB |
Output is correct |
70 |
Correct |
1567 ms |
265916 KB |
Output is correct |
71 |
Correct |
1569 ms |
265840 KB |
Output is correct |
72 |
Correct |
4799 ms |
613132 KB |
Output is correct |
73 |
Correct |
3015 ms |
333200 KB |
Output is correct |
74 |
Correct |
3336 ms |
413632 KB |
Output is correct |
75 |
Execution timed out |
5049 ms |
561116 KB |
Time limit exceeded |
76 |
Halted |
0 ms |
0 KB |
- |