Pagini recente » Cod sursa (job #2431188) | Cod sursa (job #1803329) | Cod sursa (job #1584501) | Cod sursa (job #775970) | Cod sursa (job #2447009)
#include<fstream>
using namespace std;
ifstream cin("struti.in");
ofstream cout("struti.out");
template<class X>
class Deque{
short s,d;
X v[1005];
public:
Deque(){s=1;d=0;}
void Clear(){s=1;d=0;}
short Empty(){return s>d;}
X Back(){return v[d];}
X Front(){return v[s];}
void Pop_Back(){--d;}
void Pop_Front(){++s;}
void Push_Back(X x){v[++d]=x;}
void Push_Front(X x){v[--s]=x;}
};
short n,m,p,x[2];
short mat[1005][1005];
Deque<pair<short,short> > DeqMinRectangle[2],DeqMaxRectangle[2];
Deque<short> DeqMinline[2][1005],DeqMaxline[2][1005];
int main(){
cin>>n>>m>>p;
for(short i=1;i<=n;i++)
for(short j=1;j<=m;j++)
cin>>mat[i][j];
while(p--){
cin>>x[0]>>x[1];
for(short c=0;c<2;c++)
for(short i=1;i<=n;i++){
DeqMinline[c][i].Clear();
DeqMaxline[c][i].Clear();
}
short DifMin=8005,Nr;
for(short j=1;j<=m;j++){
for(short c=0;c<2;c++){
DeqMinRectangle[c].Clear();
DeqMaxRectangle[c].Clear();
}
for(short i=1;i<=n;i++)
for(short c=0;c<2;c++){
Deque<short> &dmin=DeqMinline[c][i],&dmax=DeqMaxline[c][i];
Deque<pair<short,short> > &hmin=DeqMinRectangle[c],&hmax=DeqMaxRectangle[c];
short *a=mat[i];
while(!dmin.Empty() && a[dmin.Back()]>=a[j]) dmin.Pop_Back();
dmin.Push_Back(j);
while(!dmax.Empty() && a[dmax.Back()]<=a[j]) dmax.Pop_Back();
dmax.Push_Back(j);
if(j>=x[c]){
while(!hmin.Empty() && hmin.Back().second>=a[dmin.Front()]) hmin.Pop_Back();
hmin.Push_Back(make_pair(i,a[dmin.Front()]));
while(!hmax.Empty() && hmax.Back().second<=a[dmax.Front()]) hmax.Pop_Back();
hmax.Push_Back(make_pair(i,a[dmax.Front()]));
if(i>=x[1-c]){
short Dif=hmax.Front().second-hmin.Front().second;
if(Dif<DifMin){
DifMin=Dif;
Nr=1;
}
else if(Dif==DifMin) ++Nr;
}
if(hmin.Front().first==i-x[1-c]+1) hmin.Pop_Front();
if(hmax.Front().first==i-x[1-c]+1) hmax.Pop_Front();
}
if(dmin.Front()==j-x[c]+1) dmin.Pop_Front();
if(dmax.Front()==j-x[c]+1) dmax.Pop_Front();
if(x[0]==x[1]) break;
}
}
cout<<DifMin<<' '<<Nr<<'\n';
}
}