Cod sursa(job #2684220)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 13 decembrie 2020 11:18:27
Problema Struti Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.91 kb
#include <bits/stdc++.h>
#define NMAX 1005
using namespace std;

/************************************************/
/// PARSARE

class InParser {
private:
	FILE *f;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, f);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		f = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};
/************************************************/
/// INPUT / OUTPUT
InParser f("struti.in");
ofstream g("struti.out");
/************************************************/
/// GLOBAL DECLARATIONS

int N, M, P, land[NMAX][NMAX], ap, minDiff, minMat[NMAX][NMAX], maxMat[NMAX][NMAX];
int a,b;
deque < int > minDq;
deque < int > maxDq;
/************************************************/
/// FUNCTIONS

void ReadInput();
void Solution();
/************************************************/
///--------------------------------------------------------------------
inline void ReadInput()
{
    f >> N >> M >> P;
    for(int i = 1 ; i <= N ; ++ i)
        for(int j = 1 ; j <= M ; ++ j)
            f >> land[i][j];
}
///--------------------------------------------------------------------
void GetAnswer(const int a, const int b)
{
    /// WE CREATE THE MINMAT AND MAXMAT
    for(int j = 1 ; j <= M ; ++ j)
    {
        minDq.clear();
        maxDq.clear();
        for(int i = 1 ; i <= N ; ++ i)
        {
            while(!maxDq.empty() && land[maxDq.back()][j] <= land[i][j]) maxDq.pop_back();
            while(!minDq.empty() && land[minDq.back()][j] >= land[i][j]) minDq.pop_back();
            minDq.push_back(i);
            maxDq.push_back(i);

            if(minDq.front() == i - a) minDq.pop_front();
            if(maxDq.front() == i - a) maxDq.pop_front();

            maxMat[i][j] = land[maxDq.front()][j];
            minMat[i][j] = land[minDq.front()][j];
        }
    }

    /// DEQUE ON MINMAT AND MAXMAT

    for(int i = a ; i <= N; ++ i)
    {
        minDq.clear();
        maxDq.clear();
        for(int j = 1 ; j <= M ; ++ j)
        {
            while(!maxDq.empty() && maxMat[i][maxDq.back()] <= maxMat[i][j]) maxDq.pop_back();
            while(!minDq.empty() && minMat[i][minDq.back()] >= minMat[i][j]) minDq.pop_back();
            maxDq.push_back(j);
            minDq.push_back(j);

            if(maxDq.front() == j - b) maxDq.pop_front();
            if(minDq.front() == j - b) minDq.pop_front();

            if(j >= b && minDiff > (maxMat[i][maxDq.front()] - minMat[i][minDq.front()]))
            {
                minDiff = maxMat[i][maxDq.front()] - minMat[i][minDq.front()];
                ap = 1;
            }
            else if(j >= b && (maxMat[i][maxDq.front()] - minMat[i][minDq.front()]) == minDiff)
            {
                ap++;
            }
        }
    }
}
///--------------------------------------------------------------------
inline void Solution()
{
    while(P--)
    {
        minDiff =21000;
        ap = 0;
        f >> a >> b;
        GetAnswer(a, b);
        if(a != b)
        {
            GetAnswer(b, a);
        }
        g << minDiff << " " << ap << "\n";
    }
}
///--------------------------------------------------------------------
int main()
{
    ReadInput();
    Solution();
    return 0;
}