Cod sursa(job #926682)

Utilizator atatomirTatomir Alex atatomir Data 25 martie 2013 12:21:32
Problema Heavy metal Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.25 kb
type formatie= record
       a,b:longint;
     end;

var n,i,pos1,pos2:longint;
    v:array[1..100001]of formatie;
    bufin:array[1..65000]of byte;
    suma:int64;


procedure Sort(l, r: longint);
var
  i, j, x, y: longint;
begin
  i := l; j := r; x := v[(l+r) DIV 2].a;
  repeat
    while v[i].a < x do i := i + 1;
    while x < v[j].a do j := j - 1;
    if i <= j then
    begin
      y := v[i].a; v[i].a := v[j].a; v[j].a := y;
      y := v[i].b; v[i].b := v[j].b; v[j].b := y;
      i := i + 1; j := j - 1;
    end;
  until i > j;
  if l < j then Sort(l, j);
  if i < r then Sort(i, r);
end;

begin
  assign(input,'heavymetal.in'); reset(input);
  assign(output,'heavymetal.out'); rewrite(output);
  settextbuf(input,bufin);

  readln(n);
  for i := 1 to n do readln(v[i].a,v[i].b);
  Sort(1,n);


  //for i := 1 to n do writeln(v[i].a,' ',v[i].b);

  pos1 := v[1].a; pos2 := v[1].b; suma := 0;

  for i := 2 to n do
  begin
    if v[i].a > pos2 then
    begin
      suma := suma + pos2-pos1;
      pos1 := v[i].a;
      pos2 := v[i].b;
    end
    else
    begin
      if v[i].b > pos2 then pos2 := v[i].b ;
    end;
  end;

  suma := suma + pos2-pos1;

  write(suma);

  close(input);
  close(output);
end.