Cod sursa(job #1147428)

Utilizator felixiPuscasu Felix felixi Data 19 martie 2014 20:33:23
Problema Struti Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.35 kb
#include <fstream>
#include <deque>

using namespace std;

ifstream in("struti.in");
ofstream out("struti.out");

const int NMAX= 1001;
const int INF = (1<<30)-1;

struct SIR {
  int poz,val;
};

deque <SIR> d_min[NMAX+1], d_max[NMAX+1];
deque <SIR> df_min, df_max;

int v[NMAX+1][NMAX+1];
int N, M, Q;

int Min= INF, cate= 0;

void citeste() {
  for( int i= 1; i<=N; i++ ) {
      for( int j= 1; j<=M; j++ ) {
          in >> v[i][j];
      }
  }
}

void init_col( int lmax ) {
  for( int c= 1;  c<=M;  c++ ) {
      d_min[c].clear();
      d_max[c].clear();
      for( int i= 1;  i<lmax;  i++ ) {
          SIR a;   a.val= v[i][c];   a.poz= i;

          while( !d_min[c].empty() && d_min[c].back().val>v[i][c] ) {
              d_min[c].pop_back();
          }
          d_min[c].push_back( a );

          while( !d_max[c].empty() && d_max[c].back().val<v[i][c] ) {
              d_max[c].pop_back();
          }
          d_max[c].push_back( a );
      }
  }
}

void init_lin( int L ) {
   df_min.clear();
   df_max.clear();
   for( int c= 1;  c<L;  c++ ) {
       SIR a;
               a= d_min[c].front();   a.poz= c;
       while( !df_min.empty() && df_min.back().val > a.val ) {
           df_min.pop_back();
       }
       df_min.push_back( a );

               a= d_max[c].front();   a.poz= c;
       while( !df_max.empty() && df_max.back().val < a.val ) {
           df_max.pop_back();
       }
       df_max.push_back( a );
   }
}


void Rezolva( int x, int y ) {
    //  Inaltime x    Latime y
   init_col( x );
   for( int L= x;  L<=N;  L++ ) {
       for( int c= 1;  c<=M;  c++ ) {
           SIR a;    a.val= v[L][c];   a.poz= L;

           if( d_min[c].front().poz < L-x+1 )  d_min[c].pop_front();
           if( d_max[c].front().poz < L-x+1 )  d_max[c].pop_front();

           while( !d_min[c].empty() && d_min[c].back().val > v[L][c] ) {
               d_min[c].pop_back();
           }
           d_min[c].push_back( a );

           while( !d_max[c].empty() && d_max[c].back().val < v[L][c] ) {
               d_max[c].pop_back();
           }
           d_max[c].push_back( a );

       }

       init_lin( y );

       for( int C= y;  C<=M;  C++ ) {
           if( df_min.front().poz < C-y+1 )  df_min.pop_front();
           if( df_max.front().poz < C-y+1 )  df_max.pop_front();

           SIR a;  a.val= d_min[C].front().val;   a.poz= C;

           while( !df_min.empty() && df_min.back().val > a.val ) {
                df_min.pop_back();
           }
           df_min.push_back( a );

                   a.val= d_max[C].front().val;   a.poz= C;

           while( !df_max.empty() && df_max.back().val < a.val ) {
                df_max.pop_back();
           }
           df_max.push_back( a );

           if( df_max.front().val - df_min.front().val < Min ) {
                Min= df_max.front().val - df_min.front().val;
                cate= 0;
           }
           if( df_max.front().val - df_min.front().val == Min )  ++cate;
       }
   }
}



int main()
{
    in >> N >> M >> Q;
    citeste();
    for( int q= 0; q<Q; q++ ) {
        int x,y;
        in >> x >> y;
        Min= INF;  cate= 0;
        Rezolva( x, y );
        if( x != y ) {
            Rezolva( y, x );
        }
        out << Min << ' ' << cate << '\n';
    }
    return 0;
}