#include "rect.h"
#include<bits/stdc++.h>
using namespace std;
// #define ll long long
#define pb push_back
#define ff first
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ss second
#define int short
const int N = 2600, K = 20;
long long count_rectangles(std::vector<std::vector<signed> > a) {
int n = a.size();
int m = a[0].size();
if(n < 3 || m < 3){
return 0;
}
long long ans = 0;
vector<vector<int>> L, R, D, U;
L.resize(n, vector<int>(m));
R.resize(n, vector<int>(m));
D.resize(n, vector<int>(m));
U.resize(n, vector<int>(m));
for(int i = 0; i < n; ++i){
vector<int> q;
for(int j = 0; j < m; ++j){
while(!q.empty() && a[i][q.back()] <= a[i][j]) q.pop_back();
if(q.empty()){
L[i][j] = -1;
}else{
L[i][j] = q.back();
}
q.pb(j);
}
q.clear();
for(int j = m - 1; j >= 0; --j){
while(!q.empty() && a[i][q.back()] <= a[i][j]) q.pop_back();
if(q.empty()){
R[i][j] = m;
}else{
R[i][j] = q.back();
}
q.pb(j);
}
}
for(int j = 0; j < m; ++j){
vector<int> q;
for(int i = 0; i < n; ++i){
while(!q.empty() && a[q.back()][j] <= a[i][j]) q.pop_back();
if(q.empty()){
U[i][j] = -1;
}else{
U[i][j] = q.back();
}
q.pb(i);
}
q.clear();
for(int i = n - 1; i >= 0; --i){
while(!q.empty() && a[q.back()][j] <= a[i][j]) q.pop_back();
if(q.empty()){
D[i][j] = n;
}else{
D[i][j] = q.back();
}
q.pb(i);
}
}
vector<vector<vector<int>>> rangesL(n, vector<vector<int>>(m));
vector<vector<vector<int>>> rangesU(n, vector<vector<int>>(m));
for(int i = 1; i + 1 < n; ++i){
for(int j = 1; j + 1 < m; ++j){
int l = L[i][j], r = R[i][j];
while(l > -1 && r < m){
// cout << i << ' ' << j << ' ' << l + 1 << ' ' << r - 1 << '\n';
rangesL[i][r - 1].pb(l + 1);
if(a[i][l] < a[i][r]){
l = L[i][l];
}else if(a[i][l] > a[i][r]){
r = R[i][r];
}else{
l = L[i][l];
r = R[i][r];
}
}
}
}
for(int j = 1; j + 1 < m; ++j){
for(int i = 1; i + 1 < n; ++i){
int l = U[i][j], r = D[i][j];
while(l > -1 && r < n){
// cout << l << ' ' << r << ' ' << i << ' ' << j << '\n';
rangesU[r - 1][j].pb(l + 1);
if(a[l][j] < a[r][j]){
l = U[l][j];
}else if(a[l][j] > a[r][j]){
r = D[r][j];
}else{
l = U[l][j];
r = D[r][j];
}
}
}
}
// for(int i = 0; i < n; ++i){
// for(int j = 0; j < m; ++j) cout << L[i][j] << ' ';en;
// }
// en;en;
// for(int i = 0; i < n; ++i){
// for(int j = 0; j < m; ++j) cout << R[i][j] << ' ';en;
// }
// en;en;
// for(int i = 0; i < n; ++i){
// for(int j = 0; j < m; ++j) cout << U[i][j] << ' ';en;
// }
// en;en;
// for(int i = 0; i < n; ++i){
// for(int j = 0; j < m; ++j) cout << D[i][j] << ' ';en;
// }
vector<vector<vector<int>>> colranges(max(n,m), vector<vector<int>>(max(n,m)));
vector<vector<vector<int>>> rowranges(max(n,m), vector<vector<int>>(max(n,m)));
// cout << "wtf" << endl;
for(int i = 1; i + 1 < n; ++i){
for(int j = 1; j + 1 < m; ++j){
sort(all(rangesL[i][j]), greater<int>());
rangesL[i][j].erase(unique(all(rangesL[i][j])), rangesL[i][j].end());
sort(all(rangesU[i][j]), greater<int>());
rangesU[i][j].erase(unique(all(rangesU[i][j])), rangesU[i][j].end());
int ls = rangesL[i][j].size();
int us = rangesU[i][j].size();
// cout << i << ' ' << j << '\n';
// cout << "L: ";
for(int x = 0; x < ls; ++x){
colranges[j][rangesL[i][j][x]].pb(i);
// cout << rangesL[i][j][x] << ' ';
}
// cout << "R: ";
for(int x = 0; x < us; ++x){
rowranges[i][rangesU[i][j][x]].pb(j);
// cout << rangesU[i][j][x] << ' ';
}
// en;en;
for(int x = 0; x < ls; ++x){
int l = rangesL[i][j][x];
for(int y = 0; y < us; ++y){
int u = rangesU[i][j][y];
// cout << i << ' ' << j << ' ' << u << ' ' << l << '\n';
// cout << (rowranges[i][u].size() >= j - l + 1 && rowranges[i][u][int(rowranges[i][u].size()) - (j - l + 1)] == l) << ' ' <<
// (colranges[j][l].size() >= i - u + 1 && colranges[j][l][int(colranges[j][l].size()) - (i - u + 1)] == u) << '\n';
if(rowranges[i][u].size() >= j - l + 1 && rowranges[i][u][int(rowranges[i][u].size()) - (j - l + 1)] == l
&& colranges[j][l].size() >= i - u + 1 && colranges[j][l][int(colranges[j][l].size()) - (i - u + 1)] == u){
++ans;
}
}
}
}
}
return ans;
}
Compilation message
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:163:32: warning: comparison of integer expressions of different signedness: 'std::vector<short int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
163 | if(rowranges[i][u].size() >= j - l + 1 && rowranges[i][u][int(rowranges[i][u].size()) - (j - l + 1)] == l
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
rect.cpp:164:33: warning: comparison of integer expressions of different signedness: 'std::vector<short int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
164 | && colranges[j][l].size() >= i - u + 1 && colranges[j][l][int(colranges[j][l].size()) - (i - u + 1)] == u){
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
9 ms |
2396 KB |
Output is correct |
23 |
Correct |
8 ms |
2392 KB |
Output is correct |
24 |
Correct |
8 ms |
2428 KB |
Output is correct |
25 |
Correct |
2 ms |
1116 KB |
Output is correct |
26 |
Correct |
4 ms |
1368 KB |
Output is correct |
27 |
Correct |
4 ms |
1372 KB |
Output is correct |
28 |
Correct |
3 ms |
1372 KB |
Output is correct |
29 |
Correct |
1 ms |
1008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
9 ms |
2396 KB |
Output is correct |
18 |
Correct |
8 ms |
2392 KB |
Output is correct |
19 |
Correct |
8 ms |
2428 KB |
Output is correct |
20 |
Correct |
2 ms |
1116 KB |
Output is correct |
21 |
Correct |
4 ms |
1368 KB |
Output is correct |
22 |
Correct |
4 ms |
1372 KB |
Output is correct |
23 |
Correct |
3 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
1008 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
229 ms |
21824 KB |
Output is correct |
31 |
Correct |
212 ms |
21840 KB |
Output is correct |
32 |
Correct |
204 ms |
21840 KB |
Output is correct |
33 |
Correct |
7 ms |
5724 KB |
Output is correct |
34 |
Correct |
18 ms |
7192 KB |
Output is correct |
35 |
Correct |
21 ms |
7256 KB |
Output is correct |
36 |
Correct |
17 ms |
7000 KB |
Output is correct |
37 |
Correct |
17 ms |
7000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
9 ms |
2396 KB |
Output is correct |
18 |
Correct |
8 ms |
2392 KB |
Output is correct |
19 |
Correct |
8 ms |
2428 KB |
Output is correct |
20 |
Correct |
2 ms |
1116 KB |
Output is correct |
21 |
Correct |
4 ms |
1368 KB |
Output is correct |
22 |
Correct |
4 ms |
1372 KB |
Output is correct |
23 |
Correct |
3 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
1008 KB |
Output is correct |
25 |
Correct |
229 ms |
21824 KB |
Output is correct |
26 |
Correct |
212 ms |
21840 KB |
Output is correct |
27 |
Correct |
204 ms |
21840 KB |
Output is correct |
28 |
Correct |
7 ms |
5724 KB |
Output is correct |
29 |
Correct |
18 ms |
7192 KB |
Output is correct |
30 |
Correct |
21 ms |
7256 KB |
Output is correct |
31 |
Correct |
17 ms |
7000 KB |
Output is correct |
32 |
Correct |
17 ms |
7000 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
3376 ms |
295912 KB |
Output is correct |
39 |
Correct |
2437 ms |
349112 KB |
Output is correct |
40 |
Correct |
2527 ms |
349064 KB |
Output is correct |
41 |
Correct |
1591 ms |
401492 KB |
Output is correct |
42 |
Execution timed out |
5041 ms |
730368 KB |
Time limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
302396 KB |
Output is correct |
2 |
Correct |
152 ms |
217772 KB |
Output is correct |
3 |
Correct |
108 ms |
294600 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
108 ms |
294668 KB |
Output is correct |
6 |
Correct |
134 ms |
294740 KB |
Output is correct |
7 |
Correct |
111 ms |
294776 KB |
Output is correct |
8 |
Correct |
139 ms |
294564 KB |
Output is correct |
9 |
Correct |
135 ms |
294712 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
555 ms |
483228 KB |
Output is correct |
8 |
Correct |
1125 ms |
792148 KB |
Output is correct |
9 |
Correct |
1176 ms |
794576 KB |
Output is correct |
10 |
Correct |
1152 ms |
794780 KB |
Output is correct |
11 |
Correct |
288 ms |
487504 KB |
Output is correct |
12 |
Correct |
455 ms |
661840 KB |
Output is correct |
13 |
Correct |
492 ms |
686164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
9 ms |
2396 KB |
Output is correct |
18 |
Correct |
8 ms |
2392 KB |
Output is correct |
19 |
Correct |
8 ms |
2428 KB |
Output is correct |
20 |
Correct |
2 ms |
1116 KB |
Output is correct |
21 |
Correct |
4 ms |
1368 KB |
Output is correct |
22 |
Correct |
4 ms |
1372 KB |
Output is correct |
23 |
Correct |
3 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
1008 KB |
Output is correct |
25 |
Correct |
229 ms |
21824 KB |
Output is correct |
26 |
Correct |
212 ms |
21840 KB |
Output is correct |
27 |
Correct |
204 ms |
21840 KB |
Output is correct |
28 |
Correct |
7 ms |
5724 KB |
Output is correct |
29 |
Correct |
18 ms |
7192 KB |
Output is correct |
30 |
Correct |
21 ms |
7256 KB |
Output is correct |
31 |
Correct |
17 ms |
7000 KB |
Output is correct |
32 |
Correct |
17 ms |
7000 KB |
Output is correct |
33 |
Correct |
3376 ms |
295912 KB |
Output is correct |
34 |
Correct |
2437 ms |
349112 KB |
Output is correct |
35 |
Correct |
2527 ms |
349064 KB |
Output is correct |
36 |
Correct |
1591 ms |
401492 KB |
Output is correct |
37 |
Execution timed out |
5041 ms |
730368 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |