Cod sursa(job #12082)

Utilizator bigsarpeadrian bigsarpe Data 2 februarie 2007 21:27:18
Problema Secventa 5 Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.08 kb
{q-,r-,s-,d-,i-}
const maxn=(1 shl 20);zero=ord('0');modu1=666013;modu2=405617;cat=2;
{const maxn=(1 shl 10)+6;zero=ord('0');modu=666;}
var t:Text;
   V:Array[0..maxn]of longword;
   A,F:array[0..maxn]of longint;
   Hash1,Hash2:array[0..maxn*cat]of longint;
   grad1,from1:array[0..modu1]of longint;
   grad2,from2:Array[0..modu2]of longint;
   la,lb,lg,unde1,unde2,i,n,j:longint;s:string;gasit:boolean;
   sol:int64;
   Procedure get(limit:longint);
   begin
      fillchar(f,sizeof(f),0);lg:=0;j:=1;
      for i:=1 to N do
      begin
         while (lg<=limit)and(j<=N)do
         begin if F[A[j]]=0 then inc(lg);inc(f[A[j]]);inc(j);end;
         if lg>limit then inc(sol,j-1) else inc(sol,j);
         dec(f[a[i]]);if F[A[i]]=0 then dec(lg);
      end;
   end;
{var t1:longint;t2:longint absolute $0:$046c;}
begin
 { t1:=t2;}
   assign(T,'secv5.in');reset(T);readln(t,n,la,lb);
   for i:=1 to N do
   begin readln(t,S);for j:=1 to length(S) do V[i]:=V[i]*10+ord(s[j])-zero;end;
   close(T);V[0]:=-1;
   for i:=1 to modu1 do begin Grad1[i]:=grad1[i-1]+cat;from1[i]:=grad1[i]+1;end;
   for i:=1 to modu2 do begin Grad2[i]:=grad2[i-1]+cat;from2[i]:=grad2[i]+1;end;
   for i:=1 to N do
   begin
      unde1:=V[i]-(v[i]div modu1)*modu1;gasit:=false;
      for j:=from1[unde1]to grad1[unde1] do
      if V[Hash1[j]]=V[i] then
      begin gasit:=true;A[i]:=A[hash1[j]];break;end;
      if not gasit then
      begin
         unde2:=V[i]-(v[i]div modu2)*modu2;gasit:=false;
         for j:=from2[unde2]to grad1[unde2] do
         if V[Hash2[j]]=V[i] then
         begin gasit:=true;A[i]:=A[hash2[j]];break;end;
         if not gasit then
         begin
            inc(lg);A[i]:=lg;
            if Grad1[unde1]-From1[unde1]<Grad2[unde2]-From2[unde2] then
            begin dec(From1[unde1]);Hash1[from1[unde1]]:=i;end else
            begin dec(From2[unde2]);Hash2[from2[unde2]]:=i;end;
         end;
      end;
   end;
   get(lb);sol:=-sol;get(la-1);
   assign(t,'secv5.out');rewrite(T);writeln(T,-sol);close(T);
{   writeln((T2-t1)/18.2:0:6);                                }
end.