Cod sursa(job #1830029)

Utilizator EpictetStamatin Cristian Epictet Data 15 decembrie 2016 23:43:35
Problema Struti Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.41 kb
#include <fstream>
#include <queue>

using namespace std;

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

short n, m, p, dif_min, V[1010][1010], Mi[1010][1010], Ma[1010][1010];
int nr_dif_min;
deque < short > DQC_Mi, DQC_Ma;
deque < pair < short, short > > DQL_Mi, DQL_Ma;
char P[32768], *now;

inline void Verif()
{
    if (*now == '\0')
    {
        fin.get(P, 32768, '\0');
        now = P;
    }
}

int Get()
{
    while (*now < '0' || *now > '9')
    {
        now ++;
        Verif();
    }
    int number = 0;
    while (*now >= '0' && *now <= '9')
    {
        number = number * 10 + *now - '0';
        now ++;
        Verif();
    }
    return number;
}

void Solve(short dx, short dy)
{
    for (short j = 1; j <= m; j ++)
    {
        DQC_Mi.clear();
        DQC_Ma.clear();
        DQC_Mi.push_back(1);
        DQC_Ma.push_back(1);
        for (short i = 2; i <= n; i ++)
        {
            if (DQC_Mi.front() <= i - dx) DQC_Mi.pop_front();
            while (!DQC_Mi.empty() && V[i][j] < V[DQC_Mi.back()][j]) DQC_Mi.pop_back();
            DQC_Mi.push_back(i);
            Mi[i][j] = DQC_Mi.front();

            if (DQC_Ma.front() <= i - dx) DQC_Ma.pop_front();
            while (!DQC_Ma.empty() && V[i][j] > V[DQC_Ma.back()][j]) DQC_Ma.pop_back();
            DQC_Ma.push_back(i);
            Ma[i][j] = DQC_Ma.front();
        }
    }

    for (short i = dx; i <= n; i ++)
    {
        DQL_Mi.clear();
        DQL_Ma.clear();
        for (short j = 1; j < dy; j ++)
        {
            while (!DQL_Mi.empty() && V[Mi[i][j]][j] < V[DQL_Mi.back().first][DQL_Mi.back().second]) DQL_Mi.pop_back();
            DQL_Mi.push_back( { Mi[i][j], j } );

            while (!DQL_Ma.empty() && V[Ma[i][j]][j] > V[DQL_Ma.back().first][DQL_Ma.back().second]) DQL_Ma.pop_back();
            DQL_Ma.push_back( { Ma[i][j], j } );
        }
        for (short j = dy; j <= m; j ++)
        {
            if (DQL_Mi.front().second <= j - dy) DQL_Mi.pop_front();
            while (!DQL_Mi.empty() && V[Mi[i][j]][j] < V[DQL_Mi.back().first][DQL_Mi.back().second]) DQL_Mi.pop_back();
            DQL_Mi.push_back( { Mi[i][j], j } );

            if (!DQL_Ma.empty() && DQL_Ma.front().second <= j - dy) DQL_Ma.pop_front();
            while (!DQL_Ma.empty() && V[Ma[i][j]][j] > V[DQL_Ma.back().first][DQL_Ma.back().second]) DQL_Ma.pop_back();
            DQL_Ma.push_back( { Ma[i][j], j } );

            short dif = V[DQL_Ma.front().first][DQL_Ma.front().second] - V[DQL_Mi.front().first][DQL_Mi.front().second];
            if (dif < dif_min)
            {
                dif_min = dif;
                nr_dif_min = 1;
            }
            else if (dif == dif_min)
            {
                nr_dif_min += 1;
            }
        }
    }
}

int main()
{
    now = P;
    Verif();
    n = Get(); m = Get(); p = Get();
    for (short i = 1; i <= n; i ++)
    {
        for (short j = 1; j <= m; j ++)
        {
            V[i][j] = Get();
        }
    }
    for (short i = 1, dx, dy; i <= p; i ++)
    {
        dx = Get(); dy = Get();
        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;
}