Cod sursa(job #1571030)

Utilizator Master011Dragos Martac Master011 Data 17 ianuarie 2016 00:02:22
Problema Struti Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 6.26 kb
#include<cstdio>
#include<algorithm>
using namespace std;

const int nMax = 1001, bufSize = 6000000;
int dqMaxCol[nMax][nMax], dqMinCol[nMax][nMax], dqMaxLin[nMax], dqMinLin[nMax];
int dim1[nMax][2], dim2[nMax][2], mat[nMax][nMax], pos = bufSize;
int st1, dr1, st2, dr2;
char buf[bufSize];
FILE *in, *out;

inline char nextch(){
    if(pos == bufSize){
        fread(buf, bufSize, 1, in);
        pos = 0;
    }
    return buf[pos++];
}

inline bool isDigit(char c){
    return (c >= '0' && c <='9');
}

inline int read(){
    int x = 0;
    char ch;

    do{
        ch = nextch();
    }while(!isDigit(ch));

    do{
        x = x * 10 + ch - '0';
        ch = nextch();
    }while(isDigit(ch));

    return x;
}

int main(){
    in = fopen("struti.in","r");
    out = fopen("struti.out","w");

    int n, m, p, i, j, q, l, L, stA, drA, minDif, nrAp, mi, ma;

    n = read();
    m = read();
    p = read();

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

    for(q = 1 ; q <= p ; ++q){
        l = read();
        L = read();
        minDif = 10000; nrAp = 0;

        for( i = 1 ; i <= m ; ++i){
            dim1[i][0] = dim2[i][0] = 1;
            dim1[i][1] = dim2[i][1] = 0;
        }

        for( i = 1 ; i <= n ; ++i){

            st1 = st2 = 1; dr1 = dr2 = 0;
            for( j = 1 ; j <= m ; ++j){
                //push MAX (Col)
                stA = dim1[j][0]; drA = dim1[j][1];
                while(stA <= drA && mat[dqMaxCol[j][drA]][j] < mat[i][j])
                    drA--;
                dqMaxCol[j][++drA] = i;
                dim1[j][1] = drA;

                //pop MAX (Col)
                if(stA <= drA && dqMaxCol[j][stA] <= i - l)
                    stA++;
                dim1[j][0] = stA;


                 //push MIN (Col)
                stA = dim2[j][0]; drA = dim2[j][1];
                while(stA <= drA && mat[dqMinCol[j][drA]][j] > mat[i][j])
                    drA--;
                dqMinCol[j][++drA] = i;
                dim2[j][1] = drA;

                //pop MIN (Col)
                if(stA <= drA && dqMinCol[j][stA] <= i - l)
                    stA++;
                dim2[j][0] = stA;

                if(i >= l){
                    //push MAX (lin)
                    while(st1 <= dr1 && mat[dqMaxCol[dqMaxLin[dr1]][dim1[dqMaxLin[dr1]][0]]][dqMaxLin[dr1]] < mat[dqMaxCol[j][dim1[j][0]]][j] )
                        dr1--;
                    dqMaxLin[++dr1] = j;

                    //pop MAX (lin)
                    if(st1 <= dr1 && dqMaxLin[st1] <= j - L)
                        st1++;

                    //push MIN (lin)
                    while(st2 <= dr2 && mat[dqMinCol[dqMinLin[dr2]][dim2[dqMinLin[dr2]][0]]][dqMinLin[dr2]] > mat[dqMinCol[j][dim2[j][0]]][j])
                        dr2--;
                    dqMinLin[++dr2] = j;

                    //pop MIN (lin)
                    if(st2 <= dr2 && dqMinLin[st2] <= j - L)
                        st2++;
                    if(j >= L){
                        ma = mat[dqMaxCol[dqMaxLin[st1]][dim1[dqMaxLin[st1]][0]]][dqMaxLin[st1]];
                        mi = mat[dqMinCol[dqMinLin[st2]][dim2[dqMinLin[st2]][0]]][dqMinLin[st2]];
                        if(ma - mi < minDif){
                            minDif = ma - mi;
                            nrAp = 1;
                        }else if(ma - mi == minDif)
                            nrAp++;
                    }
                }
            }
        }

        if(l != L){
            swap(l,L);
            for( i = 1 ; i <= m ; ++i){
                dim1[i][0] = dim2[i][0] = 1;
                dim1[i][1] = dim2[i][1] = 0;
            }

            for( i = 1 ; i <= n ; ++i){

                st1 = st2 = 1; dr1 = dr2 = 0;
                for( j = 1 ; j <= m ; ++j){
                    //push MAX (Col)
                    stA = dim1[j][0]; drA = dim1[j][1];
                    while(stA <= drA && mat[dqMaxCol[j][drA]][j] < mat[i][j])
                        drA--;
                    dqMaxCol[j][++drA] = i;
                    dim1[j][1] = drA;

                    //pop MAX (Col)
                    if(stA <= drA && dqMaxCol[j][stA] <= i - l)
                        stA++;
                    dim1[j][0] = stA;


                     //push MIN (Col)
                    stA = dim2[j][0]; drA = dim2[j][1];
                    while(stA <= drA && mat[dqMinCol[j][drA]][j] > mat[i][j])
                        drA--;
                    dqMinCol[j][++drA] = i;
                    dim2[j][1] = drA;

                    //pop MIN (Col)
                    if(stA <= drA && dqMinCol[j][stA] <= i - l)
                        stA++;
                    dim2[j][0] = stA;

                    if(i >= l){
                        //push MAX (lin)
                        while(st1 <= dr1 && mat[dqMaxCol[dqMaxLin[dr1]][dim1[dqMaxLin[dr1]][0]]][dqMaxLin[dr1]] < mat[dqMaxCol[j][dim1[j][0]]][j] )
                            dr1--;
                        dqMaxLin[++dr1] = j;

                        //pop MAX (lin)
                        if(st1 <= dr1 && dqMaxLin[st1] <= j - L)
                            st1++;

                        //push MIN (lin)
                        while(st2 <= dr2 && mat[dqMinCol[dqMinLin[dr2]][dim2[dqMinLin[dr2]][0]]][dqMinLin[dr2]] > mat[dqMinCol[j][dim2[j][0]]][j])
                            dr2--;
                        dqMinLin[++dr2] = j;

                        //pop MIN (lin)
                        if(st2 <= dr2 && dqMinLin[st2] <= j - L)
                            st2++;
                        if(j >= L){
                            ma = mat[dqMaxCol[dqMaxLin[st1]][dim1[dqMaxLin[st1]][0]]][dqMaxLin[st1]];
                            mi = mat[dqMinCol[dqMinLin[st2]][dim2[dqMinLin[st2]][0]]][dqMinLin[st2]];
                            if(ma - mi < minDif){
                                minDif = ma - mi;
                                nrAp = 1;
                            }else if(ma - mi == minDif)
                                nrAp++;
                        }
                    }
                }
            }
        }
        fprintf(out,"%d %d\n", minDif, nrAp);
    }

    return 0;
}