Pagini recente » Cod sursa (job #1214125) | Cod sursa (job #3252580) | Cod sursa (job #1021529) | Cod sursa (job #1325099) | Cod sursa (job #2145629)
#include<cstdio>
#include<vector>
#include<deque>
using namespace std;
FILE * fin = fopen( "struti.in", "r" );
FILE * fout = fopen( "struti.out", "w" );
int N, M, P, A[1005][1005], MAX[1005][1005], MIN[1005][1005];
const int SZ = 100000;
int pos = 0;
char buffer[SZ];
deque<int> D[1005], d;
vector<int> V;
inline void solve_MAX( int X, int Y ){
for( int j = 1; j <= M; j++ )
D[j].clear();
for( int j = 1; j <= M; j++ ){
for( int i = 1; i < X; i++ ){
while( !D[j].empty() && A[ D[j].back() ][j] < A[i][j] )
D[j].pop_back();
D[j].push_back( i );
}
}
for( int i = X; i <= N; i++ ){
V.clear();
for( int j = 1; j <= M; j++ ){
while( !D[j].empty() && A[ D[j].back() ][j] < A[i][j] )
D[j].pop_back();
D[j].push_back( i );
if( i - D[j].front() == X )
D[j].pop_front();
V.push_back( A[ D[j].front() ][j] );
}
d.clear();
for( int j = 1; j < Y; j++ ){
while( !d.empty() && V[ d.back() - 1 ] < V[j - 1] )
d.pop_back();
d.push_back( j );
}
for( int j = Y; j <= M; j++ ){
while( !d.empty() && V[ d.back() - 1 ] < V[j - 1] )
d.pop_back();
d.push_back( j );
if( j - d.front() == Y )
d.pop_front();
MAX[i][j] = V[ d.front() - 1 ];
}
}
return;
}
inline void solve_MIN( int X, int Y ){
for( int j = 1; j <= M; j++ )
D[j].clear();
for( int j = 1; j <= M; j++ ){
for( int i = 1; i < X; i++ ){
while( !D[j].empty() && A[ D[j].back() ][j] > A[i][j] )
D[j].pop_back();
D[j].push_back( i );
}
}
for( int i = X; i <= N; i++ ){
V.clear();
for( int j = 1; j <= M; j++ ){
while( !D[j].empty() && A[ D[j].back() ][j] > A[i][j] )
D[j].pop_back();
D[j].push_back( i );
if( i - D[j].front() == X )
D[j].pop_front();
V.push_back( A[ D[j].front() ][j] );
}
d.clear();
for( int j = 1; j < Y; j++ ){
while( !d.empty() && V[ d.back() - 1 ] > V[j - 1] )
d.pop_back();
d.push_back( j );
}
for( int j = Y; j <= M; j++ ){
while( !d.empty() && V[ d.back() - 1 ] > V[j - 1] )
d.pop_back();
d.push_back( j );
if( j - d.front() == Y )
d.pop_front();
MIN[i][j] = V[ d.front() - 1 ];
}
}
return;
}
inline pair<int,int> solve( int X, int Y ){
solve_MAX( X, Y );
solve_MIN( X, Y );
pair<int,int> Sol;
Sol.first = (1<<15) - 1;
Sol.second = 0;
for( int i = X; i <= N; i++ ){
for( int j = Y; j <= M; j++ ){
if( Sol.first > MAX[i][j] - MIN[i][j] ){
Sol.first = MAX[i][j] - MIN[i][j];
Sol.second = 1;
}else{
if( Sol.first == MAX[i][j] - MIN[i][j] )
Sol.second++;
}
}
}
return Sol;
}
inline void read( int &numar ){
numar = 0;
while( buffer[pos] < '0' || buffer[pos] > '9' ){
pos++;
if( pos == SZ )
fread( buffer, 1, SZ, fin ), pos = 0;
}
while( buffer[pos] >= '0' && buffer[pos] <= '9' ){
numar = numar * 10 + ( buffer[pos] - '0' );
pos++;
if( pos == SZ )
fread( buffer, 1, SZ, fin ), pos = 0;
}
}
int main(){
fread( buffer, 1, SZ, fin );
read( N ); read( M ); read( P );
for( int i = 1; i <= N; i++ )
for( int j = 1; j <= M; j++ )
read( A[i][j] );
for( int i = 1; i <= P; i++ ){
int X, Y; read( X ); read( Y );
pair<int,int> S1 = solve( X, Y );
if( X != Y ){
pair<int,int> S2 = solve( Y, X );
if( S1.first == S2.first )
fprintf( fout, "%d %d\n", S1.first, S1.second + S2.second );
else
if( S1.first < S2.first )
fprintf( fout, "%d %d\n", S1.first, S1.second );
else
fprintf( fout, "%d %d\n", S2.first, S2.second );
}else
fprintf( fout, "%d %d\n", S1.first, S1.second );
}
return 0;
}