#include "rect.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mk make_pair
#define pb push_back
#define fr first
#define sc second
ll count_rectangles(vector<vector<int>> a) {
int n = sz(a), m = sz(a[0]);
// primeiro eu calculo os pares uteis
vector<int> line[n][m], col[n][m], val_l[n][m], val_c[n][m];
// calculando os pares na linha
for(int i = 0; i < n; i++) {
vector<int> r(m);
for(int j = 0; j < m; j++) {
r[j] = j-1;
// acho o primeiro cara maior ou igual (com indice menor)
while(r[j] >= 0 and a[i][r[j]] < a[i][j]) r[j] = r[r[j]];
if(r[j] >= 0 and r[j]+1 < j) line[i][j].pb(r[j]);
}
for(int j = m-1; j >= 0; j--) {
r[j] = j+1;
// acho o primeiro cara maior ou igual (com indice maior)
while(r[j] < m and a[i][r[j]] < a[i][j]) r[j] = r[r[j]];
if(r[j] < m and j+1 < r[j] and a[i][j] != a[i][r[j]]) line[i][r[j]].pb(j);
}
}
// calculando os pares na coluna
for(int j = 0; j < m; j++) {
vector<int> r(n);
for(int i = 0; i < n; i++) {
r[i] = i-1;
// acho o primeiro cara maior ou igual (com indice menor)
while(r[i] >= 0 and a[r[i]][j] < a[i][j]) r[i] = r[r[i]];
if(r[i] >= 0 and r[i]+1 < i) col[i][j].pb(r[i]);
}
for(int i = n-1; i >= 0; i--) {
r[i] = i+1;
// acho o primeiro cara maior ou igual (com indice maior)
while(r[i] < n and a[r[i]][j] < a[i][j]) r[i] = r[r[i]];
if(r[i] < n and i+1 < r[i] and a[i][j] != a[r[i]][j]) col[r[i]][j].pb(i);
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
sort(all(line[i][j]));
sort(all(col[i][j]));
val_l[i][j].resize(sz(line[i][j]), i);
for(int k = 0, ptr = 0; i != 0 and k < sz(line[i][j]); k++) {
while(ptr < sz(line[i-1][j]) and line[i-1][j][ptr] < line[i][j][k]) ptr++;
if(ptr >= sz(line[i-1][j])) break;
if(line[i-1][j][ptr] == line[i][j][k]) val_l[i][j][k] = val_l[i-1][j][ptr];
}
val_c[i][j].resize(sz(col[i][j]), j);
for(int k = 0, ptr = 0; j != 0 and k < sz(col[i][j]); k++) {
while(ptr < sz(col[i][j-1]) and col[i][j-1][ptr] < col[i][j][k]) ptr++;
if(ptr >= sz(col[i][j-1])) break;
if(col[i][j-1][ptr] == col[i][j][k]) val_c[i][j][k] = val_c[i][j-1][ptr];
}
}
}
// for(int i = 0; i < n; i++) {
// for(int j = 0; j < m; j++) {
// for(int x = 0; x < sz(line[i][j]); x++)
// cout << "line " << i << ": " << line[i][j][x] << " " << j << " " << val_l[i][j][x] << "\n";
// }
// }
// for(int j = 0; j < m; j++) {
// for(int i = 0; i < n; i++) {
// for(int x = 0; x < sz(col[i][j]); x++)
// cout << "col " << j << ": " << col[i][j][x] << " " << i << " " << val_c[i][j][x] << "\n";
// }
// }
// Toda parte anterior é em O(n*m*log)
// Parte que calcula a resposta em O(n*m*min(n,m))
int ans = 0;
for(int i = 1; i < n; i++) {
for(int j = 1; j < m; j++) {
// aqui nos calculamos os retangulos com borda (i,j), ou seja:
// (i,j-1), (i,j-2), ..., (i,j-x) fazem parte do retangulo
// (i-1,j), (i-2,j), ..., (i-y,j) fazem parte do retangulo
for(int x = 0; x < sz(col[i][j-1]); x++) {
for(int y = 0; y < sz(line[i-1][j]); y++) {
int v1 = col[i][j-1][x], v2 = val_c[i][j-1][x];
int v3 = line[i-1][j][y], v4 = val_l[i-1][j][y];
if(v4 <= v1+1 and v2 <= v3+1) ans++;
}
}
}
}
return ans;
}
# |
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 |
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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
348 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 |
1 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 |
# |
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 |
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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
348 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 |
1 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 |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
1208 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
2 ms |
1368 KB |
Output is correct |
27 |
Correct |
2 ms |
1476 KB |
Output is correct |
28 |
Correct |
2 ms |
1372 KB |
Output is correct |
29 |
Correct |
1 ms |
860 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 |
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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
348 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 |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1208 KB |
Output is correct |
20 |
Correct |
1 ms |
1116 KB |
Output is correct |
21 |
Correct |
2 ms |
1368 KB |
Output is correct |
22 |
Correct |
2 ms |
1476 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 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 |
5 ms |
5212 KB |
Output is correct |
31 |
Correct |
4 ms |
5232 KB |
Output is correct |
32 |
Correct |
6 ms |
5464 KB |
Output is correct |
33 |
Correct |
7 ms |
6236 KB |
Output is correct |
34 |
Correct |
10 ms |
7000 KB |
Output is correct |
35 |
Correct |
11 ms |
7332 KB |
Output is correct |
36 |
Correct |
11 ms |
7016 KB |
Output is correct |
37 |
Correct |
11 ms |
6820 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 |
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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
348 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 |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1208 KB |
Output is correct |
20 |
Correct |
1 ms |
1116 KB |
Output is correct |
21 |
Correct |
2 ms |
1368 KB |
Output is correct |
22 |
Correct |
2 ms |
1476 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
5 ms |
5212 KB |
Output is correct |
26 |
Correct |
4 ms |
5232 KB |
Output is correct |
27 |
Correct |
6 ms |
5464 KB |
Output is correct |
28 |
Correct |
7 ms |
6236 KB |
Output is correct |
29 |
Correct |
10 ms |
7000 KB |
Output is correct |
30 |
Correct |
11 ms |
7332 KB |
Output is correct |
31 |
Correct |
11 ms |
7016 KB |
Output is correct |
32 |
Correct |
11 ms |
6820 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 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 |
112 ms |
58200 KB |
Output is correct |
39 |
Correct |
66 ms |
70992 KB |
Output is correct |
40 |
Correct |
78 ms |
70992 KB |
Output is correct |
41 |
Correct |
97 ms |
83916 KB |
Output is correct |
42 |
Correct |
94 ms |
62804 KB |
Output is correct |
43 |
Correct |
64 ms |
62936 KB |
Output is correct |
44 |
Correct |
65 ms |
63148 KB |
Output is correct |
45 |
Correct |
60 ms |
59472 KB |
Output is correct |
46 |
Correct |
81 ms |
66312 KB |
Output is correct |
47 |
Correct |
105 ms |
71476 KB |
Output is correct |
48 |
Correct |
141 ms |
83020 KB |
Output is correct |
49 |
Correct |
154 ms |
85072 KB |
Output is correct |
50 |
Correct |
71 ms |
42540 KB |
Output is correct |
51 |
Correct |
74 ms |
42660 KB |
Output is correct |
52 |
Correct |
139 ms |
81456 KB |
Output is correct |
53 |
Correct |
139 ms |
82512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
1372 KB |
Output is correct |
6 |
Correct |
2 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1372 KB |
Output is correct |
8 |
Correct |
2 ms |
1372 KB |
Output is correct |
9 |
Correct |
2 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
492 ms |
386736 KB |
Output is correct |
8 |
Correct |
1098 ms |
839180 KB |
Output is correct |
9 |
Correct |
1090 ms |
842896 KB |
Output is correct |
10 |
Correct |
1098 ms |
843088 KB |
Output is correct |
11 |
Correct |
223 ms |
320084 KB |
Output is correct |
12 |
Correct |
417 ms |
607828 KB |
Output is correct |
13 |
Correct |
450 ms |
647760 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 |
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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
348 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 |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1208 KB |
Output is correct |
20 |
Correct |
1 ms |
1116 KB |
Output is correct |
21 |
Correct |
2 ms |
1368 KB |
Output is correct |
22 |
Correct |
2 ms |
1476 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
5 ms |
5212 KB |
Output is correct |
26 |
Correct |
4 ms |
5232 KB |
Output is correct |
27 |
Correct |
6 ms |
5464 KB |
Output is correct |
28 |
Correct |
7 ms |
6236 KB |
Output is correct |
29 |
Correct |
10 ms |
7000 KB |
Output is correct |
30 |
Correct |
11 ms |
7332 KB |
Output is correct |
31 |
Correct |
11 ms |
7016 KB |
Output is correct |
32 |
Correct |
11 ms |
6820 KB |
Output is correct |
33 |
Correct |
112 ms |
58200 KB |
Output is correct |
34 |
Correct |
66 ms |
70992 KB |
Output is correct |
35 |
Correct |
78 ms |
70992 KB |
Output is correct |
36 |
Correct |
97 ms |
83916 KB |
Output is correct |
37 |
Correct |
94 ms |
62804 KB |
Output is correct |
38 |
Correct |
64 ms |
62936 KB |
Output is correct |
39 |
Correct |
65 ms |
63148 KB |
Output is correct |
40 |
Correct |
60 ms |
59472 KB |
Output is correct |
41 |
Correct |
81 ms |
66312 KB |
Output is correct |
42 |
Correct |
105 ms |
71476 KB |
Output is correct |
43 |
Correct |
141 ms |
83020 KB |
Output is correct |
44 |
Correct |
154 ms |
85072 KB |
Output is correct |
45 |
Correct |
71 ms |
42540 KB |
Output is correct |
46 |
Correct |
74 ms |
42660 KB |
Output is correct |
47 |
Correct |
139 ms |
81456 KB |
Output is correct |
48 |
Correct |
139 ms |
82512 KB |
Output is correct |
49 |
Correct |
1 ms |
1368 KB |
Output is correct |
50 |
Correct |
1 ms |
1116 KB |
Output is correct |
51 |
Correct |
1 ms |
1116 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
2 ms |
1372 KB |
Output is correct |
54 |
Correct |
2 ms |
1372 KB |
Output is correct |
55 |
Correct |
2 ms |
1372 KB |
Output is correct |
56 |
Correct |
2 ms |
1372 KB |
Output is correct |
57 |
Correct |
2 ms |
1372 KB |
Output is correct |
58 |
Correct |
1 ms |
604 KB |
Output is correct |
59 |
Correct |
1 ms |
1116 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
492 ms |
386736 KB |
Output is correct |
62 |
Correct |
1098 ms |
839180 KB |
Output is correct |
63 |
Correct |
1090 ms |
842896 KB |
Output is correct |
64 |
Correct |
1098 ms |
843088 KB |
Output is correct |
65 |
Correct |
223 ms |
320084 KB |
Output is correct |
66 |
Correct |
417 ms |
607828 KB |
Output is correct |
67 |
Correct |
450 ms |
647760 KB |
Output is correct |
68 |
Correct |
0 ms |
348 KB |
Output is correct |
69 |
Correct |
1 ms |
348 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
3583 ms |
736248 KB |
Output is correct |
74 |
Correct |
845 ms |
902100 KB |
Output is correct |
75 |
Correct |
1055 ms |
896648 KB |
Output is correct |
76 |
Runtime error |
1457 ms |
1048628 KB |
Memory limit exceeded |
77 |
Halted |
0 ms |
0 KB |
- |