Cod sursa(job #938046)

Utilizator ericptsStavarache Petru Eric ericpts Data 11 aprilie 2013 17:42:26
Problema Struti Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.96 kb
#include <fstream>
#include <deque>
#include <iostream>

using namespace std;


#define rm_front(D,DX) while(!D.empty() && D.front() < i - DX + 1)D.pop_front()
#define rm_back(D,op,M) while(!D.empty() && M[i][care] op M[D.back()][care])D.pop_back()
#define pb push_back

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

int n,m,T;
int DX,DY;
int best,fnd;
const int maxn = 1010;
short M[maxn][maxn];
short big[maxn][maxn];
short small[maxn][maxn];

deque<int> D,d;

void read();
void coloana(int);
void solve();
void lines(int);

void read(){
    in >> n >> m >> T;
    for(register int i = 1 ; i <= n ; ++ i)
        for(register int j = 1 ; j <= m ; ++ j)
            in >> M[i][j];
}


void coloana(int care){
    D.clear();d.clear();
    for(register int i = 1 ; i <= n ; ++ i){

        rm_front(D,DX);
        rm_front(d,DX);

        rm_back(D,>=,M);
        rm_back(d,<=,M);

        D.pb(i);
        d.pb(i);

        if(i >= DX){
            big[care][i-DX+1] = M[D.front()][care];
            small[care][i-DX+1] = M[d.front()][care];
        }
    }
}

void lines(int care){
    D.clear();d.clear();
    int x;

    for(register int i = 1 ; i <= m ; ++ i){

        rm_front(D,DY);
        rm_front(d,DY);
        rm_back(D,>=,big);
        rm_back(d,<=,small);

        D.pb(i);
        d.pb(i);

        if(i >= DY){
            x = big[D.front()][care] - small[d.front()][care];
            if(x == best)
                ++fnd;
            else if (x < best){
                best = x;
                fnd = 1;
            }
        }
    }
}

void partial(){
    for(register int i = 1 ; i <= m ; ++ i)
        coloana(i);
    for(register int i = 1 ; i <= n-DX+1 ; ++ i)
        lines(i);
}

void solve(){
    while(T--){
        best = 1 << 30;fnd = 0;
        in >> DX >> DY;
        partial();
        if(DX != DY){
            swap(DX,DY);
            partial();
        }
        out << best << " " << fnd << "\n";
    }
}

int main()
{
    read();
    solve();
    return 0;
}