Pagini recente » Cod sursa (job #2754493) | Cod sursa (job #36864) | Cod sursa (job #1787848) | Cod sursa (job #1645354) | Cod sursa (job #1599856)
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int nmax = 1007;
int a[nmax][nmax],N,M,Q,sol,nr,maxx[nmax][nmax],minn[nmax][nmax];
deque<int>Q1,Q2;
inline void Calc(int x,int y){
int i,j,s,cnt;
for(j = 1; j <= M; ++j){
Q1.clear(); Q2.clear();
for(i = 1; i <= N; ++i){
while(!Q1.empty() && a[Q1.back()][j] < a[i][j])Q1.pop_back();
Q1.push_back(i);
if(Q1.front() <= i-x)Q1.pop_front();
maxx[i][j] = a[Q1.front()][j];
while(!Q2.empty() && a[Q2.back()][j] > a[i][j])Q2.pop_back();
Q2.push_back(i);
if(Q2.front() <= i-x)Q2.pop_front();
minn[i][j] = a[Q2.front()][j];
}
}
s = INF;
for(i = 1; i <= N; ++i){
Q1.clear();Q2.clear();
for(j = 1; j <= M; ++j){
while(!Q1.empty() && maxx[i][Q1.back()] < maxx[i][j])Q1.pop_back();
Q1.push_back(j);
if(Q1.front() <= j-y)Q1.pop_front();
while(!Q2.empty() && minn[i][Q2.back()] > minn[i][j])Q2.pop_back();
Q2.push_back(j);
if(Q2.front() <= j-y)Q2.pop_front();
if(j >= y && i >= x){
if(maxx[i][Q1.front()]-minn[i][Q2.front()]==s)
++cnt;
else if(maxx[i][Q1.front()]-minn[i][Q2.front()]<s)
cnt = 1,s = maxx[i][Q1.front()]-minn[i][Q2.front()];
}
}
}
if(s == sol)nr+=cnt;
else if(s < sol)nr=cnt,sol=s;
}
int main(){
int i,j,x,y;
freopen ("struti.in","r",stdin);
freopen ("struti.out","w",stdout);
scanf("%d %d %d\n",&N,&M,&Q);
for(i = 1; i <= N; ++i)
for(j = 1; j <= M; ++j)
scanf("%d ",&a[i][j]);
while(Q--){
scanf("%d %d\n",&x,&y);
sol = INF;
nr = 0;
Calc(x,y);
if(x!=y)Calc(y,x);
printf("%d %d\n",sol,nr);
}
return 0;
}