Pagini recente » Cod sursa (job #2081110) | Cod sursa (job #1770763) | Cod sursa (job #2190782) | Cod sursa (job #2179126) | Cod sursa (job #1756171)
#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;
int n; int m; int p;
int a[NMAX][NMAX];
deque<pp> qmin[NMAX];
deque<pp> qmax[NMAX];
int ans = 0x3f3f3f3f;
int cnt = 0;
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 && qmin[j].back().x >= a[i][j])
qmin[j].pop_back();
qmin[j].push_back({a[i][j], i});
if(qmin[j].empty() == false && i - qmin[j].front().y + 1 > dx)
qmin[j].pop_front();
while(qmax[j].empty() == false && qmax[j].back().x <= a[i][j])
qmax[j].pop_back();
qmax[j].push_back({a[i][j], i});
if(qmax[j].empty() == false && i - qmax[j].front().y + 1 > dx)
qmax[j].pop_front();
}
qlow.clear();
qhigh.clear();
for(int j = 1; j <= m; ++j) {
while(qlow.empty() == false && qlow.back().x >= qmin[j].front().x)
qlow.pop_back();
qlow.push_back({qmin[j].front().x , j});
if(qlow.empty() == false && j - qlow.front().y + 1 > dy)
qlow.pop_front();
while(qhigh.empty() == false && qhigh.back().x <= qmax[j].front().x )
qhigh.pop_back();
qhigh.push_back({qmax[j].front().x , 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() {
fin >> n >> m >> p;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m ; ++j)
fin >> a[i][j];
while(p--) {
int dx , dy; fin >> dx >> dy;
ans = 0x3f3f3f3f;
cnt = 0;
find(dx, dy);
if(dx != dy)
find(dy, dx);
fout << ans << ' ' << cnt << '\n';
}
return 0;
}