Pagini recente » Cod sursa (job #1027583) | Cod sursa (job #851723) | Cod sursa (job #1194327) | Cod sursa (job #1913260) | Cod sursa (job #916366)
Cod sursa(job #916366)
#include<fstream>
#include<deque>
//#define debug
const int int_max=(1<<31)-1;
const int N_MAX=1005;
using namespace std;
int n, m, p, l, c, sol, nr_sol, i, j;
int A[N_MAX][N_MAX], Min[N_MAX][N_MAX], Max[N_MAX][N_MAX];
deque <int> a, b;
inline void solve(int l, int c)
{
a.clear();
b.clear();
for(i=1; i<=n; ++i)
{
a.clear();
b.clear();
for(j=1; j<=m; ++j)
{
while(!a.empty() && A[i][a.back()]>=A[i][j])
a.pop_back();
a.push_back(j);
if(a.front() <= j-c)
a.pop_front();
if(j >= c)
Min[i][j]=A[i][a.front()];
while(!b.empty() && A[i][b.back()]<=A[i][j])
b.pop_back();
b.push_back(j);
if(b.front() <= j-c)
b.pop_front();
if(j >= c)
Max[i][j]=A[i][b.front()];
}
}
for(j=c; j<=m ;++j)
{
a.clear();
b.clear();
for(i=1; i<=n; ++i)
{
{
while(!a.empty() && Min[i][j]<=Min[a.back()][j])
a.pop_back();
a.push_back(i);
if(a.front() <= i-l)
a.pop_front();
while(!b.empty() && Max[i][j]>=Max[b.back()][j])
b.pop_back();
b.push_back(i);
if(b.front() <= i-l)
b.pop_front();
if(i >= l)
{
if(Max[b.front()][j]-Min[a.front()][j] < sol)
sol = Max[b.front()][j]-Min[a.front()][j], nr_sol = 1;
else if(Max[b.front()][j]-Min[a.front()][j] == sol)
++nr_sol;
}
}
}
}
}
int main()
{
ifstream cin("struti.in");
ofstream cout("struti.out");
cin>>n>>m>>p;
for( int i=1; i<=n; ++i )
for( int j=1; j<=m; ++j )
cin>>A[i][j];
for( ; p ; p--)
{
cin>>l>>c;
sol=int_max;
nr_sol=0;
solve(l, c);
if(!(l==c))
solve(c, l);
cout<<sol<<" "<<nr_sol<<"\n";
}
return 0;
}