Pagini recente » Cod sursa (job #517187) | Cod sursa (job #666256) | Cod sursa (job #287388) | Cod sursa (job #2508100) | Cod sursa (job #1988427)
#include<fstream>
#include<deque>
using namespace std;
class InParser {
private:
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp == 4096) {
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int &n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long &n) {
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
} in( "struti.in" );
ofstream out("struti.out");
int n,m,p,h,i,j,minim,l,a,b;
int mat[1001][1001],mil[1001][1001],mic[1001][1001],mal[1001][1001],mac[1001][1001];
deque<int>w,v;
int main(){
in >> n >> m >> p;
for( i = 1; i <= n; i ++ ){
for( j = 1; j <= m; j ++ ){
in >> mat[i][j];
}
}
for( h = 1; h <= p; h ++ ){
in >> a >> b;
minim = 8100;
l = 0;
for( i = 1; i <= n; i ++ ){
w.clear();
v.clear();
for( j = 1; j <= m; j ++ ){
while( w.empty() == 0 && mat[i][j] < mat[i][w.back()] ){
w.pop_back();
}
w.push_back(j);
if(w.front() <= j - b ){
w.pop_front();
}
mil[i][j] = mat[i][w.front()];
while( v.empty() == 0 && mat[i][j] > mat[i][v.back()] ){
v.pop_back();
}
v.push_back(j);
if(v.front() <= j - b ){
v.pop_front();
}
mal[i][j] = mat[i][v.front()];
}
}
for( j = 1; j <= m; j ++ ){
w.clear();
v.clear();
for( i = 1; i <= n; i ++ ){
while( w.empty() == 0 && mil[i][j] < mil[w.back()][j] ){
w.pop_back();
}
w.push_back(i);
if(w.front() <= i - a ){
w.pop_front();
}
mic[i][j] = mil[w.front()][j];
while( v.empty() == 0 && mal[i][j] > mal[v.back()][j] ){
v.pop_back();
}
v.push_back(i);
if(v.front() <= i - a ){
v.pop_front();
}
mac[i][j] = mal[v.front()][j];
}
}
for( i = a; i <= n; i ++ ){
for( j = b; j <= m; j ++ ){
if( mac[i][j] - mic[i][j] < minim ){
minim = mac[i][j] - mic[i][j];
l = 0;
}
if( mac[i][j] - mic[i][j] == minim ){
l++;
}
}
}
if( a != b ){
for( i = 1; i <= n; i ++ ){
w.clear();
v.clear();
for( j = 1; j <= m; j ++ ){
while( w.empty() == 0 && mat[i][j] < mat[i][w.back()] ){
w.pop_back();
}
w.push_back(j);
if(w.front() <= j - a ){
w.pop_front();
}
mil[i][j] = mat[i][w.front()];
while( v.empty() == 0 && mat[i][j] > mat[i][v.back()] ){
v.pop_back();
}
v.push_back(j);
if(v.front() <= j - a ){
v.pop_front();
}
mal[i][j] = mat[i][v.front()];
}
}
for( j = 1; j <= m; j ++ ){
w.clear();
v.clear();
for( i = 1; i <= n; i ++ ){
while( w.empty() == 0 && mil[i][j] < mil[w.back()][j] ){
w.pop_back();
}
w.push_back(i);
if(w.front() <= i - b ){
w.pop_front();
}
mic[i][j] = mil[w.front()][j];
while( v.empty() == 0 && mal[i][j] > mal[v.back()][j] ){
v.pop_back();
}
v.push_back(i);
if(v.front() <= i - b ){
v.pop_front();
}
mac[i][j] = mal[v.front()][j];
}
}
for( i = b; i <= n; i ++ ){
for( j = a; j <= m; j ++ ){
if( mac[i][j] - mic[i][j] < minim ){
minim = mac[i][j] - mic[i][j];
l = 0;
}
if( mac[i][j] - mic[i][j] == minim ){
l++;
}
}
}
}
out<<minim <<" "<<l<<"\n";
}
return 0;
}