Pagini recente » Cod sursa (job #1241731) | Cod sursa (job #2682598) | Cod sursa (job #2683080) | Rating asdn asda (84L4) | Cod sursa (job #2085275)
#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 short nextnum(){
short 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;
short a;
struct Ans{
short min;
int nr;
};
short v[1001][1001];
std::deque <short> d[1001];
std::deque <short> D[1001];
short e[1001];
short E[1001];
std::deque <short> min, max;
short minval, maxval;
inline Ans Compute(short dx, short dy){
Ans ans;
ans.min = 10000;
for(short i = 1; i <= m; ++i){
d[i].clear();
D[i].clear();
}
for(short j = 1; j <= n; ++j){
min.clear();
max.clear();
for(short i = 1; i <= m; ++i){
//minim
if(!d[i].empty() && j - d[i].front() + 1 > dx)
d[i].pop_front();
while(!d[i].empty() && v[i][d[i].back()] > v[i][j])
d[i].pop_back();
d[i].push_back(j);
e[i] = d[i].front();
}
for(short i = 1; i <= m; ++i){
//maxim
if(!D[i].empty() && j - D[i].front() + 1 > dx)
D[i].pop_front();
while(!D[i].empty() && v[i][D[i].back()] < v[i][j])
D[i].pop_back();
D[i].push_back(j);
E[i] = D[i].front();
}
for(short i = 1; i <= m; ++i){
//minim
if(!min.empty() && i - min.front() + 1 > dy)
min.pop_front();
a = v[i][e[i]];
while(!min.empty() && v[min.back()][e[min.back()]] > a)
min.pop_back();
min.push_back(i);
//maxim
if(!max.empty() && i - max.front() + 1 > dy)
max.pop_front();
a = v[i][E[i]];
while(!max.empty() && v[max.back()][E[max.back()]] < a)
max.pop_back();
max.push_back(i);
minval = v[min.front()][e[min.front()]];
maxval = v[max.front()][E[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;
}