#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
vector<short> b[2505][2505];
vector<short> c[2505][2505];
vector<pair<short,short>> d[2505][2505];
vector<pair<short,short>> e[2505][2505];
short lef[2505][2505];
short rig[2505][2505];
short up[2505][2505];
short down[2505][2505];
vector<short> v[2500*2500];
long long count_rectangles(std::vector<std::vector<int> > a) {
int n = a.size();
int m = a[0].size();
for(int i=1 ; i<n-1 ; i++){
stack<int> S;
for(int j=0 ; j<m ; j++){
bool flag = true;
while(!S.empty() && a[i][S.top()] <= a[i][j]){
int x = S.top();
if(x +1 <j && flag)
b[x+1][j-1].push_back(i);
if(a[i][S.top()]==a[i][j])flag=false;
S.pop();
}
if(!S.empty() ){
int x = S.top();
lef[i][j] = x;
if(x+1<j && flag)
b[x+1][j-1].push_back(i);
}
else{
lef[i][j]=-1;
}
S.push(j);
}
while(!S.empty())S.pop();
for(int j=m-1; j>=0 ; j--){
while(!S.empty() && a[i][S.top()] <= a[i][j]){
S.pop();
}
if(!S.empty() ){
int x = S.top();
rig[i][j] = x;
}
else{
rig[i][j]=-1;
}
S.push(j);
}
}
for(int i=1 ; i<m-1 ; i++){
stack<int> S;
for(int j=0 ; j<n ; j++){
bool flag = true;
while(!S.empty() && a[S.top()][i] <= a[j][i]){
int x = S.top();
if(x +1 <j && flag)
c[x+1][j-1].push_back(i);
if(a[S.top()][i]==a[j][i])flag=false;
S.pop();
}
if(!S.empty() ){
int x = S.top();
up[j][i]=x;
if(x+1<j && flag)
c[x+1][j-1].push_back(i);
}
else{
up[j][i]=-1;
}
S.push(j);
}
while(!S.empty())S.pop();
for(int j=n-1 ; j>=0 ; j--){
while(!S.empty() && a[S.top()][i] <= a[j][i]){
S.pop();
}
if(!S.empty() ){
int x = S.top();
down[j][i]=x;
}
else{
down[j][i]=-1;
}
S.push(j);
}
}
std::vector<vector<int>>().swap(a);
long long ans = 0;
for(int i=1 ; i<m ; i++){
for(int j=i ; j<m ; j++){
reverse(b[i][j].begin(),b[i][j].end());
int prv = -1;
int y;
for(auto x: b[i][j]){
if(x != prv-1){
if(prv>=0)
d[i][j].push_back({prv,y});
y=x;
}
prv=x;
}
if(prv>=0){
d[i][j].push_back({prv,y});
}
sort(d[i][j].begin(),d[i][j].end());
std::vector<short>().swap(b[i][j]);
}
}
for(int i=1 ; i<n ; i++){
for(int j=i ; j<n ; j++){
reverse(c[i][j].begin(),c[i][j].end());
int prv = -1;
int y;
for(auto x: c[i][j]){
if(x != prv-1){
if(prv>=0)
e[i][j].push_back({prv,y});
y=x;
}
prv=x;
}
if(prv>=0){
e[i][j].push_back({prv,y});
}
sort(e[i][j].begin(),e[i][j].end());
std::vector<short>().swap(c[i][j]);
}
}
int nn = 0;
for(int i = 1 ; i<n-1 ; i++){
for(int j=1 ; j<m-1 ; j++){
short x = lef[i][j];
short y = rig[i][j];
short z = up[i][j];
short w = down[i][j];
v[++nn]={x,y,z,w};
}
}
sort(v+1,v+nn+1);
for(int i=1 ; i<=nn ; i++){
if(v[i]==v[i-1])continue;
auto vv = v[i];
int x=vv[0];
int y = vv[1];
int z =vv[2];
int w = vv[3];
if(x==-1 ||y==-1 || z==-1 || w==-1)continue;
auto k = upper_bound(d[x+1][y-1].begin(),d[x+1][y-1].end(),(pair<short,short>){z+1,32767});
if(k== d[x+1][y-1].begin())continue;
if((--k)->second <w-1)continue;
auto t = upper_bound(e[z+1][w-1].begin(),e[z+1][w-1].end(),(pair<short,short>){x+1,32767});
if(t== e[z+1][w-1].begin())continue;
if((--t)->second <y-1)continue;
ans++;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
320 ms |
744532 KB |
Output is correct |
2 |
Correct |
166 ms |
744524 KB |
Output is correct |
3 |
Correct |
167 ms |
744536 KB |
Output is correct |
4 |
Correct |
169 ms |
744524 KB |
Output is correct |
5 |
Correct |
165 ms |
744712 KB |
Output is correct |
6 |
Correct |
165 ms |
744692 KB |
Output is correct |
7 |
Correct |
173 ms |
744640 KB |
Output is correct |
8 |
Correct |
165 ms |
744712 KB |
Output is correct |
9 |
Correct |
169 ms |
744572 KB |
Output is correct |
10 |
Correct |
175 ms |
744536 KB |
Output is correct |
11 |
Correct |
165 ms |
744528 KB |
Output is correct |
12 |
Correct |
167 ms |
744584 KB |
Output is correct |
13 |
Correct |
164 ms |
738388 KB |
Output is correct |
14 |
Correct |
162 ms |
742480 KB |
Output is correct |
15 |
Correct |
165 ms |
744656 KB |
Output is correct |
16 |
Correct |
164 ms |
744536 KB |
Output is correct |
17 |
Correct |
169 ms |
738388 KB |
Output is correct |
18 |
Correct |
166 ms |
740412 KB |
Output is correct |
19 |
Correct |
171 ms |
744720 KB |
Output is correct |
20 |
Correct |
171 ms |
744672 KB |
Output is correct |
21 |
Correct |
176 ms |
742484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
320 ms |
744532 KB |
Output is correct |
2 |
Correct |
166 ms |
744524 KB |
Output is correct |
3 |
Correct |
167 ms |
744536 KB |
Output is correct |
4 |
Correct |
169 ms |
744524 KB |
Output is correct |
5 |
Correct |
165 ms |
744712 KB |
Output is correct |
6 |
Correct |
165 ms |
744692 KB |
Output is correct |
7 |
Correct |
173 ms |
744640 KB |
Output is correct |
8 |
Correct |
165 ms |
744712 KB |
Output is correct |
9 |
Correct |
169 ms |
744572 KB |
Output is correct |
10 |
Correct |
175 ms |
744536 KB |
Output is correct |
11 |
Correct |
165 ms |
744528 KB |
Output is correct |
12 |
Correct |
167 ms |
744584 KB |
Output is correct |
13 |
Correct |
164 ms |
738388 KB |
Output is correct |
14 |
Correct |
162 ms |
742480 KB |
Output is correct |
15 |
Correct |
165 ms |
744656 KB |
Output is correct |
16 |
Correct |
164 ms |
744536 KB |
Output is correct |
17 |
Correct |
169 ms |
738388 KB |
Output is correct |
18 |
Correct |
166 ms |
740412 KB |
Output is correct |
19 |
Correct |
171 ms |
744720 KB |
Output is correct |
20 |
Correct |
171 ms |
744672 KB |
Output is correct |
21 |
Correct |
176 ms |
742484 KB |
Output is correct |
22 |
Correct |
167 ms |
744844 KB |
Output is correct |
23 |
Correct |
170 ms |
745064 KB |
Output is correct |
24 |
Correct |
166 ms |
744916 KB |
Output is correct |
25 |
Correct |
170 ms |
744788 KB |
Output is correct |
26 |
Correct |
171 ms |
744788 KB |
Output is correct |
27 |
Correct |
166 ms |
744788 KB |
Output is correct |
28 |
Correct |
169 ms |
744788 KB |
Output is correct |
29 |
Correct |
167 ms |
744752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
320 ms |
744532 KB |
Output is correct |
2 |
Correct |
166 ms |
744524 KB |
Output is correct |
3 |
Correct |
167 ms |
744536 KB |
Output is correct |
4 |
Correct |
169 ms |
744524 KB |
Output is correct |
5 |
Correct |
165 ms |
744712 KB |
Output is correct |
6 |
Correct |
165 ms |
744692 KB |
Output is correct |
7 |
Correct |
173 ms |
744640 KB |
Output is correct |
8 |
Correct |
165 ms |
744712 KB |
Output is correct |
9 |
Correct |
169 ms |
744572 KB |
Output is correct |
10 |
Correct |
175 ms |
744536 KB |
Output is correct |
11 |
Correct |
165 ms |
744528 KB |
Output is correct |
12 |
Correct |
167 ms |
744584 KB |
Output is correct |
13 |
Correct |
164 ms |
738388 KB |
Output is correct |
14 |
Correct |
162 ms |
742480 KB |
Output is correct |
15 |
Correct |
165 ms |
744656 KB |
Output is correct |
16 |
Correct |
164 ms |
744536 KB |
Output is correct |
17 |
Correct |
167 ms |
744844 KB |
Output is correct |
18 |
Correct |
170 ms |
745064 KB |
Output is correct |
19 |
Correct |
166 ms |
744916 KB |
Output is correct |
20 |
Correct |
170 ms |
744788 KB |
Output is correct |
21 |
Correct |
171 ms |
744788 KB |
Output is correct |
22 |
Correct |
166 ms |
744788 KB |
Output is correct |
23 |
Correct |
169 ms |
744788 KB |
Output is correct |
24 |
Correct |
167 ms |
744752 KB |
Output is correct |
25 |
Correct |
169 ms |
738388 KB |
Output is correct |
26 |
Correct |
166 ms |
740412 KB |
Output is correct |
27 |
Correct |
171 ms |
744720 KB |
Output is correct |
28 |
Correct |
171 ms |
744672 KB |
Output is correct |
29 |
Correct |
176 ms |
742484 KB |
Output is correct |
30 |
Correct |
174 ms |
746120 KB |
Output is correct |
31 |
Correct |
178 ms |
745960 KB |
Output is correct |
32 |
Correct |
181 ms |
746024 KB |
Output is correct |
33 |
Correct |
180 ms |
746204 KB |
Output is correct |
34 |
Correct |
182 ms |
746576 KB |
Output is correct |
35 |
Correct |
182 ms |
746492 KB |
Output is correct |
36 |
Correct |
190 ms |
746580 KB |
Output is correct |
37 |
Correct |
184 ms |
746324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
320 ms |
744532 KB |
Output is correct |
2 |
Correct |
166 ms |
744524 KB |
Output is correct |
3 |
Correct |
167 ms |
744536 KB |
Output is correct |
4 |
Correct |
169 ms |
744524 KB |
Output is correct |
5 |
Correct |
165 ms |
744712 KB |
Output is correct |
6 |
Correct |
165 ms |
744692 KB |
Output is correct |
7 |
Correct |
173 ms |
744640 KB |
Output is correct |
8 |
Correct |
165 ms |
744712 KB |
Output is correct |
9 |
Correct |
169 ms |
744572 KB |
Output is correct |
10 |
Correct |
175 ms |
744536 KB |
Output is correct |
11 |
Correct |
165 ms |
744528 KB |
Output is correct |
12 |
Correct |
167 ms |
744584 KB |
Output is correct |
13 |
Correct |
164 ms |
738388 KB |
Output is correct |
14 |
Correct |
162 ms |
742480 KB |
Output is correct |
15 |
Correct |
165 ms |
744656 KB |
Output is correct |
16 |
Correct |
164 ms |
744536 KB |
Output is correct |
17 |
Correct |
167 ms |
744844 KB |
Output is correct |
18 |
Correct |
170 ms |
745064 KB |
Output is correct |
19 |
Correct |
166 ms |
744916 KB |
Output is correct |
20 |
Correct |
170 ms |
744788 KB |
Output is correct |
21 |
Correct |
171 ms |
744788 KB |
Output is correct |
22 |
Correct |
166 ms |
744788 KB |
Output is correct |
23 |
Correct |
169 ms |
744788 KB |
Output is correct |
24 |
Correct |
167 ms |
744752 KB |
Output is correct |
25 |
Correct |
174 ms |
746120 KB |
Output is correct |
26 |
Correct |
178 ms |
745960 KB |
Output is correct |
27 |
Correct |
181 ms |
746024 KB |
Output is correct |
28 |
Correct |
180 ms |
746204 KB |
Output is correct |
29 |
Correct |
182 ms |
746576 KB |
Output is correct |
30 |
Correct |
182 ms |
746492 KB |
Output is correct |
31 |
Correct |
190 ms |
746580 KB |
Output is correct |
32 |
Correct |
184 ms |
746324 KB |
Output is correct |
33 |
Correct |
169 ms |
738388 KB |
Output is correct |
34 |
Correct |
166 ms |
740412 KB |
Output is correct |
35 |
Correct |
171 ms |
744720 KB |
Output is correct |
36 |
Correct |
171 ms |
744672 KB |
Output is correct |
37 |
Correct |
176 ms |
742484 KB |
Output is correct |
38 |
Correct |
321 ms |
793424 KB |
Output is correct |
39 |
Correct |
317 ms |
793404 KB |
Output is correct |
40 |
Correct |
328 ms |
793588 KB |
Output is correct |
41 |
Correct |
314 ms |
793560 KB |
Output is correct |
42 |
Correct |
316 ms |
778372 KB |
Output is correct |
43 |
Correct |
323 ms |
778348 KB |
Output is correct |
44 |
Correct |
324 ms |
778368 KB |
Output is correct |
45 |
Correct |
307 ms |
771084 KB |
Output is correct |
46 |
Correct |
336 ms |
779856 KB |
Output is correct |
47 |
Correct |
347 ms |
780112 KB |
Output is correct |
48 |
Correct |
416 ms |
784348 KB |
Output is correct |
49 |
Correct |
450 ms |
784720 KB |
Output is correct |
50 |
Correct |
288 ms |
772680 KB |
Output is correct |
51 |
Correct |
276 ms |
764500 KB |
Output is correct |
52 |
Correct |
414 ms |
782512 KB |
Output is correct |
53 |
Correct |
405 ms |
782420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
178 ms |
744784 KB |
Output is correct |
2 |
Correct |
173 ms |
744848 KB |
Output is correct |
3 |
Correct |
179 ms |
744800 KB |
Output is correct |
4 |
Correct |
168 ms |
742580 KB |
Output is correct |
5 |
Correct |
180 ms |
744784 KB |
Output is correct |
6 |
Correct |
177 ms |
744792 KB |
Output is correct |
7 |
Correct |
176 ms |
744748 KB |
Output is correct |
8 |
Correct |
185 ms |
744736 KB |
Output is correct |
9 |
Correct |
181 ms |
744852 KB |
Output is correct |
10 |
Correct |
174 ms |
740540 KB |
Output is correct |
11 |
Correct |
175 ms |
740436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
738388 KB |
Output is correct |
2 |
Correct |
166 ms |
740412 KB |
Output is correct |
3 |
Correct |
171 ms |
744720 KB |
Output is correct |
4 |
Correct |
171 ms |
744672 KB |
Output is correct |
5 |
Correct |
176 ms |
742484 KB |
Output is correct |
6 |
Correct |
167 ms |
744648 KB |
Output is correct |
7 |
Correct |
1290 ms |
878420 KB |
Output is correct |
8 |
Correct |
2789 ms |
1024488 KB |
Output is correct |
9 |
Correct |
2825 ms |
1025472 KB |
Output is correct |
10 |
Correct |
2867 ms |
1025740 KB |
Output is correct |
11 |
Correct |
773 ms |
877904 KB |
Output is correct |
12 |
Correct |
1298 ms |
992020 KB |
Output is correct |
13 |
Correct |
1367 ms |
1005648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
320 ms |
744532 KB |
Output is correct |
2 |
Correct |
166 ms |
744524 KB |
Output is correct |
3 |
Correct |
167 ms |
744536 KB |
Output is correct |
4 |
Correct |
169 ms |
744524 KB |
Output is correct |
5 |
Correct |
165 ms |
744712 KB |
Output is correct |
6 |
Correct |
165 ms |
744692 KB |
Output is correct |
7 |
Correct |
173 ms |
744640 KB |
Output is correct |
8 |
Correct |
165 ms |
744712 KB |
Output is correct |
9 |
Correct |
169 ms |
744572 KB |
Output is correct |
10 |
Correct |
175 ms |
744536 KB |
Output is correct |
11 |
Correct |
165 ms |
744528 KB |
Output is correct |
12 |
Correct |
167 ms |
744584 KB |
Output is correct |
13 |
Correct |
164 ms |
738388 KB |
Output is correct |
14 |
Correct |
162 ms |
742480 KB |
Output is correct |
15 |
Correct |
165 ms |
744656 KB |
Output is correct |
16 |
Correct |
164 ms |
744536 KB |
Output is correct |
17 |
Correct |
167 ms |
744844 KB |
Output is correct |
18 |
Correct |
170 ms |
745064 KB |
Output is correct |
19 |
Correct |
166 ms |
744916 KB |
Output is correct |
20 |
Correct |
170 ms |
744788 KB |
Output is correct |
21 |
Correct |
171 ms |
744788 KB |
Output is correct |
22 |
Correct |
166 ms |
744788 KB |
Output is correct |
23 |
Correct |
169 ms |
744788 KB |
Output is correct |
24 |
Correct |
167 ms |
744752 KB |
Output is correct |
25 |
Correct |
174 ms |
746120 KB |
Output is correct |
26 |
Correct |
178 ms |
745960 KB |
Output is correct |
27 |
Correct |
181 ms |
746024 KB |
Output is correct |
28 |
Correct |
180 ms |
746204 KB |
Output is correct |
29 |
Correct |
182 ms |
746576 KB |
Output is correct |
30 |
Correct |
182 ms |
746492 KB |
Output is correct |
31 |
Correct |
190 ms |
746580 KB |
Output is correct |
32 |
Correct |
184 ms |
746324 KB |
Output is correct |
33 |
Correct |
321 ms |
793424 KB |
Output is correct |
34 |
Correct |
317 ms |
793404 KB |
Output is correct |
35 |
Correct |
328 ms |
793588 KB |
Output is correct |
36 |
Correct |
314 ms |
793560 KB |
Output is correct |
37 |
Correct |
316 ms |
778372 KB |
Output is correct |
38 |
Correct |
323 ms |
778348 KB |
Output is correct |
39 |
Correct |
324 ms |
778368 KB |
Output is correct |
40 |
Correct |
307 ms |
771084 KB |
Output is correct |
41 |
Correct |
336 ms |
779856 KB |
Output is correct |
42 |
Correct |
347 ms |
780112 KB |
Output is correct |
43 |
Correct |
416 ms |
784348 KB |
Output is correct |
44 |
Correct |
450 ms |
784720 KB |
Output is correct |
45 |
Correct |
288 ms |
772680 KB |
Output is correct |
46 |
Correct |
276 ms |
764500 KB |
Output is correct |
47 |
Correct |
414 ms |
782512 KB |
Output is correct |
48 |
Correct |
405 ms |
782420 KB |
Output is correct |
49 |
Correct |
178 ms |
744784 KB |
Output is correct |
50 |
Correct |
173 ms |
744848 KB |
Output is correct |
51 |
Correct |
179 ms |
744800 KB |
Output is correct |
52 |
Correct |
168 ms |
742580 KB |
Output is correct |
53 |
Correct |
180 ms |
744784 KB |
Output is correct |
54 |
Correct |
177 ms |
744792 KB |
Output is correct |
55 |
Correct |
176 ms |
744748 KB |
Output is correct |
56 |
Correct |
185 ms |
744736 KB |
Output is correct |
57 |
Correct |
181 ms |
744852 KB |
Output is correct |
58 |
Correct |
174 ms |
740540 KB |
Output is correct |
59 |
Correct |
175 ms |
740436 KB |
Output is correct |
60 |
Correct |
167 ms |
744648 KB |
Output is correct |
61 |
Correct |
1290 ms |
878420 KB |
Output is correct |
62 |
Correct |
2789 ms |
1024488 KB |
Output is correct |
63 |
Correct |
2825 ms |
1025472 KB |
Output is correct |
64 |
Correct |
2867 ms |
1025740 KB |
Output is correct |
65 |
Correct |
773 ms |
877904 KB |
Output is correct |
66 |
Correct |
1298 ms |
992020 KB |
Output is correct |
67 |
Correct |
1367 ms |
1005648 KB |
Output is correct |
68 |
Correct |
169 ms |
738388 KB |
Output is correct |
69 |
Correct |
166 ms |
740412 KB |
Output is correct |
70 |
Correct |
171 ms |
744720 KB |
Output is correct |
71 |
Correct |
171 ms |
744672 KB |
Output is correct |
72 |
Correct |
176 ms |
742484 KB |
Output is correct |
73 |
Runtime error |
1178 ms |
1048576 KB |
Execution killed with signal 9 |
74 |
Halted |
0 ms |
0 KB |
- |