#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
int n, m;
int bit[2505];
int a[2][2505][2505], lst[2][2505][2505], cnt[2][2505][2505];
vector<pair<int, int> > p[2];
vector<int> s[2][2505];
void upd(int idx, int val){
for(idx++; idx < 2505; idx += (idx & -idx)) bit[idx] += val;
}
int qry(int idx){
int ans = 0;
for(idx++; idx; idx -= (idx & -idx)) ans += bit[idx];
return ans;
}
long long count_rectangles(vector<vector<int>> A) {
n = A.size(), m = A[0].size();
for(int i = 0; i < n; i++) s[0][i].push_back(0);
for(int i = 0; i < m; i++) s[1][i].push_back(0);
long long ans = 0;
for(int i = 0; i < n - 1; i++){
for(int j = 0; j < m - 1; j++){
a[0][i][j] = a[1][j][i] = A[i][j];
a[0][i][j + 1] = A[i][j + 1];
a[1][j][i + 1] = A[i + 1][j];
for(int t = 0; t < 2; t++){
bool eq = 0;
p[t].clear();
while(!s[t][i].empty()){
int c = s[t][i].back();
if(c < j && !eq){
if(lst[t][c][j + 1] != i - 1) cnt[t][c][j + 1] = 0;
cnt[t][c][j + 1]++, lst[t][c][j + 1] = i;
p[t].push_back({j - c, cnt[t][c][j + 1]});
if(t) swap(p[t].back().first, p[t].back().second);
}
eq = a[t][i][c] == a[t][i][j + 1];
if(a[t][i][c] <= a[t][i][j + 1]) s[t][i].pop_back();
else break;
}
s[t][i].push_back(j + 1);
swap(i, j);
sort(p[t].begin(), p[t].end(), greater<pair<int, int> >());
}
int it = 0;
for(pair<int, int> v : p[0]){
for(; it < p[1].size() && v.first <= p[1][it].first; it++) upd(p[1][it].second, 1);
ans += qry(v.second);
}
for(int jt = 0; jt < it; jt++) upd(p[1][jt].second, -1);
}
}
return ans;
}
Compilation message
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:52:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(; it < p[1].size() && v.first <= p[1][it].first; it++) upd(p[1][it].second, 1);
| ~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
1 ms |
7004 KB |
Output is correct |
4 |
Correct |
1 ms |
7004 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
1 ms |
9020 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
11096 KB |
Output is correct |
10 |
Correct |
2 ms |
11096 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
2 ms |
9052 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4656 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
11100 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
1 ms |
7004 KB |
Output is correct |
4 |
Correct |
1 ms |
7004 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
1 ms |
9020 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
11096 KB |
Output is correct |
10 |
Correct |
2 ms |
11096 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
2 ms |
9052 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4656 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
11100 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
11868 KB |
Output is correct |
23 |
Correct |
2 ms |
11868 KB |
Output is correct |
24 |
Correct |
2 ms |
11868 KB |
Output is correct |
25 |
Correct |
2 ms |
15452 KB |
Output is correct |
26 |
Correct |
3 ms |
15460 KB |
Output is correct |
27 |
Correct |
3 ms |
15708 KB |
Output is correct |
28 |
Correct |
3 ms |
13624 KB |
Output is correct |
29 |
Correct |
2 ms |
13404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
1 ms |
7004 KB |
Output is correct |
4 |
Correct |
1 ms |
7004 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
1 ms |
9020 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
11096 KB |
Output is correct |
10 |
Correct |
2 ms |
11096 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
2 ms |
9052 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4656 KB |
Output is correct |
17 |
Correct |
2 ms |
11868 KB |
Output is correct |
18 |
Correct |
2 ms |
11868 KB |
Output is correct |
19 |
Correct |
2 ms |
11868 KB |
Output is correct |
20 |
Correct |
2 ms |
15452 KB |
Output is correct |
21 |
Correct |
3 ms |
15460 KB |
Output is correct |
22 |
Correct |
3 ms |
15708 KB |
Output is correct |
23 |
Correct |
3 ms |
13624 KB |
Output is correct |
24 |
Correct |
2 ms |
13404 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
11100 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
6 ms |
14168 KB |
Output is correct |
31 |
Correct |
6 ms |
14172 KB |
Output is correct |
32 |
Correct |
6 ms |
14052 KB |
Output is correct |
33 |
Correct |
5 ms |
16956 KB |
Output is correct |
34 |
Correct |
7 ms |
15748 KB |
Output is correct |
35 |
Correct |
7 ms |
17244 KB |
Output is correct |
36 |
Correct |
7 ms |
15964 KB |
Output is correct |
37 |
Correct |
7 ms |
17244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
1 ms |
7004 KB |
Output is correct |
4 |
Correct |
1 ms |
7004 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
1 ms |
9020 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
11096 KB |
Output is correct |
10 |
Correct |
2 ms |
11096 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
2 ms |
9052 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4656 KB |
Output is correct |
17 |
Correct |
2 ms |
11868 KB |
Output is correct |
18 |
Correct |
2 ms |
11868 KB |
Output is correct |
19 |
Correct |
2 ms |
11868 KB |
Output is correct |
20 |
Correct |
2 ms |
15452 KB |
Output is correct |
21 |
Correct |
3 ms |
15460 KB |
Output is correct |
22 |
Correct |
3 ms |
15708 KB |
Output is correct |
23 |
Correct |
3 ms |
13624 KB |
Output is correct |
24 |
Correct |
2 ms |
13404 KB |
Output is correct |
25 |
Correct |
6 ms |
14168 KB |
Output is correct |
26 |
Correct |
6 ms |
14172 KB |
Output is correct |
27 |
Correct |
6 ms |
14052 KB |
Output is correct |
28 |
Correct |
5 ms |
16956 KB |
Output is correct |
29 |
Correct |
7 ms |
15748 KB |
Output is correct |
30 |
Correct |
7 ms |
17244 KB |
Output is correct |
31 |
Correct |
7 ms |
15964 KB |
Output is correct |
32 |
Correct |
7 ms |
17244 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
2 ms |
11100 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
48 ms |
53972 KB |
Output is correct |
39 |
Correct |
41 ms |
53840 KB |
Output is correct |
40 |
Correct |
47 ms |
51140 KB |
Output is correct |
41 |
Correct |
42 ms |
53584 KB |
Output is correct |
42 |
Correct |
52 ms |
48908 KB |
Output is correct |
43 |
Correct |
50 ms |
50512 KB |
Output is correct |
44 |
Correct |
65 ms |
50872 KB |
Output is correct |
45 |
Correct |
50 ms |
47956 KB |
Output is correct |
46 |
Correct |
30 ms |
48868 KB |
Output is correct |
47 |
Correct |
36 ms |
48148 KB |
Output is correct |
48 |
Correct |
59 ms |
51208 KB |
Output is correct |
49 |
Correct |
79 ms |
52512 KB |
Output is correct |
50 |
Correct |
34 ms |
36436 KB |
Output is correct |
51 |
Correct |
34 ms |
38732 KB |
Output is correct |
52 |
Correct |
61 ms |
48964 KB |
Output is correct |
53 |
Correct |
65 ms |
53220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
62812 KB |
Output is correct |
2 |
Correct |
11 ms |
56408 KB |
Output is correct |
3 |
Correct |
4 ms |
27996 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
10 ms |
60252 KB |
Output is correct |
6 |
Correct |
9 ms |
59996 KB |
Output is correct |
7 |
Correct |
9 ms |
60252 KB |
Output is correct |
8 |
Correct |
9 ms |
59796 KB |
Output is correct |
9 |
Correct |
9 ms |
59740 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
8 ms |
57692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
11100 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
8792 KB |
Output is correct |
7 |
Correct |
171 ms |
116688 KB |
Output is correct |
8 |
Correct |
353 ms |
171672 KB |
Output is correct |
9 |
Correct |
362 ms |
174544 KB |
Output is correct |
10 |
Correct |
362 ms |
173280 KB |
Output is correct |
11 |
Correct |
103 ms |
68432 KB |
Output is correct |
12 |
Correct |
216 ms |
101904 KB |
Output is correct |
13 |
Correct |
247 ms |
107000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
1 ms |
7004 KB |
Output is correct |
4 |
Correct |
1 ms |
7004 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
1 ms |
9020 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
11096 KB |
Output is correct |
10 |
Correct |
2 ms |
11096 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
2 ms |
9052 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4656 KB |
Output is correct |
17 |
Correct |
2 ms |
11868 KB |
Output is correct |
18 |
Correct |
2 ms |
11868 KB |
Output is correct |
19 |
Correct |
2 ms |
11868 KB |
Output is correct |
20 |
Correct |
2 ms |
15452 KB |
Output is correct |
21 |
Correct |
3 ms |
15460 KB |
Output is correct |
22 |
Correct |
3 ms |
15708 KB |
Output is correct |
23 |
Correct |
3 ms |
13624 KB |
Output is correct |
24 |
Correct |
2 ms |
13404 KB |
Output is correct |
25 |
Correct |
6 ms |
14168 KB |
Output is correct |
26 |
Correct |
6 ms |
14172 KB |
Output is correct |
27 |
Correct |
6 ms |
14052 KB |
Output is correct |
28 |
Correct |
5 ms |
16956 KB |
Output is correct |
29 |
Correct |
7 ms |
15748 KB |
Output is correct |
30 |
Correct |
7 ms |
17244 KB |
Output is correct |
31 |
Correct |
7 ms |
15964 KB |
Output is correct |
32 |
Correct |
7 ms |
17244 KB |
Output is correct |
33 |
Correct |
48 ms |
53972 KB |
Output is correct |
34 |
Correct |
41 ms |
53840 KB |
Output is correct |
35 |
Correct |
47 ms |
51140 KB |
Output is correct |
36 |
Correct |
42 ms |
53584 KB |
Output is correct |
37 |
Correct |
52 ms |
48908 KB |
Output is correct |
38 |
Correct |
50 ms |
50512 KB |
Output is correct |
39 |
Correct |
65 ms |
50872 KB |
Output is correct |
40 |
Correct |
50 ms |
47956 KB |
Output is correct |
41 |
Correct |
30 ms |
48868 KB |
Output is correct |
42 |
Correct |
36 ms |
48148 KB |
Output is correct |
43 |
Correct |
59 ms |
51208 KB |
Output is correct |
44 |
Correct |
79 ms |
52512 KB |
Output is correct |
45 |
Correct |
34 ms |
36436 KB |
Output is correct |
46 |
Correct |
34 ms |
38732 KB |
Output is correct |
47 |
Correct |
61 ms |
48964 KB |
Output is correct |
48 |
Correct |
65 ms |
53220 KB |
Output is correct |
49 |
Correct |
12 ms |
62812 KB |
Output is correct |
50 |
Correct |
11 ms |
56408 KB |
Output is correct |
51 |
Correct |
4 ms |
27996 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
10 ms |
60252 KB |
Output is correct |
54 |
Correct |
9 ms |
59996 KB |
Output is correct |
55 |
Correct |
9 ms |
60252 KB |
Output is correct |
56 |
Correct |
9 ms |
59796 KB |
Output is correct |
57 |
Correct |
9 ms |
59740 KB |
Output is correct |
58 |
Correct |
1 ms |
604 KB |
Output is correct |
59 |
Correct |
8 ms |
57692 KB |
Output is correct |
60 |
Correct |
1 ms |
8792 KB |
Output is correct |
61 |
Correct |
171 ms |
116688 KB |
Output is correct |
62 |
Correct |
353 ms |
171672 KB |
Output is correct |
63 |
Correct |
362 ms |
174544 KB |
Output is correct |
64 |
Correct |
362 ms |
173280 KB |
Output is correct |
65 |
Correct |
103 ms |
68432 KB |
Output is correct |
66 |
Correct |
216 ms |
101904 KB |
Output is correct |
67 |
Correct |
247 ms |
107000 KB |
Output is correct |
68 |
Correct |
0 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
2 ms |
11100 KB |
Output is correct |
71 |
Correct |
1 ms |
4444 KB |
Output is correct |
72 |
Correct |
1 ms |
4444 KB |
Output is correct |
73 |
Correct |
611 ms |
268468 KB |
Output is correct |
74 |
Correct |
612 ms |
269812 KB |
Output is correct |
75 |
Correct |
722 ms |
265204 KB |
Output is correct |
76 |
Correct |
617 ms |
268372 KB |
Output is correct |
77 |
Correct |
791 ms |
274436 KB |
Output is correct |
78 |
Correct |
446 ms |
153840 KB |
Output is correct |
79 |
Correct |
483 ms |
164912 KB |
Output is correct |
80 |
Correct |
730 ms |
202476 KB |
Output is correct |
81 |
Correct |
489 ms |
168788 KB |
Output is correct |
82 |
Correct |
648 ms |
203196 KB |
Output is correct |
83 |
Correct |
828 ms |
232484 KB |
Output is correct |
84 |
Correct |
455 ms |
153148 KB |
Output is correct |
85 |
Correct |
836 ms |
216552 KB |
Output is correct |
86 |
Correct |
815 ms |
212524 KB |
Output is correct |
87 |
Correct |
468 ms |
184420 KB |
Output is correct |
88 |
Correct |
766 ms |
274252 KB |
Output is correct |
89 |
Correct |
794 ms |
274324 KB |
Output is correct |
90 |
Correct |
755 ms |
274356 KB |
Output is correct |