Submission #289543

#TimeUsernameProblemLanguageResultExecution timeMemory
289543DanerZeinRectangles (IOI19_rect)C++14
50 / 100
5051 ms361012 KiB
#include "rect.h"
#include <bits/stdc++.h>
#define frst first
#define scnd second
using namespace std;
#define MAX 1000000000
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef pair<ii,ii> iii;
typedef vector<ii> vii;
vector<vi> b;
int vis[2510][2510];
int Y[4]={-1,0,1,0};
int X[4]={0,-1,0,1};
int c1,r1,c2,r2;
bool sw1=0;
int flod(int y,int x){
  //cout<<y<<" "<<x<<endl;
  c1=min(c1,x);
  r1=min(r1,y);
  c2=max(c2,x);
  r2=max(r2,y);
  vis[y][x]=1;
  int ans=1;
  for(int i=0;i<4;i++){
    if(y+Y[i]<b.size()-1 and y+Y[i]>=1 and x+X[i]<b[0].size()-1 and x+X[i]>=1){
      if(b[y+Y[i]][x+X[i]]==0 and !vis[y+Y[i]][x+X[i]])
      ans+=flod(y+Y[i],x+X[i]);
    }
    else{
      if(b[y+Y[i]][x+X[i]]==0){
	sw1=1;
      }
    }
  }
  return ans;
}
long long count_rectangles(std::vector<std::vector<int> > a) {
  long long c=0;
  if((a.size()<=700 and a[0].size()<=700) or (a.size()<=3 and a[0].size()<=2500)){
    vector<vii> ud,lr;
    ud.resize(a[0].size());
    lr.resize(a.size());
    for(int i=1;i<a[0].size()-1;i++){
      for(int j=0;j<a.size()-1;j++){
	int ma=a[j+1][i];
	for(int k=j+2;k<a.size();k++){
	  if(ma<a[j][i] and ma<a[k][i]){
	    ud[i].push_back(ii(j,k));
	  }
	  ma=max(ma,a[k][i]);
	}
      }
    }
    for(int i=1;i<a.size()-1;i++){
      for(int j=0;j<a[0].size()-1;j++){
	int ma=a[i][j+1];
	for(int k=j+2;k<a[0].size();k++){
	  if(ma<a[i][j] and ma<a[i][k]){
	    lr[i].push_back(ii(j,k));
	  }
	  ma=max(ma,a[i][k]);
	}
      }
    }
    for(int i=1;i<ud.size();i++){
      for(auto &v:ud[i]){
	int u=v.frst,d=v.scnd,l=i-1,r=i+1;
	for(int j=i;ud.size();j++){
	  bool sw=0;
	  if(j!=i){
	    for(auto &x:ud[j]){
	      if(u==x.frst and d==x.scnd){
		sw=1;
		break;
	      }
	    }
	    if(sw==0) break;
	    else r=j+1;
	  }
	  //if(i==1 and j==1) cout<<l<<" "<<r<<" "<<(u-d)-1<<endl;
	  int row=(d-u)-1;
	  for(int k=u+1;k<lr.size();k++){
	    bool sw=0;
	    for(auto &x:lr[k]){
	      if(x.first==l and x.second==r){
		sw=1;
		break;
	      }
	    }
	    if(sw==1) row--;
	    else break;
	    if(row==0) break;
	  }
	  if(row==0) c++;
	}
      }
    }
  }
  else{
    memset(vis,0,sizeof vis);
    b=a;
    for(int i=1;i<a.size()-1;i++){
      for(int j=1;j<a[i].size()-1;j++){
	if(a[i][j]==0 and !vis[i][j]){
	  c1=r1=MAX;
	  c2=r2=-MAX;
	   sw1=0;
	  int ar=flod(i,j);
	  if((abs(c1-c2)+1)*(abs(r1-r2)+1)==ar and sw1==0) c++;
	}
      }
    }
  }
  return c;
}

Compilation message (stderr)

rect.cpp: In function 'int flod(int, int)':
rect.cpp:26:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     if(y+Y[i]<b.size()-1 and y+Y[i]>=1 and x+X[i]<b[0].size()-1 and x+X[i]>=1){
      |        ~~~~~~^~~~~~~~~~~
rect.cpp:26:50: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     if(y+Y[i]<b.size()-1 and y+Y[i]>=1 and x+X[i]<b[0].size()-1 and x+X[i]>=1){
      |                                            ~~~~~~^~~~~~~~~~~~~~
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:44:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |     for(int i=1;i<a[0].size()-1;i++){
      |                 ~^~~~~~~~~~~~~~
rect.cpp:45:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |       for(int j=0;j<a.size()-1;j++){
      |                   ~^~~~~~~~~~~
rect.cpp:47:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |  for(int k=j+2;k<a.size();k++){
      |                ~^~~~~~~~~
rect.cpp:55:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |     for(int i=1;i<a.size()-1;i++){
      |                 ~^~~~~~~~~~~
rect.cpp:56:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |       for(int j=0;j<a[0].size()-1;j++){
      |                   ~^~~~~~~~~~~~~~
rect.cpp:58:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |  for(int k=j+2;k<a[0].size();k++){
      |                ~^~~~~~~~~~~~
rect.cpp:66:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |     for(int i=1;i<ud.size();i++){
      |                 ~^~~~~~~~~~
rect.cpp:83:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |    for(int k=u+1;k<lr.size();k++){
      |                  ~^~~~~~~~~~
rect.cpp:103:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |     for(int i=1;i<a.size()-1;i++){
      |                 ~^~~~~~~~~~~
rect.cpp:104:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |       for(int j=1;j<a[i].size()-1;j++){
      |                   ~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...