Cod sursa(job #3367915)

Utilizator Andrei1209Andrei Mircea Andrei1209 Data 12 octombrie 2026 10:14:34
Problema Struti Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.27 kb
#include <fstream>
#include <deque>
#include <algorithm>

using namespace std;

ifstream fin("struti.in");
ofstream fout("struti.out");

const int Nmax = 1005;
int mat[Nmax][Nmax], n, m, solution, cnt;

void solve(int dx, int dy) // dx = lungime pe verticala
{
    deque<int> colMax[Nmax];
    deque<int> colMin[Nmax];

    int i, j;

    for (i = 1; i < dx; ++i)
    {
        for (j = 1; j <= m; ++j)
        {
            while (!colMax[j].empty() &&
                   mat[i][j] > mat[colMax[j].back()][j])
            {
                colMax[j].pop_back();
            }
            colMax[j].push_back(i);

            while (!colMin[j].empty() &&
                   mat[i][j] < mat[colMin[j].back()][j])
            {
                colMin[j].pop_back();
            }
            colMin[j].push_back(i);
        }
    }

    for (i = dx; i <= n; ++i)
    {
        for (j = 1; j <= m; ++j)
        {
            if (!colMax[j].empty() &&
                colMax[j].front() <= i - dx)
            {
                colMax[j].pop_front();
            }

            while (!colMax[j].empty() &&
                   mat[i][j] > mat[colMax[j].back()][j])
            {
                colMax[j].pop_back();
            }
            colMax[j].push_back(i);

            if (!colMin[j].empty() &&
                colMin[j].front() <= i - dx)
            {
                colMin[j].pop_front();
            }

            while (!colMin[j].empty() &&
                   mat[i][j] < mat[colMin[j].back()][j])
            {
                colMin[j].pop_back();
            }
            colMin[j].push_back(i);
        }

        int v[Nmax];

        for (j = 1; j <= m; ++j)
        {
            v[j] = mat[colMax[j].back()][j];
        }

        deque<int> Max, Min;

        for (j = 1; j < dy; ++j)
        {
            while (!Max.empty() && v[Max.back()] < v[j])
                Max.pop_back();

            Max.push_back(j);

            while (!Min.empty() && v[Min.back()] > v[j])
                Min.pop_back();

            Min.push_back(j);
        }

        for (j = dy; j <= m; ++j)
        {
            if (!Max.empty() && Max.front() <= j - dy)
                Max.pop_front();

            while (!Max.empty() && v[Max.back()] < v[j])
                Max.pop_back();

            Max.push_back(j);

            if (!Min.empty() && Min.front() <= j - dy)
                Min.pop_front();

            while (!Min.empty() && v[Min.back()] > v[j])
                Min.pop_back();

            Min.push_back(j);

            if ( v[Max.front()] - v[Min.front()] < solution )
            {
                solution = v[Max.front()] - v[Min.front()];
                 cnt = 0;

            }
            if ( v[Max.front()] - v[Min.front()] == solution )
                ++cnt;
        }
    }
}

int main()
{
    int p;
    fin >> n >> m >> p;

    int i, j;

    for (i = 1; i <= n; ++i)
        for (j = 1; j <= m; ++j)
            fin >> mat[i][j];

    for (i = 1; i <= p; ++i)
    {
        int dx, dy;
        fin >> dx >> dy;

        solution = 8000 + 132;
        cnt = 0;
        solve(dx, dy);


        fout << solution << " " << cnt << '\n';
    }

    return 0;
}