Cod sursa(job #167997)

Utilizator tamas_iuliaTamas Iulia tamas_iulia Data 30 martie 2008 15:16:34
Problema Gardieni Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.15 kb
var a,b,c : array[1..50005] of longint;
    jj,i,j,k,n,t,sol,min : longint;
    f,g : text;
procedure sort(l,r : longint);
var i,j,x,y : longint;
    aux : longint;
begin
     i:=l; j:=r; x:=a[(l+r)div 2];
     y:=b[(l+r)div 2];
     repeat
           while (a[i]<x)or((a[i]=x)and(b[i]<y))
            do inc(i);
           while (x<a[j])or((x=a[j])and(y<b[j]))
            do dec(j);
           if i<=j then
           begin
                aux:=a[i];a[i]:=a[j];a[j]:=aux;
                aux:=b[i];b[i]:=b[j];b[j]:=aux;
                aux:=c[i];c[i]:=c[j];c[j]:=aux;
                inc(i); dec(j);
           end;
     until i>j;
     if l<j then sort(l,j);
     if i<r then sort(i,r);
end;

begin
assign(f,'gardieni.in');reset(f);
assign(g,'gardieni.out');rewrite(g);
read(f,n,t);
for i:=1 to n do read(f,a[i],b[i],c[i]);
sort(1,n);
j:=1;
for i:=1 to t do
begin
min:=maxlongint;
  while i>b[j] do inc(j);
jj:=j;
 { while (a[j]<=i)and(i<=b[j]) do}
 for k:=j to j+9 do
 if (a[k]<=i)and(i<=b[k]) then
{  begin }
     if c[k]<min then min:=c[k];
{     inc(j);
  end;}
j:=jj;
inc(sol,min);
end;
writeln(g,sol);
close(g);
end.