Cod sursa(job #10715)

Utilizator andrei_blanaruAndrei Blanaru andrei_blanaru Data 29 ianuarie 2007 01:28:56
Problema Secventa 5 Scor 60
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.18 kb
type ref=^elem;
     elem=record
            x,count:longword;
            urm:ref;
          end;
     sir=array [1..1050000] of longword;
const magic={6505469}10005997;
var h:array [0..magic] of ref;
    a,ll,uu:sir;
    n,l,u:longword;
    rez:int64;


procedure citire;
var i:longint;
begin
  assign(input,'secv5.in');
  reset(input);
  readln(n,l,u);
  for i:=1 to n do
    readln(a[i]);
  close(input);
end;

function gaseste(k:longword):ref;
var p:ref;
begin
  p:=h[k mod magic];
  while p<>nil do
    if p^.x=k
      then  begin
              gaseste:=p;
              exit;
            end
      else  p:=p^.urm;
  gaseste:=nil;
end;

procedure pune(k:longword);
var p:ref;
    kk:longword;
begin
  new(p);
  p^.x:=k;
  p^.count:=1;
  kk:=k mod magic;
  p^.urm:=h[kk];
  h[kk]:=p;
end;

procedure prel(q:longword;var qq:sir);
var i,j,w:longword;
    aux:ref;
begin
  j:=0;
  w:=0;
  for i:=1 to n do
    begin
      while (w<q)and(j<n) do
        begin
          inc(j);
          aux:=gaseste(a[j]);
          if aux<>nil
            then  begin
                    if aux^.count=0
                      then  inc(w);
                    inc(aux^.count);
                  end
            else  begin
                    pune(a[j]);
                    inc(w);
                  end;
        end;
      if w=q
        then  qq[i]:=j
        else  exit;
      aux:=gaseste(a[i]);
      dec(aux^.count);
      if aux^.count=0
        then  dec(w);
    end;
end;

procedure curata;
var i,k:longword;
    p:ref;
begin
  for i:=1 to n do
    begin
      k:=a[i] mod magic;
      p:=h[k];
      while h[k]<>nil do
        begin
          p:=h[k];
          h[k]:=h[k]^.urm;
          dispose(p);
        end;
    end;
end;

procedure final;
var i:longword;
begin
  for i:=1 to n do
    if ll[i]<>0
      then  begin
              if uu[i]=0
                then  uu[i]:=n+1;
              rez:=rez+uu[i]-ll[i];
            end
      else  break;
  assign(output,'secv5.out');
  rewrite(output);
  writeln(rez);
  close(output);
end;

begin
  citire;
  prel(l,ll);
  curata;
  prel(u+1,uu);
  final;
end.