#include <bits/stdc++.h>
#include "rect.h"
#define eb emplace_back
#define hsh(a, b, c) ((a)*6250000ll+(b)*2500ll+c)
using namespace std;
using ll = long long;
int N, M;
vector<ll> H, V;
long long count_rectangles(vector<vector<int>> a) {
N=a.size(); M=a[0].size();
vector<vector<int>> l, r, u, d;
for (int i=0; i<N; i++)
l.eb(vector<int>(M, -1));
r=u=d=l;
for (int i=1; i<N-1; i++) {
vector<int> st;
for (int j=0; j<M; j++) {
while (st.size()&&a[i][st.back()]<a[i][j]) st.pop_back();
if (st.size()&&st.back()<j-1) H.eb(hsh(st.back(), j, i));
st.eb(j);
}
st.clear();
for (int j=M-1; j>=0; j--) {
while (st.size()&&a[i][st.back()]<a[i][j]) st.pop_back();
if (st.size()&&st.back()>j+1) H.eb(hsh(j, st.back(), i));
st.eb(j);
}
st.clear();
for (int j=0; j<M; j++) {
while (st.size()&&a[i][st.back()]<=a[i][j]) st.pop_back();
if (st.size()) l[i][j]=st.back();
st.eb(j);
}
st.clear();
for (int j=M-1; j>=0; j--) {
while (st.size()&&a[i][st.back()]<=a[i][j]) st.pop_back();
if (st.size()) r[i][j]=st.back();
st.eb(j);
}
}
for (int j=1; j<M-1; j++) {
vector<int> st;
for (int i=0; i<N; i++) {
while (st.size()&&a[st.back()][j]<a[i][j]) st.pop_back();
if (st.size()&&st.back()<i-1) V.eb(hsh(st.back(), i, j));
st.eb(i);
}
st.clear();
for (int i=N-1; i>=0; i--) {
while (st.size()&&a[st.back()][j]<a[i][j]) st.pop_back();
if (st.size()&&st.back()>i+1) V.eb(hsh(i, st.back(), j));
st.eb(i);
}
st.clear();
for (int i=0; i<N; i++) {
while (st.size()&&a[st.back()][j]<=a[i][j]) st.pop_back();
if (st.size()) u[i][j]=st.back();
st.eb(i);
}
st.clear();
for (int i=N-1; i>=0; i--) {
while (st.size()&&a[st.back()][j]<=a[i][j]) st.pop_back();
if (st.size()) d[i][j]=st.back();
st.eb(i);
}
}
sort(H.begin(), H.end());
H.resize(unique(H.begin(), H.end())-H.begin());
sort(V.begin(), V.end());
V.resize(unique(V.begin(), V.end())-V.begin());
vector<tuple<int, int, int, int>> A;
for (int i=1; i<N-1; i++) for (int j=1; j<M-1; j++) {
int L=l[i][j], R=r[i][j], U=u[i][j], D=d[i][j];
if (L==-1||R==-1||U==-1||D==-1) continue;
int b1=lower_bound(H.begin(), H.end(), hsh(L, R, U+1))-H.begin();
if (b1>=H.size()||hsh(L, R, U+1)!=H[b1]) continue;
b1+=D-U-2;
if (b1>=H.size()||hsh(L, R, D-1)!=H[b1]) continue;
b1=lower_bound(V.begin(), V.end(), hsh(U, D, L+1))-V.begin();
if (b1>=V.size()||hsh(U, D, L+1)!=V[b1]) continue;
b1+=R-L-2;
if (b1>=V.size()||hsh(U, D, R-1)!=V[b1]) continue;
A.eb(L, R, U, D);
}
sort(A.begin(), A.end());
return unique(A.begin(), A.end())-A.begin();
}
Compilation message
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:84:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | if (b1>=H.size()||hsh(L, R, U+1)!=H[b1]) continue;
| ~~^~~~~~~~~~
rect.cpp:86:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | if (b1>=H.size()||hsh(L, R, D-1)!=H[b1]) continue;
| ~~^~~~~~~~~~
rect.cpp:89:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | if (b1>=V.size()||hsh(U, D, L+1)!=V[b1]) continue;
| ~~^~~~~~~~~~
rect.cpp:91:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | if (b1>=V.size()||hsh(U, D, R-1)!=V[b1]) continue;
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
416 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
416 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
4 ms |
896 KB |
Output is correct |
18 |
Correct |
4 ms |
768 KB |
Output is correct |
19 |
Correct |
4 ms |
896 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
4 ms |
640 KB |
Output is correct |
23 |
Correct |
4 ms |
640 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
0 ms |
256 KB |
Output is correct |
26 |
Correct |
0 ms |
256 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
0 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
416 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
4 ms |
896 KB |
Output is correct |
18 |
Correct |
4 ms |
768 KB |
Output is correct |
19 |
Correct |
4 ms |
896 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
4 ms |
640 KB |
Output is correct |
23 |
Correct |
4 ms |
640 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
22 ms |
3196 KB |
Output is correct |
26 |
Correct |
22 ms |
3196 KB |
Output is correct |
27 |
Correct |
22 ms |
3188 KB |
Output is correct |
28 |
Correct |
15 ms |
1920 KB |
Output is correct |
29 |
Correct |
22 ms |
2428 KB |
Output is correct |
30 |
Correct |
23 ms |
2576 KB |
Output is correct |
31 |
Correct |
22 ms |
2804 KB |
Output is correct |
32 |
Correct |
21 ms |
2424 KB |
Output is correct |
33 |
Correct |
0 ms |
256 KB |
Output is correct |
34 |
Correct |
0 ms |
256 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
0 ms |
384 KB |
Output is correct |
37 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
416 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
4 ms |
896 KB |
Output is correct |
18 |
Correct |
4 ms |
768 KB |
Output is correct |
19 |
Correct |
4 ms |
896 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
4 ms |
640 KB |
Output is correct |
23 |
Correct |
4 ms |
640 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
22 ms |
3196 KB |
Output is correct |
26 |
Correct |
22 ms |
3196 KB |
Output is correct |
27 |
Correct |
22 ms |
3188 KB |
Output is correct |
28 |
Correct |
15 ms |
1920 KB |
Output is correct |
29 |
Correct |
22 ms |
2428 KB |
Output is correct |
30 |
Correct |
23 ms |
2576 KB |
Output is correct |
31 |
Correct |
22 ms |
2804 KB |
Output is correct |
32 |
Correct |
21 ms |
2424 KB |
Output is correct |
33 |
Correct |
99 ms |
17376 KB |
Output is correct |
34 |
Correct |
98 ms |
17376 KB |
Output is correct |
35 |
Correct |
108 ms |
17248 KB |
Output is correct |
36 |
Correct |
75 ms |
17252 KB |
Output is correct |
37 |
Correct |
324 ms |
28304 KB |
Output is correct |
38 |
Correct |
322 ms |
28380 KB |
Output is correct |
39 |
Correct |
318 ms |
28236 KB |
Output is correct |
40 |
Correct |
299 ms |
27100 KB |
Output is correct |
41 |
Correct |
163 ms |
17124 KB |
Output is correct |
42 |
Correct |
197 ms |
20880 KB |
Output is correct |
43 |
Correct |
318 ms |
26068 KB |
Output is correct |
44 |
Correct |
315 ms |
26068 KB |
Output is correct |
45 |
Correct |
149 ms |
13432 KB |
Output is correct |
46 |
Correct |
145 ms |
13256 KB |
Output is correct |
47 |
Correct |
307 ms |
25304 KB |
Output is correct |
48 |
Correct |
315 ms |
25432 KB |
Output is correct |
49 |
Correct |
0 ms |
256 KB |
Output is correct |
50 |
Correct |
0 ms |
256 KB |
Output is correct |
51 |
Correct |
1 ms |
384 KB |
Output is correct |
52 |
Correct |
0 ms |
384 KB |
Output is correct |
53 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
640 KB |
Output is correct |
2 |
Correct |
2 ms |
640 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1147 ms |
104064 KB |
Output is correct |
3 |
Correct |
2694 ms |
221088 KB |
Output is correct |
4 |
Correct |
2673 ms |
221756 KB |
Output is correct |
5 |
Correct |
2689 ms |
221720 KB |
Output is correct |
6 |
Correct |
410 ms |
73080 KB |
Output is correct |
7 |
Correct |
890 ms |
138488 KB |
Output is correct |
8 |
Correct |
938 ms |
147832 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
416 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
4 ms |
896 KB |
Output is correct |
18 |
Correct |
4 ms |
768 KB |
Output is correct |
19 |
Correct |
4 ms |
896 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
4 ms |
640 KB |
Output is correct |
23 |
Correct |
4 ms |
640 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
22 ms |
3196 KB |
Output is correct |
26 |
Correct |
22 ms |
3196 KB |
Output is correct |
27 |
Correct |
22 ms |
3188 KB |
Output is correct |
28 |
Correct |
15 ms |
1920 KB |
Output is correct |
29 |
Correct |
22 ms |
2428 KB |
Output is correct |
30 |
Correct |
23 ms |
2576 KB |
Output is correct |
31 |
Correct |
22 ms |
2804 KB |
Output is correct |
32 |
Correct |
21 ms |
2424 KB |
Output is correct |
33 |
Correct |
99 ms |
17376 KB |
Output is correct |
34 |
Correct |
98 ms |
17376 KB |
Output is correct |
35 |
Correct |
108 ms |
17248 KB |
Output is correct |
36 |
Correct |
75 ms |
17252 KB |
Output is correct |
37 |
Correct |
324 ms |
28304 KB |
Output is correct |
38 |
Correct |
322 ms |
28380 KB |
Output is correct |
39 |
Correct |
318 ms |
28236 KB |
Output is correct |
40 |
Correct |
299 ms |
27100 KB |
Output is correct |
41 |
Correct |
163 ms |
17124 KB |
Output is correct |
42 |
Correct |
197 ms |
20880 KB |
Output is correct |
43 |
Correct |
318 ms |
26068 KB |
Output is correct |
44 |
Correct |
315 ms |
26068 KB |
Output is correct |
45 |
Correct |
149 ms |
13432 KB |
Output is correct |
46 |
Correct |
145 ms |
13256 KB |
Output is correct |
47 |
Correct |
307 ms |
25304 KB |
Output is correct |
48 |
Correct |
315 ms |
25432 KB |
Output is correct |
49 |
Correct |
2 ms |
640 KB |
Output is correct |
50 |
Correct |
2 ms |
640 KB |
Output is correct |
51 |
Correct |
1 ms |
512 KB |
Output is correct |
52 |
Correct |
0 ms |
256 KB |
Output is correct |
53 |
Correct |
2 ms |
640 KB |
Output is correct |
54 |
Correct |
2 ms |
640 KB |
Output is correct |
55 |
Correct |
2 ms |
640 KB |
Output is correct |
56 |
Correct |
2 ms |
640 KB |
Output is correct |
57 |
Correct |
2 ms |
640 KB |
Output is correct |
58 |
Correct |
1 ms |
384 KB |
Output is correct |
59 |
Correct |
1 ms |
512 KB |
Output is correct |
60 |
Correct |
1 ms |
256 KB |
Output is correct |
61 |
Correct |
1147 ms |
104064 KB |
Output is correct |
62 |
Correct |
2694 ms |
221088 KB |
Output is correct |
63 |
Correct |
2673 ms |
221756 KB |
Output is correct |
64 |
Correct |
2689 ms |
221720 KB |
Output is correct |
65 |
Correct |
410 ms |
73080 KB |
Output is correct |
66 |
Correct |
890 ms |
138488 KB |
Output is correct |
67 |
Correct |
938 ms |
147832 KB |
Output is correct |
68 |
Correct |
1719 ms |
221644 KB |
Output is correct |
69 |
Correct |
1615 ms |
221456 KB |
Output is correct |
70 |
Correct |
1614 ms |
221688 KB |
Output is correct |
71 |
Correct |
1405 ms |
221640 KB |
Output is correct |
72 |
Execution timed out |
5076 ms |
376968 KB |
Time limit exceeded |
73 |
Halted |
0 ms |
0 KB |
- |