Cod sursa(job #2015948)

Utilizator AlexandruLuchianov1Alex Luchianov AlexandruLuchianov1 Data 28 august 2017 10:59:06
Problema Secventa 2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.09 kb
#include <iostream>
#include <fstream>

using namespace std;
ifstream in ("matrice3.in");
ofstream out ("matrice3.out");
int const nmax = 250;
int lg[1 + nmax];
unsigned char v[1 + nmax][1 + nmax];
unsigned char rmq[10][10][1 + nmax][1 + nmax];///max din dreptunhiul (i , j )-> (i + 2 ^ h1 - 1,j + 2 ^ h2 - 1)
unsigned char square[1 + nmax][1 + nmax];
unsigned char lefty[1 + nmax][1 + nmax];
unsigned char up[1 + nmax][1 + nmax];

int n ,m;
void precompute(){
  for(int i = 2 ; i <= nmax ;i++){
    lg[i] = lg[(i>>1)] + 1;
  }
  for(int i = 1 ; i <= n ;i++){
    for(int j = 1 ; j <= m ;j++){
      if(v[i][j] == 1){
        up[i][j] = 0;
        lefty[i][j] = 0;
      } else{
        up[i][j] = up[i - 1][j] + 1;
        lefty[i][j] = lefty[i][j - 1] + 1;
      }
    }
  }
  for(int i = 1 ; i <= n ;i++){
    for(int j = 1 ; j <= m ;j++){
      if(v[i][j] == 1){
        square[i][j] = 0;
      } else{
        square[i][j] = min((unsigned char)(square[i - 1][j - 1] + 1) , min(lefty[i][j] , up[i][j]) );
      }
    }
  }
}
void computermq(){
  for(int i = 1 ; i <= n ; i++){
    for(int j = 1 ; j <= m ;j++){
      rmq[0][0][i][j] = square[i][j];
      //cout<<square[i][j]<<" ";
    }
    //cout<<'\n';
  }
  //cout<<'\n';
  for(int h1 = 0 ;h1 < 8 ;h1++){
    int lim1 = n - (1 << h1) + 1;
    if(0 < h1){
      for(int i = 1 ; i <= lim1 ;i++){
        for(int j = 1 ; j <= m ;j++){
          rmq[h1][0][i][j] = max(rmq[h1 - 1][0][i][j] ,rmq[h1 - 1][0][i + (1 << (h1 - 1))][j] );
          //cout<<h1<<" "<<0<<" "<<i<<" "<<j<<" "<<rmq[h1][0][i][j]<<'\n';
        }
      }
    }

    for(int h2 = 1 ;h2 < 8 ;h2++){
      int lim2 = m - (1 << h2) + 1;

      for(int i = 1 ; i <= lim1 ;i++){
        for(int j = 1 ; j <= lim2 ;j++){
          rmq[h1][h2][i][j] = max(rmq[h1][h2 - 1][i][j] , rmq[h1][h2 - 1][i][j + (1 << (h2 - 1))] );
          //cout<<h1<<" "<<h2<<" "<<i<<" "<<j<<" "<<rmq[h1][h2][i][j]<<'\n';
        }
      }

    }
  }
}
int test(int x1 , int y1 , int x2 , int y2){///cea mai mare val in dreptunghiul (x1 , y1 )-> (x2 , y2);
  if(x2 < x1 || y2 < y1)
    return 0;
  int h1 = lg[x2 - x1 + 1];
  int h2 = lg[y2 - y1 + 1];
  unsigned char result = max(rmq[h1][h2][x1][y1] , rmq[h1][h2][x2 - (1 << h1) + 1][y1]);
  result = max(result ,rmq[h1][h2][x1][y2 - (1 << h2) + 1] );
  result = max(result ,rmq[h1][h2][x2 - (1 << h1) + 1][y2 - (1 << h2) + 1] );
  //cout<<h1<<" "<<h2<<" "<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<" "<<result<<'\n';
  return result;
}
int x1 , x2 , y1 , y2;

int binarysearch(int from ,int to){
  //cout<<from<<" "<<to<<'\n';
  if(from < to){
    int mid = (from + to) / 2;
    if(mid <= test(x1 + mid - 1, y1 + mid - 1, x2 , y2)){
      return binarysearch(mid + 1 , to);
    } else{
      return binarysearch(from ,mid);
    }
  } else
    return from;
}
int main()
{
  int q;
  in>>n>>m>>q;
  char c;
  for(int i = 1 ; i <= n ;i++){
    for(int j = 1 ; j <= m ;j++){
      in>>c;
      v[i][j] = c - '0';
    }
  }
  precompute();
  computermq();
  for(int i = 0 ; i < q ;i++){
    in>>x1>>y1>>x2>>y2;
    //cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<'\n';
    out<<max(0 , binarysearch(1 , nmax) - 1)<<'\n';
  }
  return 0;
}