Pagini recente » Cod sursa (job #1145452) | Cod sursa (job #2602395) | Cod sursa (job #948524) | Cod sursa (job #2565971) | Cod sursa (job #2085578)
#include <bits/stdc++.h>
#define MAXN 1000
#define BUF_SIZE 1 << 14
char buf[BUF_SIZE];
int pbuf=BUF_SIZE;
FILE*fi,*fo;
inline char nextch(){
if(pbuf==BUF_SIZE){
fread(buf, BUF_SIZE, 1, fi);
pbuf=0;
}
return buf[pbuf++];
}
inline int nextnum(){
int a = 0;
char c = nextch();
while(!isdigit(c))
c = nextch();
while(isdigit(c)){
a = a * 10 + c - '0';
c = nextch();
}
return a;
}
short m, n;
struct Ans{
short min;
int nr;
};
short v[1 + MAXN][1 + MAXN];
short a[1 + MAXN][1 + MAXN];
short b[1 + MAXN][1 + MAXN];
inline Ans Compute(short dx, short dy){
Ans ans;
ans.min = 10000;
std::deque <short> min, max;
for(int i = 1; i <= m; i++){
min.clear();
max.clear();
for(int j = 1; j <= n; j++){
if(!min.empty() && j - min.front() + 1 > dx)
min.pop_front();
while(!min.empty() && v[i][min.back()] > v[i][j])
min.pop_back();
min.push_back(j);
a[j][i] = v[i][min.front()];
if(!max.empty() && j - max.front() + 1 > dx)
max.pop_front();
while(!max.empty() && v[i][max.back()] < v[i][j])
max.pop_back();
max.push_back(j);
b[j][i] = v[i][max.front()];
}
}
for(int j = 1; j <= n; j++){
min.clear();
max.clear();
for(int i = 1; i <= m; i++){
if(!min.empty() && i - min.front() + 1 > dy)
min.pop_front();
while(!min.empty() && a[j][min.back()] > a[j][i])
min.pop_back();
min.push_back(i);
if(!max.empty() && i - max.front() + 1 > dy)
max.pop_front();
while(!max.empty() && b[j][max.back()] < b[j][i])
max.pop_back();
max.push_back(i);
short minval = a[j][min.front()];
short maxval = b[j][max.front()];
if(i >= dy && j >= dx){
if(maxval - minval < ans.min){
ans.min = maxval - minval;
ans.nr = 0;
}
if(maxval - minval == ans.min)
ans.nr++;
}
}
}
return ans;
}
int main(){
fi=fopen("struti.in","r");
fo=fopen("struti.out","w");
short p, dx, dy;
m = nextnum(), n = nextnum(), p = nextnum();
for(short i = 1; i <= m; ++i)
for(short j = 1; j <= n; ++j)
v[i][j] = nextnum();
for(short i = 1; i <= p; ++i){
dx = nextnum();
dy = nextnum();
Ans a = Compute(dx, dy);
Ans b = Compute(dy, dx);
if(dx == dy)
fprintf(fo,"%hd %d\n", a.min, a.nr);
else{
if(a.min < b.min)
fprintf(fo,"%hd %d\n", a.min, a.nr);
else if(a.min == b.min)
fprintf(fo,"%hd %d\n", a.min, a.nr + b.nr);
else
fprintf(fo,"%hd %d\n", b.min, b.nr);
}
}
fclose(fi);
fclose(fo);
return 0;
}