Pagini recente » Cod sursa (job #2090014) | Cod sursa (job #2715964) | Cod sursa (job #1614) | Cod sursa (job #161005) | Cod sursa (job #1767553)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("struti.in");
ofstream fout("struti.out");
#define pp pair<int16_t, int16_t>
#define x first
#define y second
const int NMAX = 1004;
const int DIMBUF = 5e6 + NMAX;
int n; int m; int p;
int a[NMAX][NMAX];
deque<int> qmin[NMAX];
deque<int> qmax[NMAX];
int ans = 0x3f3f3f3f;
int cnt = 0;
char buff[DIMBUF]; int pos; int len;
int get(int& x) {
while(pos < len && !('0' <= buff[pos] && buff[pos] <= '9' ) ) {
++pos;
}
x = 0;
while(pos < len && '0' <= buff[pos] && buff[pos] <= '9') {
x = x * 10 + buff[pos] - '0';
++pos;
}
}
void find(int dx, int dy) {//dx * dy
for(int i = 1; i <= max(n, m); ++i) {
qmax[i].clear(); qmin[i].clear();
}
deque<pp> qlow;
deque<pp> qhigh;
for(int i = 1 ; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
//actulizez cozile pe orizonatala
while(qmin[j].empty() == false && a[qmin[j].back()][j] >= a[i][j])
qmin[j].pop_back();
qmin[j].push_back(i);
if(qmin[j].empty() == false && i - qmin[j].front() + 1 > dx)
qmin[j].pop_front();
while(qmax[j].empty() == false && a[qmax[j].back()][j] <= a[i][j])
qmax[j].pop_back();
qmax[j].push_back(i);
if(qmax[j].empty() == false && i - qmax[j].front() + 1 > dx)
qmax[j].pop_front();
}
qlow.clear();
qhigh.clear();
for(int j = 1; j <= m; ++j) {
while(qlow.empty() == false && qlow.back().x >= a[qmin[j].front()][j])
qlow.pop_back();
qlow.push_back({a[qmin[j].front()][j] , j});
if(qlow.empty() == false && j - qlow.front().y + 1 > dy)
qlow.pop_front();
while(qhigh.empty() == false && qhigh.back().x <= a[qmax[j].front()][j])
qhigh.pop_back();
qhigh.push_back({a[qmax[j].front()][j] , j});
if(qhigh.empty() == false && j - qhigh.front().y + 1 > dy)
qhigh.pop_front();
if(j >= dy && i >= dx) {
if(qhigh.front().x - qlow.front().x == ans)
cnt++;
if(qhigh.front().x - qlow.front().x < ans) {
ans = qhigh.front().x - qlow.front().x;
cnt = 1;
}
}
}
}
}
int main() {
freopen("struti.in", "r", stdin);
freopen("struti.out", "w", stdout);
fread(buff, 1, DIMBUF, stdin);
len = strlen(buff);
get(n); get(m); get(p);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m ; ++j)
get(a[i][j]);
while(p--) {
int dx , dy;
get(dx); get(dy);
ans = 0x3f3f3f3f;
cnt = 0;
find(dx, dy);
if(dx != dy)
find(dy, dx);
fout << ans << ' ' << cnt << '\n';
}
return 0;
}