Cod sursa(job #47645)

Utilizator andrei_infoMirestean Andrei andrei_info Data 3 aprilie 2007 21:22:10
Problema Secventa 5 Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.08 kb
{secventa 5 infoarena unirea 2007}

const maxhash = 1111111;
      nmax = 1 shl 20;

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


procedure addhash(old:longword; neww:longint);
var p : pnod;
    poz : longint;
begin
poz:=old mod maxhash;
new(p); p^.old:=old; p^.new:=neww; p^.next:=hash[poz];
hash[poz]:=p;
end;

function cautahash(old:longword):longint;
var p:pnod;
    poz:longint;
begin
cautahash:=0;
poz:=old mod maxhash;
p:=hash[poz];
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):int64;
var i,li:longint;
    rez:int64;
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,y:longint;
    x:longword;
    buf: array[1..32768] of byte;
    f:text;
begin
new:=0;
assign(f,'secv5.in'); reset(f); settextbuf(f,buf);
readln(f,n,l,u);
for i:=1 to n do
        begin
        readln(f,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.