Cod sursa(job #10140)

Utilizator bigsarpeadrian bigsarpe Data 27 ianuarie 2007 22:07:40
Problema Secventa 5 Scor 30
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.05 kb
{q-,r-,s-,d-,i-}
const maxn=1 shl 20;zero=ord('0');bucata=1 shl 11;modu=bucata-1;
var t:Text;
   V,A,B:array[0..maxn]of longword;s:string;
   by:array[0..maxn]of word;
   Count:array[0..bucata]of longint;
   n,la,lb,i,pas,lg:longint;last:longword;
   return:int64;
   procedure init;
   begin
      assign(T,'secv5.in');reset(T);readln(t,n,la,lb);
      for i:=1 to N do
      begin
         readln(t,S);
         for pas:=1 to length(s) do V[i]:=V[i]*10+ord(S[pas])-zero;
      end;close(t);
   end;
   PRocedure radixsort;
   begin
      for i:=1 to N do begin by[i]:=V[i]and modu;inc(count[by[i]]);end;
      for i:=1 to bucata do inc(count[i],count[i-1]);
      for i:=1 to N do begin B[count[by[i]]]:=i;dec(count[by[i]]);end;
      fillchar(count,sizeof(count),0);
      for i:=1 to N do begin by[i]:=(v[i]shr 11)and modu;inc(count[by[i]]);end;
      for i:=1 to bucata do inc(count[i],count[i-1]);
      for i:=N downto 1 do begin A[count[By[B[i]]]]:=B[i];dec(count[by[B[i]]]);end;
      fillchar(count,sizeof(count),0);
      for i:=1 to N do begin by[i]:=v[i]shr 22;inc(count[by[i]]);end;
      for i:=1 to bucata do inc(count[i],count[i-1]);
      for i:=N downto 1 do begin B[count[By[A[i]]]]:=A[i];dec(count[by[A[i]]]);end;
   end;
   procedure get(limit:longint);
   begin
      i:=1;lg:=0;fillchar(by,sizeof(by),0);
      for pas:=1 to N do
      begin
         while (lg<=limit)and(i<=N)do
         begin if by[V[i]]=0 then inc(lg);inc(by[V[i]]);inc(i);end;
         if lg>limit then inc(return,i-1) else inc(return,i);
         dec(by[V[pas]]);if by[V[pas]]=0 then dec(lg);
      end;
   end;
{var t1:longint;t2:longint absolute $0:$046c;}
begin
 {  t1:=t2;}
   init;
{   writeln((T2-t1)/18.2:0:6);  }
   radixsort;
{   writeln((T2-t1)/18.2:0:6);}
   last:=V[A[1]];lg:=1;
   for i:=1 to N do
   begin
      if V[A[i]]<>last then begin last:=v[a[i]];inc(lg);end;
      V[A[i]]:=lg;
   end;
   get(lb);return:=-return;get(la-1);
   assign(t,'secv5.out');rewrite(T);writeln(t,-return);close(T);
{   writeln((T2-t1)/18.2:0:6);}
end.