#include <bits/stdc++.h>
#include "rect.h"
using namespace std;
#define fr first
#define sc second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
typedef long long lld;
typedef pair<int, int> pii;
int N, M;
int A[2502][2502];
pii ver[2502][2502], hor[2502][2502];
struct SEGMENT {
int a, b, t, dp;
bool operator < (const SEGMENT &ot)const{
if (t != ot.t) return t < ot.t;
if (a != ot.a) return a < ot.a;
return b < ot.b;
}
bool operator == (const SEGMENT &ot)const{
return a == ot.a && b == ot.b && t == ot.t;
}
bool operator != (const SEGMENT &ot)const{
return a != ot.a || b != ot.b || t != ot.t;
}
};
lld count_rectangles(vector<vector<int> > a)
{
N = sz(a); M = sz(a[0]);
for (int i=1;i<=N;i++) for (int j=1;j<=M;j++) A[i][j] = a[i-1][j-1];
vector <SEGMENT> vertical, horizontal;
for (int i=1;i<=N;i++){
{
stack <int> stk;
for (int j=1;j<=M;j++){
while (!stk.empty() && A[i][stk.top()] <= A[i][j]) stk.pop();
hor[i][j].fr = stk.empty() ? 1 : stk.top()+1;
stk.push(j);
}
}
{
stack <int> stk;
for (int j=M;j;j--){
while (!stk.empty() && A[i][stk.top()] <= A[i][j]) stk.pop();
hor[i][j].sc = stk.empty() ? M : stk.top()-1;
horizontal.pb({hor[i][j].fr, hor[i][j].sc, i});
stk.push(j);
}
}
}
for (int j=1;j<=M;j++){
{
stack <int> stk;
for (int i=1;i<=N;i++){
while (!stk.empty() && A[stk.top()][j] <= A[i][j]) stk.pop();
ver[i][j].fr = stk.empty() ? 1 : stk.top()+1;
stk.push(i);
}
}
{
stack <int> stk;
for (int i=N;i;i--){
while (!stk.empty() && A[stk.top()][j] <= A[i][j]) stk.pop();
ver[i][j].sc = stk.empty() ? N : stk.top()-1;
vertical.pb({ver[i][j].fr, ver[i][j].sc, j});
stk.push(i);
}
}
}
sort(all(horizontal));
sort(all(vertical));
for (auto i=horizontal.begin(),j=i;i!=horizontal.end();i++){
while (*j < SEGMENT{i->a, i->b, i->t-1}) j++;
if (*j == SEGMENT{i->a, i->b, i->t-1}) i->dp = j->dp+1;
else i->dp = 1;
}
for (auto i=vertical.begin(),j=i;i!=vertical.end();i++){
while (*j < SEGMENT{i->a, i->b, i->t-1}) j++;
if (*j == SEGMENT{i->a, i->b, i->t-1}) i->dp = j->dp+1;
else i->dp = 1;
}
set <tuple<int, int, int, int>> vis;
for (int i=1;i<=N;i++) for (int j=1;j<=M;j++){
int sy = ver[i][j].fr, ey = ver[i][j].sc;
int sx = hor[i][j].fr, ex = hor[i][j].sc;
if (sy == 1 || sx == 1 || ey == N || ex == M) continue;
{
auto it = lower_bound(all(vertical), SEGMENT{sy, ey, ex});
if (it == vertical.end() || *it != SEGMENT{sy, ey, ex} || it->dp <= ex-sx) continue;
}
{
auto it = lower_bound(all(horizontal), SEGMENT{sx, ex, ey});
if (it == horizontal.end() || *it != SEGMENT{sx, ex, ey} || it->dp <= ey-sy) continue;
}
auto tp = make_tuple(sy, sx, ey, ex);
if (vis.count(tp)) continue;
vis.insert(tp);
}
return sz(vis);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
760 KB |
Output is correct |
4 |
Correct |
3 ms |
760 KB |
Output is correct |
5 |
Correct |
2 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
760 KB |
Output is correct |
7 |
Correct |
3 ms |
760 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
760 KB |
Output is correct |
10 |
Correct |
3 ms |
764 KB |
Output is correct |
11 |
Correct |
3 ms |
760 KB |
Output is correct |
12 |
Correct |
3 ms |
764 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
6 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
760 KB |
Output is correct |
20 |
Correct |
3 ms |
760 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
760 KB |
Output is correct |
4 |
Correct |
3 ms |
760 KB |
Output is correct |
5 |
Correct |
2 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
760 KB |
Output is correct |
7 |
Correct |
3 ms |
760 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
760 KB |
Output is correct |
10 |
Correct |
3 ms |
764 KB |
Output is correct |
11 |
Correct |
3 ms |
760 KB |
Output is correct |
12 |
Correct |
3 ms |
764 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
6 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
7 ms |
2040 KB |
Output is correct |
18 |
Correct |
7 ms |
2040 KB |
Output is correct |
19 |
Correct |
7 ms |
2040 KB |
Output is correct |
20 |
Correct |
6 ms |
1788 KB |
Output is correct |
21 |
Correct |
6 ms |
1784 KB |
Output is correct |
22 |
Correct |
7 ms |
1912 KB |
Output is correct |
23 |
Correct |
6 ms |
1784 KB |
Output is correct |
24 |
Correct |
5 ms |
1528 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
760 KB |
Output is correct |
28 |
Correct |
3 ms |
760 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
760 KB |
Output is correct |
4 |
Correct |
3 ms |
760 KB |
Output is correct |
5 |
Correct |
2 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
760 KB |
Output is correct |
7 |
Correct |
3 ms |
760 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
760 KB |
Output is correct |
10 |
Correct |
3 ms |
764 KB |
Output is correct |
11 |
Correct |
3 ms |
760 KB |
Output is correct |
12 |
Correct |
3 ms |
764 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
6 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
7 ms |
2040 KB |
Output is correct |
18 |
Correct |
7 ms |
2040 KB |
Output is correct |
19 |
Correct |
7 ms |
2040 KB |
Output is correct |
20 |
Correct |
6 ms |
1788 KB |
Output is correct |
21 |
Correct |
6 ms |
1784 KB |
Output is correct |
22 |
Correct |
7 ms |
1912 KB |
Output is correct |
23 |
Correct |
6 ms |
1784 KB |
Output is correct |
24 |
Correct |
5 ms |
1528 KB |
Output is correct |
25 |
Correct |
40 ms |
7504 KB |
Output is correct |
26 |
Correct |
40 ms |
7532 KB |
Output is correct |
27 |
Correct |
40 ms |
7528 KB |
Output is correct |
28 |
Correct |
22 ms |
5920 KB |
Output is correct |
29 |
Correct |
29 ms |
5988 KB |
Output is correct |
30 |
Correct |
30 ms |
6024 KB |
Output is correct |
31 |
Correct |
30 ms |
6128 KB |
Output is correct |
32 |
Correct |
30 ms |
5992 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
3 ms |
376 KB |
Output is correct |
35 |
Correct |
3 ms |
760 KB |
Output is correct |
36 |
Correct |
3 ms |
760 KB |
Output is correct |
37 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
760 KB |
Output is correct |
4 |
Correct |
3 ms |
760 KB |
Output is correct |
5 |
Correct |
2 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
760 KB |
Output is correct |
7 |
Correct |
3 ms |
760 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
760 KB |
Output is correct |
10 |
Correct |
3 ms |
764 KB |
Output is correct |
11 |
Correct |
3 ms |
760 KB |
Output is correct |
12 |
Correct |
3 ms |
764 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
6 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
7 ms |
2040 KB |
Output is correct |
18 |
Correct |
7 ms |
2040 KB |
Output is correct |
19 |
Correct |
7 ms |
2040 KB |
Output is correct |
20 |
Correct |
6 ms |
1788 KB |
Output is correct |
21 |
Correct |
6 ms |
1784 KB |
Output is correct |
22 |
Correct |
7 ms |
1912 KB |
Output is correct |
23 |
Correct |
6 ms |
1784 KB |
Output is correct |
24 |
Correct |
5 ms |
1528 KB |
Output is correct |
25 |
Correct |
40 ms |
7504 KB |
Output is correct |
26 |
Correct |
40 ms |
7532 KB |
Output is correct |
27 |
Correct |
40 ms |
7528 KB |
Output is correct |
28 |
Correct |
22 ms |
5920 KB |
Output is correct |
29 |
Correct |
29 ms |
5988 KB |
Output is correct |
30 |
Correct |
30 ms |
6024 KB |
Output is correct |
31 |
Correct |
30 ms |
6128 KB |
Output is correct |
32 |
Correct |
30 ms |
5992 KB |
Output is correct |
33 |
Correct |
168 ms |
40572 KB |
Output is correct |
34 |
Correct |
174 ms |
40452 KB |
Output is correct |
35 |
Correct |
170 ms |
40400 KB |
Output is correct |
36 |
Correct |
173 ms |
40400 KB |
Output is correct |
37 |
Correct |
598 ms |
68048 KB |
Output is correct |
38 |
Correct |
629 ms |
68124 KB |
Output is correct |
39 |
Correct |
609 ms |
68188 KB |
Output is correct |
40 |
Correct |
566 ms |
63636 KB |
Output is correct |
41 |
Correct |
230 ms |
40412 KB |
Output is correct |
42 |
Correct |
272 ms |
40584 KB |
Output is correct |
43 |
Correct |
377 ms |
48076 KB |
Output is correct |
44 |
Correct |
386 ms |
48236 KB |
Output is correct |
45 |
Correct |
191 ms |
28504 KB |
Output is correct |
46 |
Correct |
183 ms |
24240 KB |
Output is correct |
47 |
Correct |
409 ms |
48076 KB |
Output is correct |
48 |
Correct |
402 ms |
48096 KB |
Output is correct |
49 |
Correct |
2 ms |
376 KB |
Output is correct |
50 |
Correct |
3 ms |
376 KB |
Output is correct |
51 |
Correct |
3 ms |
760 KB |
Output is correct |
52 |
Correct |
3 ms |
760 KB |
Output is correct |
53 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
888 KB |
Output is correct |
2 |
Correct |
5 ms |
888 KB |
Output is correct |
3 |
Correct |
4 ms |
888 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
1016 KB |
Output is correct |
6 |
Correct |
6 ms |
888 KB |
Output is correct |
7 |
Correct |
5 ms |
888 KB |
Output is correct |
8 |
Correct |
5 ms |
888 KB |
Output is correct |
9 |
Correct |
5 ms |
888 KB |
Output is correct |
10 |
Correct |
3 ms |
632 KB |
Output is correct |
11 |
Correct |
5 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
480 KB |
Output is correct |
2 |
Correct |
1600 ms |
191104 KB |
Output is correct |
3 |
Correct |
3711 ms |
394044 KB |
Output is correct |
4 |
Correct |
3693 ms |
395136 KB |
Output is correct |
5 |
Correct |
3681 ms |
395256 KB |
Output is correct |
6 |
Correct |
771 ms |
196260 KB |
Output is correct |
7 |
Correct |
1680 ms |
385952 KB |
Output is correct |
8 |
Correct |
1752 ms |
395168 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
760 KB |
Output is correct |
12 |
Correct |
3 ms |
760 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
760 KB |
Output is correct |
4 |
Correct |
3 ms |
760 KB |
Output is correct |
5 |
Correct |
2 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
760 KB |
Output is correct |
7 |
Correct |
3 ms |
760 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
760 KB |
Output is correct |
10 |
Correct |
3 ms |
764 KB |
Output is correct |
11 |
Correct |
3 ms |
760 KB |
Output is correct |
12 |
Correct |
3 ms |
764 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
6 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
7 ms |
2040 KB |
Output is correct |
18 |
Correct |
7 ms |
2040 KB |
Output is correct |
19 |
Correct |
7 ms |
2040 KB |
Output is correct |
20 |
Correct |
6 ms |
1788 KB |
Output is correct |
21 |
Correct |
6 ms |
1784 KB |
Output is correct |
22 |
Correct |
7 ms |
1912 KB |
Output is correct |
23 |
Correct |
6 ms |
1784 KB |
Output is correct |
24 |
Correct |
5 ms |
1528 KB |
Output is correct |
25 |
Correct |
40 ms |
7504 KB |
Output is correct |
26 |
Correct |
40 ms |
7532 KB |
Output is correct |
27 |
Correct |
40 ms |
7528 KB |
Output is correct |
28 |
Correct |
22 ms |
5920 KB |
Output is correct |
29 |
Correct |
29 ms |
5988 KB |
Output is correct |
30 |
Correct |
30 ms |
6024 KB |
Output is correct |
31 |
Correct |
30 ms |
6128 KB |
Output is correct |
32 |
Correct |
30 ms |
5992 KB |
Output is correct |
33 |
Correct |
168 ms |
40572 KB |
Output is correct |
34 |
Correct |
174 ms |
40452 KB |
Output is correct |
35 |
Correct |
170 ms |
40400 KB |
Output is correct |
36 |
Correct |
173 ms |
40400 KB |
Output is correct |
37 |
Correct |
598 ms |
68048 KB |
Output is correct |
38 |
Correct |
629 ms |
68124 KB |
Output is correct |
39 |
Correct |
609 ms |
68188 KB |
Output is correct |
40 |
Correct |
566 ms |
63636 KB |
Output is correct |
41 |
Correct |
230 ms |
40412 KB |
Output is correct |
42 |
Correct |
272 ms |
40584 KB |
Output is correct |
43 |
Correct |
377 ms |
48076 KB |
Output is correct |
44 |
Correct |
386 ms |
48236 KB |
Output is correct |
45 |
Correct |
191 ms |
28504 KB |
Output is correct |
46 |
Correct |
183 ms |
24240 KB |
Output is correct |
47 |
Correct |
409 ms |
48076 KB |
Output is correct |
48 |
Correct |
402 ms |
48096 KB |
Output is correct |
49 |
Correct |
5 ms |
888 KB |
Output is correct |
50 |
Correct |
5 ms |
888 KB |
Output is correct |
51 |
Correct |
4 ms |
888 KB |
Output is correct |
52 |
Correct |
2 ms |
376 KB |
Output is correct |
53 |
Correct |
5 ms |
1016 KB |
Output is correct |
54 |
Correct |
6 ms |
888 KB |
Output is correct |
55 |
Correct |
5 ms |
888 KB |
Output is correct |
56 |
Correct |
5 ms |
888 KB |
Output is correct |
57 |
Correct |
5 ms |
888 KB |
Output is correct |
58 |
Correct |
3 ms |
632 KB |
Output is correct |
59 |
Correct |
5 ms |
760 KB |
Output is correct |
60 |
Correct |
2 ms |
480 KB |
Output is correct |
61 |
Correct |
1600 ms |
191104 KB |
Output is correct |
62 |
Correct |
3711 ms |
394044 KB |
Output is correct |
63 |
Correct |
3693 ms |
395136 KB |
Output is correct |
64 |
Correct |
3681 ms |
395256 KB |
Output is correct |
65 |
Correct |
771 ms |
196260 KB |
Output is correct |
66 |
Correct |
1680 ms |
385952 KB |
Output is correct |
67 |
Correct |
1752 ms |
395168 KB |
Output is correct |
68 |
Correct |
2627 ms |
395284 KB |
Output is correct |
69 |
Correct |
2549 ms |
395020 KB |
Output is correct |
70 |
Correct |
2486 ms |
395460 KB |
Output is correct |
71 |
Correct |
2558 ms |
395004 KB |
Output is correct |
72 |
Execution timed out |
5111 ms |
502572 KB |
Time limit exceeded |
73 |
Halted |
0 ms |
0 KB |
- |