#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;
}
int ans = 0;
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;
}
ans++;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
380 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 |
3 ms |
760 KB |
Output is correct |
6 |
Incorrect |
3 ms |
760 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
380 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 |
3 ms |
760 KB |
Output is correct |
6 |
Incorrect |
3 ms |
760 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
380 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 |
3 ms |
760 KB |
Output is correct |
6 |
Incorrect |
3 ms |
760 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
380 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 |
3 ms |
760 KB |
Output is correct |
6 |
Incorrect |
3 ms |
760 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
888 KB |
Output is correct |
2 |
Correct |
5 ms |
936 KB |
Output is correct |
3 |
Correct |
5 ms |
888 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
888 KB |
Output is correct |
6 |
Correct |
5 ms |
936 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 |
504 KB |
Output is correct |
11 |
Correct |
4 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Incorrect |
1508 ms |
191348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
380 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 |
3 ms |
760 KB |
Output is correct |
6 |
Incorrect |
3 ms |
760 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |