#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<pii> line[n][m], col[n][m];
// calculando os pares na linha
vector<int> r(max(n,m));
for(int i = 0; i < n; i++) {
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], i});
}
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, i});
}
}
// calculando os pares na coluna
for(int j = 0; j < m; j++) {
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], j});
}
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, j});
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
sort(all(line[i][j]));
sort(all(col[i][j]));
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].fr < line[i][j][k].fr) ptr++;
if(ptr >= sz(line[i-1][j])) break;
if(line[i-1][j][ptr].fr == line[i][j][k].fr) line[i][j][k].sc = line[i-1][j][ptr].sc;
}
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].fr < col[i][j][k].fr) ptr++;
if(ptr >= sz(col[i][j-1])) break;
if(col[i][j-1][ptr].fr == col[i][j][k].fr) col[i][j][k].sc = col[i][j-1][ptr].sc;
}
}
}
// 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].fr, v2 = col[i][j-1][x].sc;
int v3 = line[i-1][j][y].fr, v4 = line[i-1][j][y].sc;
if(v4 <= v1+1 and v2 <= v3+1) ans++;
}
}
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
404 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 |
344 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 |
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 |
1 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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
404 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 |
344 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 |
856 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
604 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
404 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 |
856 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
344 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 |
4 ms |
3420 KB |
Output is correct |
31 |
Correct |
4 ms |
3420 KB |
Output is correct |
32 |
Correct |
4 ms |
3408 KB |
Output is correct |
33 |
Correct |
4 ms |
3420 KB |
Output is correct |
34 |
Correct |
8 ms |
3928 KB |
Output is correct |
35 |
Correct |
8 ms |
4028 KB |
Output is correct |
36 |
Correct |
7 ms |
3732 KB |
Output is correct |
37 |
Correct |
8 ms |
3932 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
404 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 |
856 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
4 ms |
3420 KB |
Output is correct |
26 |
Correct |
4 ms |
3420 KB |
Output is correct |
27 |
Correct |
4 ms |
3408 KB |
Output is correct |
28 |
Correct |
4 ms |
3420 KB |
Output is correct |
29 |
Correct |
8 ms |
3928 KB |
Output is correct |
30 |
Correct |
8 ms |
4028 KB |
Output is correct |
31 |
Correct |
7 ms |
3732 KB |
Output is correct |
32 |
Correct |
8 ms |
3932 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
344 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 |
101 ms |
32852 KB |
Output is correct |
39 |
Correct |
44 ms |
37716 KB |
Output is correct |
40 |
Correct |
43 ms |
37716 KB |
Output is correct |
41 |
Correct |
52 ms |
42576 KB |
Output is correct |
42 |
Correct |
51 ms |
38376 KB |
Output is correct |
43 |
Correct |
51 ms |
38504 KB |
Output is correct |
44 |
Correct |
50 ms |
38500 KB |
Output is correct |
45 |
Correct |
56 ms |
36188 KB |
Output is correct |
46 |
Correct |
51 ms |
34828 KB |
Output is correct |
47 |
Correct |
62 ms |
37488 KB |
Output is correct |
48 |
Correct |
104 ms |
45240 KB |
Output is correct |
49 |
Correct |
106 ms |
45192 KB |
Output is correct |
50 |
Correct |
50 ms |
22712 KB |
Output is correct |
51 |
Correct |
49 ms |
22868 KB |
Output is correct |
52 |
Correct |
99 ms |
43860 KB |
Output is correct |
53 |
Correct |
103 ms |
43860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
0 ms |
860 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
328 ms |
202064 KB |
Output is correct |
8 |
Correct |
724 ms |
438332 KB |
Output is correct |
9 |
Correct |
760 ms |
440400 KB |
Output is correct |
10 |
Correct |
739 ms |
440656 KB |
Output is correct |
11 |
Correct |
128 ms |
169300 KB |
Output is correct |
12 |
Correct |
271 ms |
321616 KB |
Output is correct |
13 |
Correct |
316 ms |
342940 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
404 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 |
856 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
4 ms |
3420 KB |
Output is correct |
26 |
Correct |
4 ms |
3420 KB |
Output is correct |
27 |
Correct |
4 ms |
3408 KB |
Output is correct |
28 |
Correct |
4 ms |
3420 KB |
Output is correct |
29 |
Correct |
8 ms |
3928 KB |
Output is correct |
30 |
Correct |
8 ms |
4028 KB |
Output is correct |
31 |
Correct |
7 ms |
3732 KB |
Output is correct |
32 |
Correct |
8 ms |
3932 KB |
Output is correct |
33 |
Correct |
101 ms |
32852 KB |
Output is correct |
34 |
Correct |
44 ms |
37716 KB |
Output is correct |
35 |
Correct |
43 ms |
37716 KB |
Output is correct |
36 |
Correct |
52 ms |
42576 KB |
Output is correct |
37 |
Correct |
51 ms |
38376 KB |
Output is correct |
38 |
Correct |
51 ms |
38504 KB |
Output is correct |
39 |
Correct |
50 ms |
38500 KB |
Output is correct |
40 |
Correct |
56 ms |
36188 KB |
Output is correct |
41 |
Correct |
51 ms |
34828 KB |
Output is correct |
42 |
Correct |
62 ms |
37488 KB |
Output is correct |
43 |
Correct |
104 ms |
45240 KB |
Output is correct |
44 |
Correct |
106 ms |
45192 KB |
Output is correct |
45 |
Correct |
50 ms |
22712 KB |
Output is correct |
46 |
Correct |
49 ms |
22868 KB |
Output is correct |
47 |
Correct |
99 ms |
43860 KB |
Output is correct |
48 |
Correct |
103 ms |
43860 KB |
Output is correct |
49 |
Correct |
1 ms |
860 KB |
Output is correct |
50 |
Correct |
1 ms |
860 KB |
Output is correct |
51 |
Correct |
0 ms |
860 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
1 ms |
860 KB |
Output is correct |
54 |
Correct |
1 ms |
860 KB |
Output is correct |
55 |
Correct |
1 ms |
860 KB |
Output is correct |
56 |
Correct |
1 ms |
860 KB |
Output is correct |
57 |
Correct |
1 ms |
860 KB |
Output is correct |
58 |
Correct |
0 ms |
604 KB |
Output is correct |
59 |
Correct |
1 ms |
604 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
328 ms |
202064 KB |
Output is correct |
62 |
Correct |
724 ms |
438332 KB |
Output is correct |
63 |
Correct |
760 ms |
440400 KB |
Output is correct |
64 |
Correct |
739 ms |
440656 KB |
Output is correct |
65 |
Correct |
128 ms |
169300 KB |
Output is correct |
66 |
Correct |
271 ms |
321616 KB |
Output is correct |
67 |
Correct |
316 ms |
342940 KB |
Output is correct |
68 |
Correct |
0 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
344 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 |
3398 ms |
404564 KB |
Output is correct |
74 |
Correct |
548 ms |
471380 KB |
Output is correct |
75 |
Correct |
740 ms |
471376 KB |
Output is correct |
76 |
Correct |
901 ms |
552920 KB |
Output is correct |
77 |
Correct |
655 ms |
507624 KB |
Output is correct |
78 |
Correct |
990 ms |
358048 KB |
Output is correct |
79 |
Correct |
1060 ms |
385744 KB |
Output is correct |
80 |
Correct |
1778 ms |
596520 KB |
Output is correct |
81 |
Correct |
1008 ms |
374100 KB |
Output is correct |
82 |
Correct |
1394 ms |
498524 KB |
Output is correct |
83 |
Correct |
1754 ms |
622928 KB |
Output is correct |
84 |
Correct |
942 ms |
356440 KB |
Output is correct |
85 |
Correct |
1664 ms |
607040 KB |
Output is correct |
86 |
Correct |
1592 ms |
589484 KB |
Output is correct |
87 |
Correct |
393 ms |
309048 KB |
Output is correct |
88 |
Correct |
646 ms |
505876 KB |
Output is correct |
89 |
Correct |
671 ms |
506076 KB |
Output is correct |
90 |
Correct |
661 ms |
506116 KB |
Output is correct |