Pagini recente » Cod sursa (job #2581784) | Cod sursa (job #2039343) | Cod sursa (job #282242) | Cod sursa (job #2490946) | Cod sursa (job #1993782)
#include <fstream>
#include <deque>
using namespace std;
ofstream fout ("struti.out");
class InputReader {
public:
InputReader() {}
InputReader(const char *name) {
fin = fopen(name, "r");
buffpos = Size - 1;
}
inline InputReader &operator >>(int &n) {
char ch = nextpos();
while((ch < '0' || ch > '9') && ch != '-') {
ch = nextpos();
}
int semn = 1;
if (ch == '-') {
semn = -1; ch = nextpos();
}
n = 0;
while('0' <= ch && ch <= '9') {
n = n * 10 + ch - '0';
ch = nextpos();
}
n *= semn;
return *this;
}
private:
FILE *fin;
static const int Size = 1 << 17;
int buffpos;
char buff[Size];
inline char nextpos() {
++ buffpos;
if(buffpos == Size) {
buffpos = 0;
fread(buff, Size, 1, fin);
}
return buff[ buffpos ];
}
} fin ("struti.in");
const int nmax = 1000;
const int inf = 1 << 30;
int n, m;
int v[nmax + 1][nmax + 1];
deque<pair<int, int>> mx, mn, mncol[nmax + 1], mxcol[nmax + 1];
void baga_minim (deque<pair<int, int>> &d, int val, int pos, int lg) {
while (!d.empty() && d.back().first >= val) {
d.pop_back();
}
d.push_back(make_pair(val, pos));
if (d.front().second <= pos - lg) {
d.pop_front();
}
}
void baga_maxim (deque<pair<int, int>> &d, int val, int pos, int lg) {
while (!d.empty() && d.back().first <= val) {
d.pop_back();
}
d.push_back(make_pair(val, pos));
if (d.front().second <= pos - lg) {
d.pop_front();
}
}
pair<int, int> solve (int x, int y) {
for (int i = 1; i <= m; ++ i) {
mncol[ i ].clear();
mxcol[ i ].clear();
}
int sol, cate;
sol = inf; cate = 0;
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= m; ++ j) {
baga_minim(mncol[ j ], v[ i ][ j ], i, x);
baga_maxim(mxcol[ j ], v[ i ][ j ], i, x);
}
mn.clear(); mx.clear();
for (int j = 1; j <= m; ++ j) {
baga_minim(mn, mncol[ j ].front().first, j, y);
baga_maxim(mx, mxcol[ j ].front().first, j, y);
if (x <= i && y <= j) {
int dif = mx.front().first - mn.front().first;
if (dif < sol) {
sol = dif; cate = 0;
}
cate += (dif == sol);
}
}
}
return make_pair(sol, cate);
}
int main() {
int q;
fin >> n >> m >> q;
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= m; ++ j) {
fin >> v[ i ][ j ];
}
}
while (q --) {
int x, y;
fin >> x >> y;
pair<int, int> ans1 = solve(x, y);
if (x != y) {
pair<int, int> ans2 = solve(y, x);
if (ans1.first == ans2.first) {
ans1.second += ans2.second;
} else {
ans1 = ans2;
}
}
fout << ans1.first << " " << ans1.second << "\n";
}
fout.close();
return 0;
}