#include "rect.h"
#include "bits/stdc++.h"
#define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
#define s second
#define f first
using namespace std;
typedef pair<int, int> pii;
const int MxN=2510;
int n, m, a[MxN][MxN], cn[MxN][MxN];
pii par[MxN][MxN];
pii fnd(int i, int j) {
if(par[i][j]==pii({i,j})) {
return {i,j};
}
pii ret=fnd(par[i][j].f, par[i][j].s);
return par[i][j]=ret;
}
void mrg(int i, int j, int x, int y) {
pii a=fnd(i, j), b=fnd(x, y);
if(a.f==b.f && a.s==b.s) return;
cn[b.f][b.s]+=cn[a.f][a.s];
par[a.f][a.s]=b;
}
bool st_node(int i, int j, int in) {
if(in==0) return a[i][j]==0 && a[i][j-1]==1 && a[i-1][j]==1 && a[i+1][j]==1;
return a[i][j]==0 && a[i-1][j]==1 && a[i][j-1]==1 && a[i][j+1]==1;
}
bool nd_node(int i, int j, int in) {
if(in==0) return a[i][j]==0 && a[i-1][j]==1 && a[i+1][j]==1 && a[i][j+1]==1;
return a[i][j]==0 && a[i+1][j]==1 && a[i][j-1]==1 && a[i][j+1]==1;
}
bool mid_node(int i, int j, int in) {
if(in==0) return a[i][j]==0 && a[i-1][j]==1 && a[i+1][j]==1;
return a[i][j]==0 && a[i][j-1]==1 && a[i][j+1]==1;
}
bool sqr(int i, int j) {
return a[i][j]==0 && a[i+1][j]==1 && a[i-1][j]==1 && a[i][j-1]==1 && a[i][j+1]==1;
}
bool corn(int i, int j) {
int sm=a[i-1][j]+a[i+1][j]+a[i][j-1]+a[i][j+1];
if(sm!=2) return false;
if(a[i][j]==0 && a[i-1][j]==1 && a[i][j-1]==1) return true;
if(a[i][j]==0 && a[i-1][j]==1 && a[i][j+1]==1) return true;
if(a[i][j]==0 && a[i+1][j]==1 && a[i][j-1]==1) return true;
if(a[i][j]==0 && a[i+1][j]==1 && a[i][j+1]==1) return true;
return false;
}
long long count_rectangles(std::vector<std::vector<int> > A) {
n=A.size(); m=A[0].size();
FOR(i, 0, n) FOR(j, 0, m) a[i][j]=A[i][j];
int ans=0;
FOR(i, 1, n-1) FOR(j, 1, m-1) if(sqr(i, j)) ans++;
FOR(i, 1, n-1) {
bool started=false;
FOR(j, 1, m-1) {
if(st_node(i, j, 0)) {
started=true;
}
else if(nd_node(i, j, 0)) {
if(started) ans++;
}
else if(!mid_node(i, j, 0)) {
started=false;
}
}
}
FOR(j, 1, m-1) {
bool started=false;
FOR(i, 1, n-1) {
if(st_node(i, j, 1)) {
started=true;
}
else if(nd_node(i, j, 1)) {
if(started) ans++;
}
else if(!mid_node(i, j, 1)) {
started=false;
}
}
}
FOR(i, 1, n-1) {
FOR(j, 1, m-1) {
par[i][j]={i,j};
if(corn(i, j)) cn[i][j]++;
if(a[i-1][j]==0) mrg(i, j, i-1, j);
if(a[i][j-1]==0) mrg(i, j, i, j-1);
}
}
map<pii, int> mp;
FOR(i, 1, n-1) {
FOR(j, 1, m-1) {
auto [fst, snd]=fnd(i, j);
mp[{fst,snd}]=cn[fst][snd];
}
}
for(auto it : mp) {
if(it.s==4) ans++;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |