Cod sursa(job #1024907)

Utilizator hungntnktpHungntnktp hungntnktp Data 9 noiembrie 2013 12:18:47
Problema Struti Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 4.46 kb
const
    inp='struti.in';
    out='struti.out';
    maxn=1000+1;
type
    mg1=array[1..maxn] of longint;
    mg2=array[1..maxn,1..maxn] of longint;

var
    st,p:mg1;
    a,maxc,minc,tmp:mg2;
    fi,fo:text;
    i,j,m,n,q,dx,dy,res,dem:longint;

procedure process(dx,dy:longint);
var
    i,top,bot,j,k:longint;
begin
    fillchar(tmp,sizeof(tmp),0);
    for j:=1 to n do
        begin
            fillchar(st,sizeof(st),0);
            fillchar(p,sizeof(p),0);
            top:=0;
            bot:=1;
            k:=dy;
            for i:=1 to k-1 do
                begin
                    while (top>=bot) and (st[top]>=a[i][j]) do dec(top);
                    inc(top);
                    st[top]:=a[i][j];
                    p[top]:=i;
                end;
            for i:=1 to m-k+1 do
                begin
                    while (top>=bot) and (st[top]>=a[i+k-1][j]) do dec(top);
                    inc(top);
                    st[top]:=a[i+k-1][j];
                    p[top]:=i+k-1;
                    tmp[i+k-1][j]:=a[p[bot]][j];
                    while (top>=bot) and (p[bot]<=i) do inc(bot);
                end;
        end;
    for i:=dy to m do
        begin
            fillchar(st,sizeof(st),0);
            fillchar(p,sizeof(p),0);
            top:=0;
            bot:=1;
            k:=dx;
            for j:=1 to k-1 do
                begin
                    while (top>=bot) and (st[top]>=tmp[i][j]) do dec(top);
                    inc(top);
                    st[top]:=tmp[i][j];
                    p[top]:=j;
                end;
            for j:=1 to n-k+1 do
                begin
                    while (top>=bot) and (st[top]>=tmp[i][j+k-1]) do dec(top);
                    inc(top);
                    st[top]:=tmp[i][j+k-1];
                    p[top]:=j+k-1;
                    minc[i][j+k-1]:=tmp[i][p[bot]];
                    while (top>=bot) and (p[bot]<=j) do inc(bot);
                end;
        end;

//////////////////////////////////////////////////////////////////////////////////
    fillchar(tmp,sizeof(tmp),0);
    for j:=1 to n do
        begin
            fillchar(st,sizeof(st),0);
            fillchar(p,sizeof(p),0);
            top:=0;
            bot:=1;
            k:=dy;
            for i:=1 to k-1 do
                begin
                    while (top>=bot) and (st[top]<=a[i][j]) do dec(top);
                    inc(top);
                    st[top]:=a[i][j];
                    p[top]:=i;
                end;
            for i:=1 to m-k+1 do
                begin
                    while (top>=bot) and (st[top]<=a[i+k-1][j]) do dec(top);
                    inc(top);
                    st[top]:=a[i+k-1][j];
                    p[top]:=i+k-1;
                    tmp[i+k-1][j]:=a[p[bot]][j];
                    while (top>=bot) and (p[bot]<=i) do inc(bot);
                end;
        end;
    for i:=dy to m do
        begin
            fillchar(st,sizeof(st),0);
            fillchar(p,sizeof(p),0);
            top:=0;
            bot:=1;
            k:=dx;
            for j:=1 to k-1 do
                begin
                    while (top>=bot) and (st[top]<=tmp[i][j]) do dec(top);
                    inc(top);
                    st[top]:=tmp[i][j];
                    p[top]:=j;
                end;
            for j:=1 to n-k+1 do
                begin
                    while (top>=bot) and (st[top]<=tmp[i][j+k-1]) do dec(top);
                    inc(top);
                    st[top]:=tmp[i][j+k-1];
                    p[top]:=j+k-1;
                    maxc[i][j+k-1]:=tmp[i][p[bot]];
                    while (top>=bot) and (p[bot]<=j) do inc(bot);
                end;
        end;
    for i:=dy to m do
        for j:=dx to n do
            if maxc[i,j]-minc[i,j] < res then
                begin
                    res:=maxc[i,j]-minc[i,j];
                    dem:=1;
                end
            else
            if maxc[i,j]-minc[i,j] = res then
                inc(dem);
end;

begin
    assign(fi,inp); assign(fo,out);
    reset(fi);      rewrite(fo);
    read(fi,m,n,q);
    for i:=1 to m do
        for j:=1 to n do
            read(fi,a[i,j]);
    repeat
        read(fi,dx,dy);
        dem:=0;
        res:=maxlongint;
        process(dx,dy);
        if dx<>dy then process(dy,dx);
        dec(q);
        writeln(fo,res,' ',dem);
    until q<=0;
    close(fi);  close(fo);
end.