#include"rect.h"
#include<algorithm>
#include<unordered_set>
#include<set>
#include<iostream>
using namespace std;
typedef long long ll;
int gap_right[2500][2500];
int gap_down[2500][2500];
int gap_left[2500][2500];
int gap_up[2500][2500];
int arr[2500][2500];
int w,h;
int get_min_down(int y,int x1,int x2){
int res=2500;
for(int x=x1;x<x2;x++)
res=min(res,gap_down[x][y]);
return res;
}
int get_max_up(int y,int x1,int x2){
int res=-1;
for(int x=x1;x<x2;x++)
res=max(res,gap_up[x][y]);
return res;
}
int get_min_right(int x,int y1,int y2){
int res=2500;
for(int y=y1;y<y2;y++)
res=min(res,gap_right[x][y]);
return res;
}
int get_max_left(int x,int y1,int y2){
int res=-1;
for(int y=y1;y<y2;y++)
res=max(res,gap_left[x][y]);
return res;
}
void process_column(int x){
vector<pair<int,int>>vals;
for(int y=0;y<h;y++){
while(!vals.empty()&&vals.back().first<arr[x][y])
vals.pop_back();
gap_up[x][y]=-1;
if(!vals.empty())
gap_up[x][y]=vals.back().second;
vals.emplace_back(arr[x][y],y);
}
vals.clear();
for(int y=h-1;y>=0;y--){
while(!vals.empty()&&vals.back().first<arr[x][y])
vals.pop_back();
gap_down[x][y]=2500;
if(!vals.empty())
gap_down[x][y]=vals.back().second;
vals.emplace_back(arr[x][y],y);
}
}
void process_row(int y){
vector<pair<int,int>>vals;
for(int x=0;x<w;x++){
while(!vals.empty()&&vals.back().first<arr[x][y])
vals.pop_back();
gap_left[x][y]=-1;
if(!vals.empty())
gap_left[x][y]=vals.back().second;
vals.emplace_back(arr[x][y],x);
}
vals.clear();
for(int x=w;x>=0;x--){
while(!vals.empty()&&vals.back().first<arr[x][y])
vals.pop_back();
gap_right[x][y]=2500;
if(!vals.empty())
gap_right[x][y]=vals.back().second;
vals.emplace_back(arr[x][y],x);
}
}
int check1(int x1,int x2,int y){
if(x1==-1||x2==2500||x1==x2-1)
return 0;
int y1=y-1;
int y2=get_min_down(y1,x1+1,x2);
if(y2>=h)
return 0;
if(y1==y2-1)
return 0;
if(get_max_up(y2,x1+1,x2)>y1)
return 0;
if(get_min_right(x1,y1+1,y2)<x2)
return 0;
if(get_max_left(x2,y1+1,y2)>x1)
return 0;
for(int x=x1+1;x<x2;x++)
if(gap_down[x][y1]!=y2&&gap_up[x][y2]!=y1)
return 0;
for(int y=y1+1;y<y2;y++)
if(gap_right[x1][y]!=x2&&gap_left[x2][y]!=x1)
return 0;
return 1;
}
int check2(int x1,int x2,int y){
if(x1==-1||x2==2500||x1==x2-1)
return 0;
int y2=y+1;
int y1=get_max_up(y2,x1+1,x2);
if(y1==-1)
return 0;
if(y1==y2-1)
return 0;
if(get_min_down(y1,x1+1,x2)<y2)
return 0;
if(get_min_right(x1,y1+1,y2)<x2)
return 0;
if(get_max_left(x2,y1+1,y2)>x1)
return 0;
for(int x=x1+1;x<x2;x++)
if((gap_down[x][y1]!=y2&&gap_up[x][y2]!=y1)||gap_down[x][y1]==y2)
return 0;
for(int y=y1+1;y<y2;y++)
if(gap_right[x1][y]!=x2&&gap_left[x2][y]!=x1)
return 0;
return 1;
}
int check(int x1,int x2,int y){
return check1(x1,x2,y)+check2(x1,x2,y);
}
ll count_rectangles(vector<vector<int>>a_){
h=(int)a_.size();
w=(int)a_[0].size();
for(int x=0;x<w;x++){
for(int y=0;y<h;y++){
arr[x][y]=a_[y][x];
}
}
for(int x=0;x<w;x++)
process_column(x);
for(int y=0;y<h;y++)
process_row(y);
ll res=0;
for(int x=0;x<w;x++)
for(int y=1;y<h-1;y++){
res+=check(x,gap_right[x][y],y);
if(arr[gap_left[x][y]][y]!=arr[x][y])
res+=check(gap_left[x][y],x,y);
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10760 KB |
Output is correct |
4 |
Correct |
1 ms |
10584 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10584 KB |
Output is correct |
12 |
Correct |
1 ms |
10584 KB |
Output is correct |
13 |
Correct |
1 ms |
8792 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
8536 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
2 ms |
10584 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
21 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10760 KB |
Output is correct |
4 |
Correct |
1 ms |
10584 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10584 KB |
Output is correct |
12 |
Correct |
1 ms |
10584 KB |
Output is correct |
13 |
Correct |
1 ms |
8792 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
8536 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
2 ms |
10584 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
21 |
Correct |
1 ms |
8540 KB |
Output is correct |
22 |
Correct |
4 ms |
16728 KB |
Output is correct |
23 |
Correct |
4 ms |
16988 KB |
Output is correct |
24 |
Correct |
4 ms |
16732 KB |
Output is correct |
25 |
Correct |
2 ms |
16988 KB |
Output is correct |
26 |
Correct |
3 ms |
16988 KB |
Output is correct |
27 |
Correct |
3 ms |
16732 KB |
Output is correct |
28 |
Correct |
3 ms |
17240 KB |
Output is correct |
29 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10760 KB |
Output is correct |
4 |
Correct |
1 ms |
10584 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10584 KB |
Output is correct |
12 |
Correct |
1 ms |
10584 KB |
Output is correct |
13 |
Correct |
1 ms |
8792 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
4 ms |
16728 KB |
Output is correct |
18 |
Correct |
4 ms |
16988 KB |
Output is correct |
19 |
Correct |
4 ms |
16732 KB |
Output is correct |
20 |
Correct |
2 ms |
16988 KB |
Output is correct |
21 |
Correct |
3 ms |
16988 KB |
Output is correct |
22 |
Correct |
3 ms |
16732 KB |
Output is correct |
23 |
Correct |
3 ms |
17240 KB |
Output is correct |
24 |
Correct |
2 ms |
12636 KB |
Output is correct |
25 |
Correct |
1 ms |
8536 KB |
Output is correct |
26 |
Correct |
1 ms |
8540 KB |
Output is correct |
27 |
Correct |
2 ms |
10584 KB |
Output is correct |
28 |
Correct |
1 ms |
8540 KB |
Output is correct |
29 |
Correct |
1 ms |
8540 KB |
Output is correct |
30 |
Correct |
29 ms |
19288 KB |
Output is correct |
31 |
Correct |
27 ms |
19292 KB |
Output is correct |
32 |
Correct |
28 ms |
19292 KB |
Output is correct |
33 |
Correct |
5 ms |
19292 KB |
Output is correct |
34 |
Correct |
7 ms |
19288 KB |
Output is correct |
35 |
Correct |
7 ms |
19292 KB |
Output is correct |
36 |
Correct |
7 ms |
19292 KB |
Output is correct |
37 |
Correct |
7 ms |
19292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10760 KB |
Output is correct |
4 |
Correct |
1 ms |
10584 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10584 KB |
Output is correct |
12 |
Correct |
1 ms |
10584 KB |
Output is correct |
13 |
Correct |
1 ms |
8792 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
4 ms |
16728 KB |
Output is correct |
18 |
Correct |
4 ms |
16988 KB |
Output is correct |
19 |
Correct |
4 ms |
16732 KB |
Output is correct |
20 |
Correct |
2 ms |
16988 KB |
Output is correct |
21 |
Correct |
3 ms |
16988 KB |
Output is correct |
22 |
Correct |
3 ms |
16732 KB |
Output is correct |
23 |
Correct |
3 ms |
17240 KB |
Output is correct |
24 |
Correct |
2 ms |
12636 KB |
Output is correct |
25 |
Correct |
29 ms |
19288 KB |
Output is correct |
26 |
Correct |
27 ms |
19292 KB |
Output is correct |
27 |
Correct |
28 ms |
19292 KB |
Output is correct |
28 |
Correct |
5 ms |
19292 KB |
Output is correct |
29 |
Correct |
7 ms |
19288 KB |
Output is correct |
30 |
Correct |
7 ms |
19292 KB |
Output is correct |
31 |
Correct |
7 ms |
19292 KB |
Output is correct |
32 |
Correct |
7 ms |
19292 KB |
Output is correct |
33 |
Correct |
1 ms |
8536 KB |
Output is correct |
34 |
Correct |
1 ms |
8540 KB |
Output is correct |
35 |
Correct |
2 ms |
10584 KB |
Output is correct |
36 |
Correct |
1 ms |
8540 KB |
Output is correct |
37 |
Correct |
1 ms |
8540 KB |
Output is correct |
38 |
Correct |
137 ms |
51800 KB |
Output is correct |
39 |
Correct |
135 ms |
51644 KB |
Output is correct |
40 |
Correct |
132 ms |
51652 KB |
Output is correct |
41 |
Correct |
132 ms |
51640 KB |
Output is correct |
42 |
Correct |
1033 ms |
51640 KB |
Output is correct |
43 |
Correct |
1112 ms |
51796 KB |
Output is correct |
44 |
Correct |
1082 ms |
51784 KB |
Output is correct |
45 |
Correct |
957 ms |
51380 KB |
Output is correct |
46 |
Correct |
33 ms |
51592 KB |
Output is correct |
47 |
Correct |
41 ms |
51700 KB |
Output is correct |
48 |
Correct |
63 ms |
51620 KB |
Output is correct |
49 |
Correct |
74 ms |
51572 KB |
Output is correct |
50 |
Correct |
35 ms |
31068 KB |
Output is correct |
51 |
Correct |
37 ms |
49500 KB |
Output is correct |
52 |
Correct |
68 ms |
51544 KB |
Output is correct |
53 |
Correct |
70 ms |
51536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
121944 KB |
Output is correct |
2 |
Correct |
21 ms |
113240 KB |
Output is correct |
3 |
Correct |
13 ms |
121948 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
13 ms |
121948 KB |
Output is correct |
6 |
Correct |
14 ms |
121944 KB |
Output is correct |
7 |
Correct |
13 ms |
121948 KB |
Output is correct |
8 |
Correct |
13 ms |
121992 KB |
Output is correct |
9 |
Correct |
13 ms |
121948 KB |
Output is correct |
10 |
Correct |
12 ms |
122200 KB |
Output is correct |
11 |
Correct |
13 ms |
121944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
10584 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8536 KB |
Output is correct |
7 |
Correct |
179 ms |
144060 KB |
Output is correct |
8 |
Correct |
369 ms |
171612 KB |
Output is correct |
9 |
Correct |
370 ms |
171868 KB |
Output is correct |
10 |
Correct |
382 ms |
171864 KB |
Output is correct |
11 |
Correct |
107 ms |
146784 KB |
Output is correct |
12 |
Correct |
191 ms |
167764 KB |
Output is correct |
13 |
Correct |
213 ms |
171944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10760 KB |
Output is correct |
4 |
Correct |
1 ms |
10584 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10584 KB |
Output is correct |
12 |
Correct |
1 ms |
10584 KB |
Output is correct |
13 |
Correct |
1 ms |
8792 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
4 ms |
16728 KB |
Output is correct |
18 |
Correct |
4 ms |
16988 KB |
Output is correct |
19 |
Correct |
4 ms |
16732 KB |
Output is correct |
20 |
Correct |
2 ms |
16988 KB |
Output is correct |
21 |
Correct |
3 ms |
16988 KB |
Output is correct |
22 |
Correct |
3 ms |
16732 KB |
Output is correct |
23 |
Correct |
3 ms |
17240 KB |
Output is correct |
24 |
Correct |
2 ms |
12636 KB |
Output is correct |
25 |
Correct |
29 ms |
19288 KB |
Output is correct |
26 |
Correct |
27 ms |
19292 KB |
Output is correct |
27 |
Correct |
28 ms |
19292 KB |
Output is correct |
28 |
Correct |
5 ms |
19292 KB |
Output is correct |
29 |
Correct |
7 ms |
19288 KB |
Output is correct |
30 |
Correct |
7 ms |
19292 KB |
Output is correct |
31 |
Correct |
7 ms |
19292 KB |
Output is correct |
32 |
Correct |
7 ms |
19292 KB |
Output is correct |
33 |
Correct |
137 ms |
51800 KB |
Output is correct |
34 |
Correct |
135 ms |
51644 KB |
Output is correct |
35 |
Correct |
132 ms |
51652 KB |
Output is correct |
36 |
Correct |
132 ms |
51640 KB |
Output is correct |
37 |
Correct |
1033 ms |
51640 KB |
Output is correct |
38 |
Correct |
1112 ms |
51796 KB |
Output is correct |
39 |
Correct |
1082 ms |
51784 KB |
Output is correct |
40 |
Correct |
957 ms |
51380 KB |
Output is correct |
41 |
Correct |
33 ms |
51592 KB |
Output is correct |
42 |
Correct |
41 ms |
51700 KB |
Output is correct |
43 |
Correct |
63 ms |
51620 KB |
Output is correct |
44 |
Correct |
74 ms |
51572 KB |
Output is correct |
45 |
Correct |
35 ms |
31068 KB |
Output is correct |
46 |
Correct |
37 ms |
49500 KB |
Output is correct |
47 |
Correct |
68 ms |
51544 KB |
Output is correct |
48 |
Correct |
70 ms |
51536 KB |
Output is correct |
49 |
Correct |
27 ms |
121944 KB |
Output is correct |
50 |
Correct |
21 ms |
113240 KB |
Output is correct |
51 |
Correct |
13 ms |
121948 KB |
Output is correct |
52 |
Correct |
1 ms |
8540 KB |
Output is correct |
53 |
Correct |
13 ms |
121948 KB |
Output is correct |
54 |
Correct |
14 ms |
121944 KB |
Output is correct |
55 |
Correct |
13 ms |
121948 KB |
Output is correct |
56 |
Correct |
13 ms |
121992 KB |
Output is correct |
57 |
Correct |
13 ms |
121948 KB |
Output is correct |
58 |
Correct |
12 ms |
122200 KB |
Output is correct |
59 |
Correct |
13 ms |
121944 KB |
Output is correct |
60 |
Correct |
1 ms |
8536 KB |
Output is correct |
61 |
Correct |
179 ms |
144060 KB |
Output is correct |
62 |
Correct |
369 ms |
171612 KB |
Output is correct |
63 |
Correct |
370 ms |
171868 KB |
Output is correct |
64 |
Correct |
382 ms |
171864 KB |
Output is correct |
65 |
Correct |
107 ms |
146784 KB |
Output is correct |
66 |
Correct |
191 ms |
167764 KB |
Output is correct |
67 |
Correct |
213 ms |
171944 KB |
Output is correct |
68 |
Correct |
1 ms |
8536 KB |
Output is correct |
69 |
Correct |
1 ms |
8540 KB |
Output is correct |
70 |
Correct |
2 ms |
10584 KB |
Output is correct |
71 |
Correct |
1 ms |
8540 KB |
Output is correct |
72 |
Correct |
1 ms |
8540 KB |
Output is correct |
73 |
Execution timed out |
5026 ms |
171748 KB |
Time limit exceeded |
74 |
Halted |
0 ms |
0 KB |
- |