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 <cstdio>
#include <unistd.h>
#include <cassert>
#include <string>
#include<bits/stdc++.h>
using namespace std;
int treemax[10001];
int treemin[10001];
void updatemax(int node,int start,int end,int idx,int val){
if(start==end){
treemax[node]=val;
}else{
int mid=(start+end)/2;
if(start<=idx and idx<=mid){
updatemax(2*node,start,mid,idx,val);
}else{
updatemax(2*node+1,mid+1,end,idx,val);
}
treemax[node]=max(treemax[2*node],treemax[2*node+1]);
}
}
void updatemin(int node,int start,int end,int idx,int val){
if(start==end){
treemin[node]=val;
}else{
int mid=(start+end)/2;
if(start<=idx and idx<=mid){
updatemin(2*node,start,mid,idx,val);
}else{
updatemin(2*node+1,mid+1,end,idx,val);
}
treemin[node]=min(treemin[2*node],treemin[2*node+1]);
}
}
int querymax(int node,int start,int end,int l,int r){
if(l>end or r<start){
return 0;
}
if(l<=start and end<=r){
return treemax[node];
}
int mid=(start+end)/2;
return max(querymax(2*node,start,mid,l,r),querymax(2*node+1,mid+1,end,l,r));
}
int querymin(int node,int start,int end,int l,int r){
if(l>end or r<start){
return 1e9;
}
if(l<=start and end<=r){
return treemin[node];
}
int mid=(start+end)/2;
return min(querymin(2*node,start,mid,l,r),querymin(2*node+1,mid+1,end,l,r));
}
long long count_rectangles(std::vector<std::vector<int> > a) {
int m=a[0].size();
int n=a.size();
if(n<=2 or m<=2){
return 0;
}
if(n<=3){
for(int i=0;i<m;i++){
if(a[1][i]<a[0][i] and a[1][i]<a[2][i]){
updatemin(1,0,m-1,i,1);
}
updatemax(1,0,m-1,i,a[1][i]);
}
int ans=0;
for(int j=1;j<(m-1);j++){
for(int k=j;k<(m-1);k++){
int x=querymin(1,0,m-1,j,k);
if(x==1){
int y=querymax(1,0,m-1,j,k);
if(y<a[1][j-1] and y<a[1][k+1]){
ans++;
}
}
}
}
return ans;
}else{
int ans=0;
int pref[m][n];
int dolje[n][m];
int desno[n][m];
for(int i=0;i<n;i++){
for(int j=m-1;j>=0;j--){
if(a[i][j]==1){
desno[i][j]=j;
}else{
if(j==(m-1)){
desno[i][j]=-1;
}else{
desno[i][j]=desno[i][j+1];
}
}
}
}
for(int j=0;j<m;j++){
for(int i=n-1;i>=0;i--){
if(a[i][j]==1){
dolje[i][j]=i;
}else{
if(i==(n-1)){
dolje[i][j]=-1;
}else{
dolje[i][j]=dolje[i+1][j];
}
}
}
}
for(int j=0;j<m;j++){
for(int i=0;i<n;i++){
if(i==0){
pref[j][i]=a[i][j];
}else{
pref[j][i]=pref[j][i-1]+a[i][j];
}
}
}
for(int i=1;i<(n-1);i++){
for(int j=1;j<(m-1);j++){
if(a[i-1][j]==1 and a[i][j]==0 and a[i][j-1]==1){
int t1=0;
int i1=dolje[i][j];
int j1=desno[i][j];
for(int k=i;k<dolje[i][j];k++){
for(int t=j;t<desno[i][j];t++){
if(a[k][t]!=0){
t1++;
break;
}
if(a[i-1][t]!=1 or a[i1][t]!=1){
t1++;
break;
}
if(a[k][j-1]!=1 or a[k][j1]!=1){
t1++;
break;
}
}
}
if(t1==0){
ans++;
}
}
}
}
return ans;
}
}
# | 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... |