Pagini recente » Cod sursa (job #1881593) | Cod sursa (job #299109) | Cod sursa (job #1788482) | Cod sursa (job #989413) | Cod sursa (job #2147961)
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
ifstream in("struti.in");
ofstream out("struti.out");
deque<pair<int,int>> dm,dM;
int mat[1001][1001],matm[1001][1001],matM[1001][1001];
int mn,nr,n,m;
void solve(int X,int Y)
{
int i,j;
for(i=1;i<=n;i++)
{
dm.clear();
dM.clear();
for(j=1;j<=m;j++)
{
while(!dm.empty()&&mat[i][j]<=dm.back().x)
dm.pop_back();
dm.push_back(make_pair(mat[i][j],j));
if(dm.front().y<j-Y+1)
dm.pop_front();
matm[i][j]=dm.front().x;
while(!dM.empty()&&mat[i][j]>=dM.back().x)
dM.pop_back();
dM.push_back(make_pair(mat[i][j],j));
if(dM.front().y<j-Y+1)
dM.pop_front();
matM[i][j]=dM.front().x;
}
}
for(j=Y;j<=m;j++)
{
dm.clear();
dM.clear();
for(i=1;i<=n;i++)
{
while(!dm.empty()&&matm[i][j]<=dm.back().x)
dm.pop_back();
dm.push_back(make_pair(matm[i][j],i));
if(dm.front().y<i-X+1)
dm.pop_front();
while(!dM.empty()&&matM[i][j]>=dM.back().x)
dM.pop_back();
dM.push_back(make_pair(matM[i][j],i));
if(dM.front().y<i-X+1)
dM.pop_front();
if(i>=X)
{
int dif=dM.front().x-dm.front().x;
if(dif<mn)
{
mn=dif;
nr=1;
}
else if(dif==mn)
nr++;
}
}
}
}
int main()
{
int p,i,j,a,b;
in>>n>>m>>p;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
in>>mat[i][j];
while(p--)
{
in>>a>>b;
mn=99999;
nr=0;
solve(a,b);
if(a!=b)
solve(b,a);
out<<mn<<" "<<nr<<"\n";
}
return 0;
}