Cod sursa(job #1218282)

Utilizator popoiu.georgeGeorge Popoiu popoiu.george Data 10 august 2014 12:30:55
Problema Struti Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.18 kb
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>

using namespace std;

#define INFL "struti.in"
#define OUTFL "struti.out"

#define INF 0x3f3f3f3f
#define nmax 1001
#define lmax 10
#define DIM 10000

int m, n, p;
int A[nmax][nmax];
deque<int> qMin[nmax], qMax[nmax], q, qq;

char buff[DIM];
int poz = DIM-1;

inline void citeste(int &numar)
{
     numar = 0;
     //cat timp caracterul din buffer nu e cifra ignor
     while (buff[poz] < '0' || buff[poz] > '9')
          //daca am "golit" bufferul atunci il umplu
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     //cat timp dau de o cifra recalculez numarul
     while ('0'<=buff[poz] && buff[poz]<='9')
     {
          numar = numar*10 + buff[poz] - '0';
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     }
}

void read() {
    citeste(m);
    citeste(n);
    citeste(p);
    //scanf("%d%d%d", &m, &n, &p);

    for(int i=1; i<=m; ++i)
        for(int j=1; j<=n; ++j)
            citeste(A[i][j]);
            //scanf("%d", &A[i][j]);
}

int qf, qqf;
int qb, qqb;

int _solve(int x, int y, int &cnt) {
    int ans = INF, diff;
    cnt = 0;

    for(int i=1; i<=m; ++i) {
        for(int j=1; j<=n; ++j) {
            /*while(!qMin[j].empty() && i - qMin[j].front() >= x)
                qMin[j].pop_front();*/
            while(!qMin[j].empty() && A[i][j] <= A[qMin[j].back()][j])
                qMin[j].pop_back();
            qMin[j].push_back(i);
            if(qMin[j].front() == i - x)
                qMin[j].pop_front();

            /*while(!qMax[j].empty() && i - qMax[j].front() >= x)
                qMax[j].pop_front();*/
            while(!qMax[j].empty() && A[i][j] >= A[qMax[j].back()][j])
                qMax[j].pop_back();
            qMax[j].push_back(i);
            if(qMax[j].front() == i - x)
                qMax[j].pop_front();
        }

        if(i >= x) {
            for(int j=1; j<=n; ++j) {
                /*while(!q.empty() && j - q.front() >= y)
                    q.pop_front();*/
                while(!q.empty() && A[qMin[j].front()][j] <= A[qMin[(qb = q.back())].front()][qb])
                    q.pop_back();
                q.push_back(j);
                if(q.front() == j - y)
                    q.pop_front();

                /*while(!qq.empty() && j - qq.front() >= y)
                    qq.pop_front();*/
                while(!qq.empty() && A[qMax[j].front()][j] >= A[qMax[(qqb = qq.back())].front()][qqb])
                    qq.pop_back();
                qq.push_back(j);
                if(qq.front() == j - y)
                    qq.pop_front();

                if(j >= y) {
                    diff = A[qMax[(qqf = qq.front())].front()][qqf] - A[qMin[(qf = q.front())].front()][qf];

                    if(diff < ans) {
                        ans = diff;
                        cnt = 1;
                    }
                    else if(diff == ans) {
                        ++cnt;
                    }
                }
            }

            q.clear();
            qq.clear();
        }
    }

    for(int i=1; i<=m; ++i) {
        qMin[i].clear();
        qMax[i].clear();
    }

    return ans;
}

void solve() {
    int dx, dy, ans, cnt, c, aux;

    for(int i=1; i<=p; ++i) {
        citeste(dx);
        citeste(dy);
        //scanf("%d%d", &dx, &dy);

        if(dx <= m && dy <= n) {
            ans = _solve(dx, dy, c);
            cnt = c;
        }

        if(dx != dy && dy <= m && dx <= n) {
            aux = _solve(dy, dx, c);
            if(aux < ans) {
                ans = aux;
                cnt = c;
            }
            else if(aux == ans)
                cnt += c;
        }

        printf("%d %d\n", ans, cnt);
    }
}

int main() {
//#define ONLINE_JUDGE 1
#ifndef ONLINE_JUDGE
	freopen(INFL, "r", stdin);
	freopen(OUTFL, "w", stdout);
#endif

	read();
	solve();

	return 0;
}