#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2500;
int N, M, A[MAXN+10][MAXN+10];
int L[MAXN+10][MAXN+10], R[MAXN+10][MAXN+10], U[MAXN+10][MAXN+10], D[MAXN+10][MAXN+10];
struct Line
{
int y, x1, x2;
bool operator < (const Line &p) const
{
if(y!=p.y) return y<p.y;
if(x1!=p.x1) return x1<p.x1;
return x2<p.x2;
}
bool operator == (const Line &p) { return y==p.y && x1==p.x1 && x2==p.x2; }
bool operator != (const Line &p) { return !(y==p.y && x1==p.x1 && x2==p.x2); }
};
vector<Line> H, V, HB[MAXN+10], VB[MAXN+10];
int PHB[MAXN+10], PVB[MAXN+10];
int lowH[MAXN*MAXN+10], lowV[MAXN*MAXN+10];
ll count_rectangles(vector<vector<int>> _A)
{
int i, j;
N=_A.size(); M=_A[0].size();
for(i=1; i<=N; i++) for(j=1; j<=M; j++) A[i][j]=_A[i-1][j-1];
for(i=1; i<=N; i++)
{
vector<int> S;
S.clear();
for(j=1; j<=M; j++) L[i][j]=0;
for(j=1; j<=M; j++)
{
while(!S.empty() && A[i][S.back()]<=A[i][j]) S.pop_back();
if(!S.empty()) L[i][j]=S.back();
S.push_back(j);
}
S.clear();
for(j=1; j<=M; j++) R[i][j]=M+1;
for(j=M; j>=1; j--)
{
while(!S.empty() && A[i][S.back()]<=A[i][j]) S.pop_back();
if(!S.empty()) R[i][j]=S.back();
S.push_back(j);
}
}
for(i=1; i<=M; i++)
{
vector<int> S;
S.clear();
for(j=1; j<=N; j++) U[j][i]=0;
for(j=1; j<=N; j++)
{
while(!S.empty() && A[S.back()][i]<=A[j][i]) S.pop_back();
if(!S.empty()) U[j][i]=S.back();
S.push_back(j);
}
S.clear();
for(j=1; j<=N; j++) D[j][i]=N+1;
for(j=N; j>=1; j--)
{
while(!S.empty() && A[S.back()][i]<=A[j][i]) S.pop_back();
if(!S.empty()) D[j][i]=S.back();
S.push_back(j);
}
}
for(i=1; i<=N; i++)
{
for(j=1; j<=M; j++)
{
if(L[i][j]==0) continue;
if(R[i][j]==M+1) continue;
H.push_back({i, L[i][j]+1, R[i][j]-1});
}
for(j=1; j<=M; j++)
{
if(U[i][j]==0) continue;
if(D[i][j]==N+1) continue;
V.push_back({j, U[i][j]+1, D[i][j]-1});
}
}
sort(H.begin(), H.end());
H.erase(unique(H.begin(), H.end()), H.end());
sort(V.begin(), V.end());
V.erase(unique(V.begin(), V.end()), V.end());
for(i=H.size()-1, j=H.size()-1; i>=0; i--)
{
lowH[i]=H[i].y;
Line t={H[i].y+1, H[i].x1, H[i].x2};
for(; j>=0 && t<H[j]; j--);
if(j>=0 && t==H[j]) lowH[i]=lowH[j];
}
for(i=V.size()-1, j=V.size()-1; i>=0; i--)
{
lowV[i]=V[i].y;
Line t={V[i].y+1, V[i].x1, V[i].x2};
for(; j>=0 && t<V[j]; j--);
if(j>=0 && t==V[j]) lowV[i]=lowV[j];
}
for(auto it : H) HB[it.y].push_back(it);
for(auto it : V) VB[it.y].push_back(it);
for(i=1; i<=N; i++) PHB[i]=PHB[i-1]+HB[i].size();
for(i=1; i<=M; i++) PVB[i]=PVB[i-1]+VB[i].size();
vector<pair<pii, pii>> ans;
for(i=2; i<=N-1; i++) for(j=2; j<=M-1; j++)
{
if(L[i][j]==0) continue;
if(R[i][j]==M+1) continue;
if(U[i][j]==0) continue;
if(D[i][j]==N+1) continue;
Line tu={U[i][j]+1, L[i][j]+1, R[i][j]-1};
Line tl={L[i][j]+1, U[i][j]+1, D[i][j]-1};
auto it=lower_bound(HB[tu.y].begin(), HB[tu.y].end(), tu);
auto jt=lower_bound(VB[tl.y].begin(), VB[tl.y].end(), tl);
if(it!=HB[tu.y].end() && *it!=tu) continue;
if(jt!=VB[tl.y].end() && *jt!=tl) continue;
if(lowH[it-HB[tu.y].begin()+PHB[tu.y-1]]<D[i][j]-1) continue;
if(lowV[jt-VB[tl.y].begin()+PVB[tl.y-1]]<R[i][j]-1) continue;
ans.push_back({pii(L[i][j]+1, R[i][j]-1), pii(U[i][j]+1, D[i][j]-1)});
}
sort(ans.begin(), ans.end());
ans.erase(unique(ans.begin(), ans.end()), ans.end());
return ans.size();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
1272 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1144 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Incorrect |
3 ms |
1148 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
1272 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1144 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Incorrect |
3 ms |
1148 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
1272 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1144 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Incorrect |
3 ms |
1148 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
1272 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1144 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Incorrect |
3 ms |
1148 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1272 KB |
Output is correct |
2 |
Correct |
4 ms |
1144 KB |
Output is correct |
3 |
Correct |
3 ms |
760 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Incorrect |
5 ms |
1016 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
1272 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1144 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Incorrect |
3 ms |
1148 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |