Pagini recente » Cod sursa (job #539896) | Cod sursa (job #1127772) | Cod sursa (job #1485884) | Cod sursa (job #1940195) | Cod sursa (job #633752)
Cod sursa(job #633752)
# include <cstdio>
# include <deque>
using namespace std;
# define verf ++poz == hg ? fread ( ch, 1, hg, stdin ), poz = 0 : 0
const char *FIN = "struti.in", *FOU = "struti.out";
const short MAX = 1005, oo = 0x3f, hg = 1 << 13;
deque <short> mini, maxi;
short A[MAX][MAX], minm[MAX][MAX], maxm[MAX][MAX];
short N, M, T, poz;
char ch[hg];
inline void cit ( short &x ) {
if ( ch[0] == '\0' ) fread ( ch, 1, hg, stdin ) ;
else for ( ; ch[poz] < '0' || ch[poz] > '9' ; verf ) ;
for ( x = 0 ; ch[poz] >= '0' && ch[poz] <= '9' ; x = x * 10 + ch[poz] - '0', verf ) ;
}
inline void solve (short x, short y, short &MIN, short &NR) {
for (short i = 1; i <= M; ++i) {
mini.clear (), maxi.clear ();
for (short j = 1; j <= N; ++j) {
for (; ! mini.empty () && A[i][j] <= A[i][mini.back ()]; mini.pop_back ());
for (; ! maxi.empty () && A[i][j] >= A[i][maxi.back ()]; maxi.pop_back ());
mini.push_back (j), maxi.push_back (j);
if (mini.front () <= j - x) mini.pop_front ();
if (maxi.front () <= j - x) maxi.pop_front ();
minm[i][j] = A[i][mini.front ()];
maxm[i][j] = A[i][maxi.front ()];
}
}
for (short i = x; i <= N; ++i) {
mini.clear (), maxi.clear ();
for (short j = 1; j <= M; ++j) {
for (; ! mini.empty () && minm[j][i] <= minm[mini.back ()][i]; mini.pop_back ());
for (; ! maxi.empty () && maxm[j][i] >= maxm[maxi.back ()][i]; maxi.pop_back ());
mini.push_back (j), maxi.push_back (j);
if (mini.front () <= j - y) mini.pop_front ();
if (maxi.front () <= j - y) maxi.pop_front ();
if (j >= y) {
short aux = maxm[maxi.front ()][i] - minm[mini.front ()][i];
if (aux < MIN) MIN = aux, NR = 1;
else if (aux == MIN) NR += 1;
}
}
}
}
int main (void) {
freopen (FIN, "r", stdin);
freopen (FOU, "w", stdout);
cit (M), cit (N), cit (T);
for (short i = 1; i <= M; ++i)
for (short j = 1; j <= N; ++j)
cit (A[i][j]);
for (short i = 1, x, y; i <= T; ++i) {
cit (x), cit (y);
short MIN = oo, NR = 0;
solve (x, y, MIN, NR);
if (x != y) solve (y, x, MIN, NR);
printf ("%d %d\n", MIN, NR);
}
}