#include "rect.h"
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int N = 2510;
const int inf = 1e8;
int v[N][N];
int p1[N][N];
int p2[N][N];
int p3[N][N];
int p4[N][N];
struct Segtree{
int tree[4*N];
int join(int a, int b){
return min(a, b);
}
void build(int node, int l, int r){
if(l == r){
tree[node] = inf;
return;
}
int mid = (l+r)/2;
build(2*node, l, mid);
build(2*node+1, mid+1, r);
tree[node] = join(tree[2*node], tree[2*node+1]);
return;
}
void update(int node, int l, int r, int pos, int val){
if(l == r){
tree[node] = val;
return;
}
int mid = (l+r)/2;
if(l <= pos and pos <= mid) update(2*node, l, mid, pos, val);
else update(2*node+1, mid+1, r, pos, val);
tree[node] = join(tree[2*node], tree[2*node+1]);
return;
}
int query(int node, int l, int r, int tl, int tr){
if(l <= tl and tr <= r) return tree[node];
if(l > tr or tl > r) return inf;
int mid = (tl+tr)/2;
return join(query(2*node, l, r, tl, mid), query(2*node+1, l, r, mid+1, tr));
}
} seg1[N], seg3[N];
struct Segtree2{
int tree[4*N];
int join(int a, int b){
return max(a, b);
}
void build(int node, int l, int r){
if(l == r){
tree[node] = -1;
return;
}
int mid = (l+r)/2;
build(2*node, l, mid);
build(2*node+1, mid+1, r);
tree[node] = join(tree[2*node], tree[2*node+1]);
return;
}
void update(int node, int l, int r, int pos, int val){
if(l == r){
tree[node] = val;
return;
}
int mid = (l+r)/2;
if(l <= pos and pos <= mid) update(2*node, l, mid, pos, val);
else update(2*node+1, mid+1, r, pos, val);
tree[node] = join(tree[2*node], tree[2*node+1]);
return;
}
int query(int node, int l, int r, int tl, int tr){
if(l <= tl and tr <= r) return tree[node];
if(l > tr or tl > r) return -1;
int mid = (tl+tr)/2;
return join(query(2*node, l, r, tl, mid), query(2*node+1, l, r, mid+1, tr));
}
} seg2[N], seg4[N];
set <array <int, 4>> s;
int n, m;
bool check(int r1, int c1, int r2, int c2){
if(s.find({r1, c1, r2, c2}) != s.end()) return false;
if(r1 == 0 or r2 == n+1 or r2-r1 <= 1) return false;
if(c1 == 0 or c2 == m+1 or c2-c1 <= 1) return false;
if(r1+1 > r2-1 or c1+1 > c2-1) return false;
if(c1 <= 0 or c1 > m or c2 <= 0 or c2 > m or r1 <= 0 or r1 > n or r2 <= 0 or r2 > n) return false;
int x1 = seg1[c1].query(1,r1+1, r2-1, 1, n);
if(c1+1 <= x1 and x1 <= c2-1) return false;
x1 = seg2[c2].query(1, r1+1, r2-1, 1, n);
if(c1 < x1 and x1 < c2) return false;
int x2 = seg3[r1].query(1, c1+1, c2-1, 1, m);
if(r1 < x2 and x2 < r2) return false;
x2 = seg4[r2].query(1, c1+1, c2-1, 1, m);
if(r1 < x2 and x2 < r2) return false;
s.insert({r1, c1, r2, c2});
return true;
}
long long count_rectangles(std::vector<std::vector<int32_t> > a) {
n = a.size(), m = a[0].size();
s.clear();
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
v[i][j] = a[i-1][j-1];
}
}
for(int i = 1;i <= n;i++){
stack <int> s;
for(int j = 1;j <= m;j++){
if(s.empty()) s.push(j);
else{
while(!s.empty()){
int t = s.top();
if(v[i][j] >= v[i][t]){
p1[i][t] = j;
s.pop();
}
else{
break;
}
}
s.push(j);
}
}
while(!s.empty()){
int t = s.top();
p1[i][t] = m+1;
s.pop();
}
}
for(int i = 1;i <= n;i++){
stack <int> s;
for(int j = m;j > 0;j--){
if(s.empty()) s.push(j);
else{
while(!s.empty()){
int t = s.top();
if(v[i][j] >= v[i][t]){
p2[i][t] = j;
s.pop();
}
else{
break;
}
}
s.push(j);
}
}
while(!s.empty()){
int t = s.top();
p2[i][t] = 0;
s.pop();
}
}
for(int j = 1;j <= m;j++){
stack <int> s;
for(int i = 1;i <= n;i++){
if(s.empty()) s.push(i);
else{
while(!s.empty()){
int t = s.top();
if(v[i][j] >= v[t][j]){
p3[t][j] = i;
s.pop();
}
else{
break;
}
}
s.push(i);
}
}
while(!s.empty()){
int t = s.top();
p3[t][j] = n+1;
s.pop();
}
}
for(int j = 1;j <= m;j++){
stack <int> s;
for(int i = n;i > 0;i--){
if(s.empty()) s.push(i);
else{
while(!s.empty()){
int t = s.top();
if(v[i][j] >= v[t][j]){
p4[t][j] = i;
s.pop();
}
else{
break;
}
}
s.push(i);
}
}
while(!s.empty()){
int t = s.top();
p4[t][j] = 0;
s.pop();
}
}
for(int i = 1;i <= m;i++){
seg1[i].build(1, 1, n);
seg2[i].build(1, 1, n);
}
for(int i = 1;i <= n;i++){
seg3[i].build(1, 1, m);
seg4[i].build(1, 1, m);
}
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
seg1[j].update(1, 1, n, i, p1[i][j]);
seg2[j].update(1, 1, n, i, p2[i][j]);
seg3[i].update(1, 1, m, j, p3[i][j]);
seg4[i].update(1, 1, m, j, p4[i][j]);
}
}
for(int r1 = 1;r1 < n;r1++){
for(int c1 = 1;c1 < m;c1++){
int r2 = p3[r1][c1+1];
int c2 = (1 <= r1+1 and r1+1 <= r2-1 and r2-1 <= n ? seg1[c1].query(1, r1+1, r2-1, 1, n) : -1);
check(r1, c1, r2, c2);
c2 = p1[r1+1][c1];
r2 = (1 <= c1+1 and c1+1 <= c2-1 and c2-1 <= m ? seg3[r1].query(1, c1+1, c2-1, 1, m) : -1);
check(r1, c1, r2, c2);
}
}
for(int r1 = 1;r1 < n;r1++){
for(int c2 = m;c2 > 1;c2--){
int c1 = p2[r1+1][c2];
int r2 = (1 <= c1+1 and c1+1 <= c2-1 and c2-1 <= m ? seg3[r1].query(1, c1+1, c2-1, 1, m) : -1);
check(r1, c1, r2, c2);
r2 = p3[r1][c2-1];
c1 = (1 <= r1+1 and r1+1 <= r2-1 and r2-1 <= n ? seg2[c2].query(1, r1+1, r2-1, 1, n) : -1);
check(r1, c1, r2, c2);
}
}
for(int r2 = n;r2 > 1;r2--){
for(int c1 = 1;c1 < m;c1++){
int r1 = p4[r2][c1+1];
int c2 = (1 <= r1+1 and r1+1 <= r2-1 and r2-1 <= n ? seg1[c1].query(1, r1+1, r2-1, 1, n) : -1);
check(r1, c1, r2, c2);
c2 = p1[r2-1][c1];
r1 = (1 <= c1+1 and c1+1 <= c2-1 and c2-1 <= m ? seg4[r2].query(1, c1+1, c2-1, 1, m) : -1);
check(r1, c1, r2, c2);
/*
int c2 = p1[r2-1][c1];
int r1 = p4[r2][c1+1];
check(r1, c1, r2, c2);
*/
}
}
for(int r2 = n;r2 > 1;r2--){
for(int c2 = m;c2 > 1;c2--){
int c1 = p2[r2-1][c2];
int r1 = (1 <= c1+1 and c1+1 <= c2-1 and c2-1 <= m ? seg4[r2].query(1, c1+1, c2-1, 1, m) : -1);
check(r1, c1, r2, c2);
r1 = p4[r2][c2-1];
c1 = (1 <= r1+1 and r1+1 <= r2-1 and r2-1 <= n ? seg2[c2].query(1, r1+1, r2-1, 1, n) : -1);
check(r1, c1, r2, c2);
/*
int c1 = p2[r2-1][c2];
int r1 = p4[r2][c2-1];
check(r1, c1, r2, c2);
*/
}
}
return s.size();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
1624 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
1368 KB |
Output is correct |
10 |
Correct |
1 ms |
1628 KB |
Output is correct |
11 |
Correct |
1 ms |
1628 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 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 |
1 ms |
1372 KB |
Output is correct |
20 |
Correct |
1 ms |
1372 KB |
Output is correct |
21 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
1624 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
1368 KB |
Output is correct |
10 |
Correct |
1 ms |
1628 KB |
Output is correct |
11 |
Correct |
1 ms |
1628 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 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 |
1 ms |
1372 KB |
Output is correct |
20 |
Correct |
1 ms |
1372 KB |
Output is correct |
21 |
Correct |
0 ms |
604 KB |
Output is correct |
22 |
Correct |
7 ms |
4188 KB |
Output is correct |
23 |
Correct |
7 ms |
4188 KB |
Output is correct |
24 |
Correct |
8 ms |
4184 KB |
Output is correct |
25 |
Correct |
5 ms |
3676 KB |
Output is correct |
26 |
Correct |
6 ms |
3932 KB |
Output is correct |
27 |
Correct |
7 ms |
3964 KB |
Output is correct |
28 |
Correct |
6 ms |
3932 KB |
Output is correct |
29 |
Correct |
3 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
1624 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
1368 KB |
Output is correct |
10 |
Correct |
1 ms |
1628 KB |
Output is correct |
11 |
Correct |
1 ms |
1628 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
7 ms |
4188 KB |
Output is correct |
18 |
Correct |
7 ms |
4188 KB |
Output is correct |
19 |
Correct |
8 ms |
4184 KB |
Output is correct |
20 |
Correct |
5 ms |
3676 KB |
Output is correct |
21 |
Correct |
6 ms |
3932 KB |
Output is correct |
22 |
Correct |
7 ms |
3964 KB |
Output is correct |
23 |
Correct |
6 ms |
3932 KB |
Output is correct |
24 |
Correct |
3 ms |
3164 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
1372 KB |
Output is correct |
28 |
Correct |
1 ms |
1372 KB |
Output is correct |
29 |
Correct |
0 ms |
604 KB |
Output is correct |
30 |
Correct |
50 ms |
12832 KB |
Output is correct |
31 |
Correct |
47 ms |
12632 KB |
Output is correct |
32 |
Correct |
50 ms |
12628 KB |
Output is correct |
33 |
Correct |
26 ms |
10584 KB |
Output is correct |
34 |
Correct |
38 ms |
11092 KB |
Output is correct |
35 |
Correct |
46 ms |
11036 KB |
Output is correct |
36 |
Correct |
40 ms |
11092 KB |
Output is correct |
37 |
Correct |
43 ms |
11092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
1624 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
1368 KB |
Output is correct |
10 |
Correct |
1 ms |
1628 KB |
Output is correct |
11 |
Correct |
1 ms |
1628 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
7 ms |
4188 KB |
Output is correct |
18 |
Correct |
7 ms |
4188 KB |
Output is correct |
19 |
Correct |
8 ms |
4184 KB |
Output is correct |
20 |
Correct |
5 ms |
3676 KB |
Output is correct |
21 |
Correct |
6 ms |
3932 KB |
Output is correct |
22 |
Correct |
7 ms |
3964 KB |
Output is correct |
23 |
Correct |
6 ms |
3932 KB |
Output is correct |
24 |
Correct |
3 ms |
3164 KB |
Output is correct |
25 |
Correct |
50 ms |
12832 KB |
Output is correct |
26 |
Correct |
47 ms |
12632 KB |
Output is correct |
27 |
Correct |
50 ms |
12628 KB |
Output is correct |
28 |
Correct |
26 ms |
10584 KB |
Output is correct |
29 |
Correct |
38 ms |
11092 KB |
Output is correct |
30 |
Correct |
46 ms |
11036 KB |
Output is correct |
31 |
Correct |
40 ms |
11092 KB |
Output is correct |
32 |
Correct |
43 ms |
11092 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
1372 KB |
Output is correct |
36 |
Correct |
1 ms |
1372 KB |
Output is correct |
37 |
Correct |
0 ms |
604 KB |
Output is correct |
38 |
Correct |
258 ms |
61700 KB |
Output is correct |
39 |
Correct |
265 ms |
61676 KB |
Output is correct |
40 |
Correct |
263 ms |
61524 KB |
Output is correct |
41 |
Correct |
223 ms |
61612 KB |
Output is correct |
42 |
Correct |
861 ms |
92248 KB |
Output is correct |
43 |
Correct |
892 ms |
92016 KB |
Output is correct |
44 |
Correct |
872 ms |
92068 KB |
Output is correct |
45 |
Correct |
834 ms |
87096 KB |
Output is correct |
46 |
Correct |
451 ms |
62924 KB |
Output is correct |
47 |
Correct |
508 ms |
64596 KB |
Output is correct |
48 |
Correct |
738 ms |
72044 KB |
Output is correct |
49 |
Correct |
764 ms |
71924 KB |
Output is correct |
50 |
Correct |
340 ms |
45988 KB |
Output is correct |
51 |
Correct |
336 ms |
38904 KB |
Output is correct |
52 |
Correct |
707 ms |
72024 KB |
Output is correct |
53 |
Correct |
719 ms |
72020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
21336 KB |
Output is correct |
2 |
Correct |
10 ms |
18268 KB |
Output is correct |
3 |
Correct |
9 ms |
21084 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
11 ms |
21340 KB |
Output is correct |
6 |
Correct |
11 ms |
21340 KB |
Output is correct |
7 |
Correct |
11 ms |
21340 KB |
Output is correct |
8 |
Correct |
11 ms |
21084 KB |
Output is correct |
9 |
Correct |
11 ms |
21336 KB |
Output is correct |
10 |
Correct |
8 ms |
20828 KB |
Output is correct |
11 |
Correct |
9 ms |
21084 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 |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
856 KB |
Output is correct |
7 |
Correct |
2947 ms |
272544 KB |
Output is correct |
8 |
Execution timed out |
5077 ms |
546292 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
1624 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
1368 KB |
Output is correct |
10 |
Correct |
1 ms |
1628 KB |
Output is correct |
11 |
Correct |
1 ms |
1628 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
7 ms |
4188 KB |
Output is correct |
18 |
Correct |
7 ms |
4188 KB |
Output is correct |
19 |
Correct |
8 ms |
4184 KB |
Output is correct |
20 |
Correct |
5 ms |
3676 KB |
Output is correct |
21 |
Correct |
6 ms |
3932 KB |
Output is correct |
22 |
Correct |
7 ms |
3964 KB |
Output is correct |
23 |
Correct |
6 ms |
3932 KB |
Output is correct |
24 |
Correct |
3 ms |
3164 KB |
Output is correct |
25 |
Correct |
50 ms |
12832 KB |
Output is correct |
26 |
Correct |
47 ms |
12632 KB |
Output is correct |
27 |
Correct |
50 ms |
12628 KB |
Output is correct |
28 |
Correct |
26 ms |
10584 KB |
Output is correct |
29 |
Correct |
38 ms |
11092 KB |
Output is correct |
30 |
Correct |
46 ms |
11036 KB |
Output is correct |
31 |
Correct |
40 ms |
11092 KB |
Output is correct |
32 |
Correct |
43 ms |
11092 KB |
Output is correct |
33 |
Correct |
258 ms |
61700 KB |
Output is correct |
34 |
Correct |
265 ms |
61676 KB |
Output is correct |
35 |
Correct |
263 ms |
61524 KB |
Output is correct |
36 |
Correct |
223 ms |
61612 KB |
Output is correct |
37 |
Correct |
861 ms |
92248 KB |
Output is correct |
38 |
Correct |
892 ms |
92016 KB |
Output is correct |
39 |
Correct |
872 ms |
92068 KB |
Output is correct |
40 |
Correct |
834 ms |
87096 KB |
Output is correct |
41 |
Correct |
451 ms |
62924 KB |
Output is correct |
42 |
Correct |
508 ms |
64596 KB |
Output is correct |
43 |
Correct |
738 ms |
72044 KB |
Output is correct |
44 |
Correct |
764 ms |
71924 KB |
Output is correct |
45 |
Correct |
340 ms |
45988 KB |
Output is correct |
46 |
Correct |
336 ms |
38904 KB |
Output is correct |
47 |
Correct |
707 ms |
72024 KB |
Output is correct |
48 |
Correct |
719 ms |
72020 KB |
Output is correct |
49 |
Correct |
11 ms |
21336 KB |
Output is correct |
50 |
Correct |
10 ms |
18268 KB |
Output is correct |
51 |
Correct |
9 ms |
21084 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
11 ms |
21340 KB |
Output is correct |
54 |
Correct |
11 ms |
21340 KB |
Output is correct |
55 |
Correct |
11 ms |
21340 KB |
Output is correct |
56 |
Correct |
11 ms |
21084 KB |
Output is correct |
57 |
Correct |
11 ms |
21336 KB |
Output is correct |
58 |
Correct |
8 ms |
20828 KB |
Output is correct |
59 |
Correct |
9 ms |
21084 KB |
Output is correct |
60 |
Correct |
1 ms |
856 KB |
Output is correct |
61 |
Correct |
2947 ms |
272544 KB |
Output is correct |
62 |
Execution timed out |
5077 ms |
546292 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |