# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
155872 | vanic | Rectangles (IOI19_rect) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "rect.h"
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <map>
#include <stack>
#include <set>
#include <array>
using namespace std;
typedef long long ll;
const int maxn=2503;
int n, m;
int l[maxn][maxn];
//vector < int > r[maxn][maxn], c[maxn][maxn];
unordered_map < int, int > r[maxn], c[maxn];
int rind[maxn][maxn], cind[maxn][maxn];
pair < int, int > red[maxn][maxn], stup[maxn][maxn];
int bio2[maxn][maxn], bio3[maxn][maxn];
//vector < array < int, 4 > > bio;
unordered_map < ll, bool > bio;
inline int hashiraj(int b, int c){
return b*maxn+c;
}
inline ll hashah(ll a, ll b, ll c, ll d){
return a*maxn*maxn*maxn+b*maxn*maxn+c*maxn+d;
}
void upd1(int x){
stack < pair < int, int > > q;
q.push(make_pair(1e8, -1));
for(int i=0; i<m; i++){
while(q.top().first<=l[x][i]){
q.pop();
}
red[x][i].first=q.top().second+1;
q.push(make_pair(l[x][i], i));
}
while(!q.empty()){
q.pop();
}
q.push(make_pair(1e8, m));
for(int i=m-1; i>-1; i--){
while(q.top().first<=l[x][i]){
q.pop();
}
red[x][i].second=q.top().second-1;
if(bio2[red[x][i].first][red[x][i].second]!=x+1){
bio2[red[x][i].first][red[x][i].second]=x+1;
rind[red[x][i].first][red[x][i].second]++;
r[red[x][i].first][hashiraj(red[x][i].second, x)]=rind[red[x][i].first][red[x][i].second];
}
q.push(make_pair(l[x][i], i));
}
}
void upd2(int x){
stack < pair < int, int > > q;
q.push(make_pair(1e8, -1));
for(int i=0; i<n; i++){
while(q.top().first<=l[i][x]){
q.pop();
}
stup[i][x].first=q.top().second+1;
q.push(make_pair(l[i][x], i));
}
while(!q.empty()){
q.pop();
}
q.push(make_pair(1e8, n));
for(int i=n-1; i>-1; i--){
while(q.top().first<=l[i][x]){
q.pop();
}
stup[i][x].second=q.top().second-1;
if(bio3[stup[i][x].first][stup[i][x].second]!=x+1){
bio3[stup[i][x].first][stup[i][x].second]=x+1;
cind[stup[i][x].first][stup[i][x].second]++;
c[stup[i][x].first][hashiraj(stup[i][x].second, x)]=cind[stup[i][x].first][stup[i][x].second];
}
q.push(make_pair(l[i][x], i));
}
}
bool provjeri(int x, int y){
int r1, r2, c1, c2;
r1=red[x][y].first;
r2=red[x][y].second;
c1=stup[x][y].first;
c2=stup[x][y].second;
if(r1==0 || c1==0 || r2==m-1 || c2==n-1){
return 0;
}
if(r[r1][hashiraj(r2, c2)]!=0 && r[r1][hashiraj(r2, c1)]!=0
&& c[c1][hashiraj(c2, r2)]!=0 && c[c1][hashiraj(c2, r1)]!=0
&& r[r1][hashiraj(r2, c2)]-r[r1][hashiraj(r2, c1)]==c2-c1 && c[c1][hashiraj(c2, r2)]-c[c1][hashiraj(c2, r1)]==r2-r1){
if(!bio[hashah(r1, r2, c1, c2)]){
bio[hashah(r1, r2, c1, c2)]=1;
return 1;
}
}
return 0;
}
long long count_rectangles(vector < vector < int > > a){
n=a.size();
m=a[0].size();
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
l[i][j]=a[i][j];
}
}
for(int i=0; i<n; i++){
upd1(i);
}
for(int i=0; i<m; i++){
upd2(i);
}
int br=0;
for(int i=1; i<n-1; i++){
for(int j=1; j<m-1; j++){
br+=provjeri(i, j);
}
}
return br;
}
int main(){
scanf("%d%d", &n, &m);
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
scanf("%d", &l[i][j]);
}
}
for(int i=0; i<n; i++){
upd1(i);
}
for(int i=0; i<m; i++){
upd2(i);
}
int br=0;
for(int i=1; i<n-1; i++){
for(int j=1; j<m-1; j++){
br+=provjeri(i, j);
}
}
printf("%d\n", br);
return 0;
}