Pagini recente » Cod sursa (job #2786546) | Cod sursa (job #3157599) | Cod sursa (job #699085) | Cod sursa (job #2303798) | Cod sursa (job #1830009)
#include <fstream>
#include <cstring>
#include <queue>
using namespace std;
ifstream fin ("struti.in");
ofstream fout ("struti.out");
int n, m, p, dif_min, nr_dif_min, V[1010][1010], Dif[1010][1010];
deque < int > DQC_Mi[1010], DQC_Ma[1010];
deque < pair < int, int > > DQL_Mi, DQL_Ma;
void Solve(int dx, int dy)
{
for (int j = 1; j <= m; j ++)
{
DQC_Mi[j].clear();
DQC_Ma[j].clear();
DQC_Mi[j].push_back(1);
DQC_Ma[j].push_back(1);
}
for (int i = 2; i <= n; i ++)
{
DQL_Mi.clear();
DQL_Ma.clear();
for (int j = 1; j <= m; j ++)
{
if (DQC_Mi[j].front() <= i - dx) DQC_Mi[j].pop_front();
while (!DQC_Mi[j].empty() && V[i][j] < V[DQC_Mi[j].back()][j]) DQC_Mi[j].pop_back();
DQC_Mi[j].push_back(i);
if (DQL_Mi.empty()) DQL_Mi.push_back( { DQC_Mi[j].front(), j } );
while (!DQL_Mi.empty() && DQL_Mi.front().second <= j - dy) DQL_Mi.pop_front();
while (!DQL_Mi.empty() && V[DQC_Mi[j].front()][j] < V[DQL_Mi.back().first][DQL_Mi.back().second]) DQL_Mi.pop_back();
DQL_Mi.push_back( { DQC_Mi[j].front(), j } );
if (DQC_Ma[j].front() <= i - dx) DQC_Ma[j].pop_front();
while (!DQC_Ma[j].empty() && V[i][j] > V[DQC_Ma[j].back()][j]) DQC_Ma[j].pop_back();
DQC_Ma[j].push_back(i);
if (DQL_Ma.empty()) DQL_Ma.push_back( { DQC_Ma[j].front(), j } );
while (!DQL_Ma.empty() && DQL_Ma.front().second <= j - dy) DQL_Ma.pop_front();
while (!DQL_Ma.empty() && V[DQC_Ma[j].front()][j] > V[DQL_Ma.back().first][DQL_Ma.back().second]) DQL_Ma.pop_back();
DQL_Ma.push_back( { DQC_Ma[j].front(), j } );
if (i >= dx && j >= dy)
{
Dif[i][j] = V[DQL_Ma.front().first][DQL_Ma.front().second] - V[DQL_Mi.front().first][DQL_Mi.front().second];
}
}
}
for (int i = dx; i <= n; i ++)
{
for (int j = dy; j <= m; j ++)
{
if (Dif[i][j] < dif_min)
{
dif_min = Dif[i][j];
nr_dif_min = 1;
}
else if (Dif[i][j] == dif_min)
{
nr_dif_min += 1;
}
}
}
}
int main()
{
fin >> n >> m >> p;
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= m; j ++)
{
fin >> V[i][j];
}
}
for (int i = 1, dx, dy; i <= p; i ++)
{
fin >> dx >> dy;
dif_min = 10000;
nr_dif_min = 0;
if (dx == dy)
{
Solve(dx, dy);
}
else
{
Solve(dx, dy);
Solve(dy, dx);
}
fout << dif_min << ' ' << nr_dif_min << '\n';
}
fout.close();
return 0;
}