Pagini recente » Cod sursa (job #1586983) | Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #228977) | Cod sursa (job #2015315)
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>
#include <deque>
using namespace std;
const int inf = 3e4 + 5;
const int NMax = 1e3 + 5;
class parser {
using integer = short;
static const int strMax = 6*NMax*NMax + 50;
ifstream in;
char str[strMax],*p;
bool failed;
public:
parser(string s) {
in.open(s);
in.get(str,strMax,1);
p = str;
failed = false;
}
parser& operator>> (integer& nr) {
bool neg = 0;
while ( !( ('0' <= *p && *p <= '9') || *p == '\0') ) {
if (*p == '-') {
neg ^= 1;
}
++p;
}
if (*p == '\0') {
nr = 0;
failed = true;
return *this;
}
int temp = 0;
while ('0' <= *p && *p <= '9') {
temp = temp * 10 + *p++ - '0';
}
if (neg) {temp = -temp;}
nr = temp;
return *this;
}
bool fail() {
return failed;
}
void print() {
cout<<str<<'\n';
}
void close() {
in.close();
}
};
parser pin("struti.in");
ofstream out("struti.out");
#define ll long long
#define ull unsigned long long
#define pb push_back
using zint = short;
zint N,M,P;
zint a[NMax][NMax];
struct elem {
zint idx,val;
elem (zint _idx = 0,zint _val = 0) {
idx = _idx;
val = _val;
}
};
void solve(zint,zint,zint&,int&);
int main() {
pin>>N>>M>>P;
for (zint i=1;i <= N;++i) {
for (zint j=1;j <= M;++j) {
pin>>a[i][j];
}
}
while (P--) {
zint dx,dy,min1,min2;
int nrMin1,nrMin2;
pin>>dx>>dy;
solve(dx,dy,min1,nrMin1);
//cout<<min1<<' '<<nrMin1<<'\n';
if (dx != dy) {
solve(dy,dx,min2,nrMin2);
//cout<<min2<<' '<<nrMin2<<'\n';
if (min1 == min2) {
nrMin1 += nrMin2;
}
else if (min1 > min2) {
min1 = min2;
nrMin1 = nrMin2;
}
}
//cout<<'\n';
out<<min1<<' '<<nrMin1<<'\n';
}
pin.close();out.close();
return 0;
}
void solve(zint dx,zint dy,zint& mn,int& nrMin) {
deque<elem> rowMn[NMax],rowMx[NMax];
mn = inf, nrMin = 0;
for (int j=1;j <= M;++j) {
for (int i=1;i <= N;++i) {
while (rowMn[i].size() && rowMn[i].back().val >= a[i][j]) {
rowMn[i].pop_back();
}
rowMn[i].push_back(elem(j,a[i][j]));
if (rowMn[i].front().idx == j-dy) {
rowMn[i].pop_front();
}
while (rowMx[i].size() && rowMx[i].back().val <= a[i][j]) {
rowMx[i].pop_back();
}
rowMx[i].push_back(elem(j,a[i][j]));
if (rowMx[i].front().idx == j-dy) {
rowMx[i].pop_front();
}
//cout<<i<<' '<<rowMn[i].front()<<' '<<rowMx[i].front()<<'\n';
}
//cout<<'\n';
if (j >= dy) {
deque<elem> colMn,colMx;
for (int i=1;i <= N;++i) {
while (colMn.size() && colMn.back().val >= rowMn[i].front().val) {
colMn.pop_back();
}
colMn.push_back(elem(i,rowMn[i].front().val));
if (colMn.front().idx == i-dx) {
colMn.pop_front();
}
while (colMx.size() && colMx.back().val <= rowMx[i].front().val) {
colMx.pop_back();
}
colMx.push_back(elem(i,rowMx[i].front().val));
if (colMx.front().idx == i-dx) {
colMx.pop_front();
}
//cout<<i<<' '<<colMn.front()<<' '<<colMx.front()<<"\n\n\n";
if (i >= dx) {
short val = colMx.front().val - colMn.front().val;
//cout<<val<<"\n\n\n";
if (mn > val) {
mn = val;
nrMin = 1;
}
else if (mn == val) {
++nrMin;
}
}
}
}
}
}