I and OI
Past...
const maxn=50001;
      maxm=trunc(ln(maxn)/ln(2));
var   f,g:array[0..maxn,0..maxm] of longint;
      num:array[0..maxn] of longint;
      n,m,i,j,maxnum,minnum,l,r,k:longint;

      function max(a,b:longint):longint;
      begin if a>b then exit(a); exit(b); end;

      function min(a,b:longint):longint;
      begin if a<b then exit(a); exit(b); end;

      function lg(x:longint):longint;
      begin exit(trunc(ln(x)/ln(2))); end;

      function ex(x:longint):longint;
      begin exit(1<<x); end;

begin
      readln(n,m);
      for i:=1 to n do
      begin
            readln(num[i]);
            f[i,0]:=num[i];
            g[i,0]:=num[i];
      end;
      for j:=1 to lg(n) do
         for i:=1 to n-ex(j)+1 do
         f[i,j]:=min(f[i,j-1],f[i+ex(j-1),j-1]);

      for j:=1 to lg(n) do
         for i:=1 to n-ex(j)+1 do
         g[i,j]:=max(g[i,j-1],g[i+ex(j-1),j-1]);

      for i:=1 to m do
      begin
            readln(l,r);
            k:=lg(r-l+1);
            minnum:=min(f[l,k],f[r-ex(k)+1,k]);
            maxnum:=max(g[l,k],g[r-ex(k)+1,k]);
            writeln(maxnum-minnum);
      end;
end.

  

posted on 2011-11-09 19:15  exponent  阅读(274)  评论(0编辑  收藏  举报