Pagini recente » Cod sursa (job #2898119) | Cod sursa (job #1117911) | Cod sursa (job #1866598) | Cod sursa (job #2121247) | Cod sursa (job #2120682)
#include <cstdio>
#include <deque>
#include <cstring>
using namespace std;
const int NMAX = 1005;
int v[NMAX][NMAX];
int amax[NMAX][NMAX];
int amin[NMAX][NMAX];
int bmax[NMAX][NMAX];
int bmin[NMAX][NMAX];
int n, m;
struct ABC {
int x, y;
};
deque <ABC> dq;
void remove_amax(ABC poz) {
while(!dq.empty() && v[poz.x][poz.y] > v[dq.back().x][dq.back().y]) {
dq.pop_back();
}
dq.push_back(poz);
}
void remove_amin(ABC poz) {
while(!dq.empty() && v[poz.x][poz.y] < v[dq.back().x][dq.back().y]) {
dq.pop_back();
}
dq.push_back(poz);
}
void init_amax(int k) {
memset(amax, 0, sizeof(amax));
for(int i = 1; i <= n; ++i) {
int j = 1;
dq.clear();
while(j < k) {
remove_amax({i, j});
amax[i][j++] = v[dq.front().x][dq.front().y];
}
while(j <= m) {
remove_amax({i, j});
while(!dq.empty() && j - dq.front().y >= k) {
dq.pop_front();
}
amax[i][j++] = v[dq.front().x][dq.front().y];
}
}
}
void init_amin(int k) {
memset(amin, 0, sizeof(amin));
for(int i = 1; i <= n; ++i) {
int j = 1;
dq.clear();
while(j < k) {
remove_amin({i, j});
amin[i][j++] = v[dq.front().x][dq.front().y];
}
while(j <= m) {
remove_amin({i, j});
while(!dq.empty() && j - dq.front().y >= k) {
dq.pop_front();
}
amin[i][j++] = v[dq.front().x][dq.front().y];
}
}
}
void r_amax(ABC poz) {
while(!dq.empty() && amax[poz.x][poz.y] > amax[dq.back().x][dq.back().y]) {
dq.pop_back();
}
dq.push_back(poz);
}
void r_amin(ABC poz) {
while(!dq.empty() && amin[poz.x][poz.y] < amin[dq.back().x][dq.back().y]) {
dq.pop_back();
}
dq.push_back(poz);
}
void update_amax(int k) {
memset(bmax, 0, sizeof(bmax));
for(int j = 1; j <= m; ++j) {
int i = 1;
dq.clear();
while(i < k) {
r_amax({i, j});
bmax[i++][j] = amax[dq.front().x][dq.front().y];
}
while(i <= n) {
r_amax({i, j});
while(!dq.empty() && i - dq.front().x >= k) {
dq.pop_front();
}
bmax[i++][j] = amax[dq.front().x][dq.front().y];
}
}
}
void update_amin(int k) {
memset(bmin, 0, sizeof(bmin));
for(int j = 1; j <= m; ++j) {
int i = 1;
dq.clear();
while(i < k) {
r_amin({i, j});
bmin[i++][j] = amin[dq.front().x][dq.front().y];
}
while(i <= n) {
r_amin({i, j});
while(!dq.empty() && i - dq.front().x >= k) {
dq.pop_front();
}
bmin[i++][j] = amin[dq.front().x][dq.front().y];
}
}
}
int main() {
int p;
freopen("struti.in", "r", stdin);
freopen("struti.out", "w", stdout);
scanf("%d%d%d", &n, &m, &p);
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
scanf("%d", &v[i][j]);
}
}
while(p--) {
int x, y;
scanf("%d%d", &x, &y);
init_amax(y);
init_amin(y);
update_amax(x);
update_amin(x);
int sol = 0x7fffffff, apsol = 0;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
if(i >= x && j >= y) {
if(sol > bmax[i][j] - bmin[i][j]) {
sol = bmax[i][j] - bmin[i][j];
apsol = 0;
}
if(sol == bmax[i][j] - bmin[i][j]) {
++apsol;
}
}
}
}
if(x != y) {
init_amax(x);
init_amin(x);
update_amax(y);
update_amin(y);
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
if(i >= y && j >= x) {
if(sol > bmax[i][j] - bmin[i][j]) {
sol = bmax[i][j] - bmin[i][j];
apsol = 0;
}
if(sol == bmax[i][j] - bmin[i][j]) {
++apsol;
}
}
}
}
}
printf("%d %d\n", sol, apsol);
}
return 0;
}