This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool check(ll i,ll j,ll n,ll m,ll u,ll d,ll l,ll r,vector<vector<int>>&a){
if(i<=0||j<=0||i>=(n-1)||j>=(m-1)) return false;
if(a[i][j]<=l&&a[i][j]<=r&&a[i][j]<=u&&a[i][j]<=d) return true;
return false;
}
bool c1(ll i,ll j,ll n,ll m){
if(i<=0||j<=0||i>=(n-1)||j>=(m-1)) return false;
return true;
}
long long count_rectangles(std::vector<std::vector<int> > a) {
ll n = a.size();
ll m = a[0].size();
vector<vector<vector<ll>>> arr(n+10,vector<vector<ll>>(m+10,vector<ll>(4,0)));
ll cnt = 0;
map<ll,map<ll,ll>> hash;
queue<vector<ll>> pq;
// Row Col U D L R
for(ll i=0;i<n;i++){
for(ll j=0;j<n;j++){
arr[i][j][0]=a[i][j];
arr[i][j][1]=a[i][j];
arr[i][j][2]=a[i][j];
arr[i][j][3]=a[i][j];
}
}
for(ll i=1;i<n-1;i++){
for(ll j=1;j<m-1;j++){
arr[i][j][0]=a[i-1][j],arr[i][j][1]=a[i+1][j],arr[i][j][2]=a[i][j-1],arr[i][j][3]=a[i][j+1];
if(a[i][j]<=a[i+1][j]&&a[i][j]<=a[i-1][j]&&a[i][j]<=a[i][j+1]&&a[i][j]<=a[i][j-1]){
vector<ll> vec = {i,j};
pq.push(vec);
hash[i][j]=1;
}
}
}
vector<ll> dr = {-1,0,1,0};
vector<ll> dc = {0,-1,0,1};
ll ans = 0;
while(!pq.empty()){
vector<ll> v = pq.front();
pq.pop();
ans++;
//cout<<v[0]<<" "<<v[1]<<endl;
for(ll i=0;i<4;i++){
ll nr = v[0]+dr[i];
ll nc = v[1]+dc[i];
vector<ll> nv = v;
if(c1(nr,nc,n,m)==false){
continue;
}
ll left,right,up,down;
left = arr[nr][nc-1][2];
right = arr[nr][nc+1][3];
up = arr[nr-1][nc][0];
down = arr[nr+1][nc][1];
if(check(nr,nc,n,m,up,down,left,right,a)){
if(!hash[nr][nc]){
hash[nr][nc]=1;
arr[nr][nc][0]=up;
arr[nr][nc][1]=down;
arr[nr][nc][2]=left;
arr[nr][nc][3]=right;
nv[0]=nr;
nv[1]=nc;
pq.push(nv);
}
}
}
}
return ans;
}
Compilation message (stderr)
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:21:8: warning: unused variable 'cnt' [-Wunused-variable]
21 | ll cnt = 0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |