Cod sursa(job #14939)

Utilizator andrei_infoMirestean Andrei andrei_info Data 10 februarie 2007 12:12:37
Problema Secventa 5 Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.17 kb
{secventa 5 infoarena unirea 2007}

const maxhash = 1111111;
      nmax = 1 shl 20;

type pnod = ^tnod;
     tnod = record
                old,new:longint;
                next:pnod;
                end;
     lista = record
                head,last:pnod;
                end;
var sir: array[1..nmax] of longint;
    hash : array[0..maxhash] of lista;
    frecv: array[1..nmax] of longint;
    n,l,u,nrdis:longint;
    rez:longint;


procedure addhash(old,neww:longint);
var p : pnod;
    poz : longint;
begin
new(p); p^.old:=old; p^.new:=neww; p^.next:=nil;
poz:=old mod maxhash;
if hash[poz].head = nil then
        hash[poz].head:=p
else    hash[poz].last^.next:=p;
hash[poz].last:=p;
end;

function cautahash(old:longint):longint;
var p:pnod;
    poz:longint;
begin
cautahash:=0;
poz:=old mod maxhash;
p:=hash[poz].head;
while p <> nil do
        begin
        if p^.old = old then
                begin
                cautahash:=p^.new;
                break;
                end;
        p:=p^.next;
        end;
end;

function calc(x:longint):longint;
var i,li:longint;
    rez:longint;
begin
fillchar(frecv,sizeof(frecv),0);
nrdis:=0; rez:=0;
li:=1;
for i:=1 to n do
        begin
        if frecv[sir[i]] = 0 then
                begin
                frecv[sir[i]]:=1;
                inc(nrdis);
                end
        else inc(frecv[sir[i]]);
        while nrdis > x do
                begin
                dec(frecv[sir[li]]);
                if frecv[sir[li]] = 0 then dec(nrdis);
                inc(li);
                end;
        rez:=rez+(i-li+1);
        end;
calc:=rez;
end;

procedure citire;
var i,new,x,y:longint;
begin
new:=0;
assign(input,'secv5.in'); reset(input);
readln(n,l,u);
for i:=1 to n do
        begin
        readln(x);
        y:=cautahash(x);
        if y <> 0 then sir[i]:=y
        else
                begin
                inc(new);
                addhash(x,new);
                sir[i]:=new;
                end;
        end;
close(input);
end;



begin
citire;
assign(output,'secv5.out'); rewrite(output);
writeln(calc(u)-calc(l-1));
close(output);
end.