Thứ Hai, 9 tháng 5, 2022

INTERVALS PASCAL

var a,b:array[0..100009] of longint;
    i,n:longint;
    kq,res:int64;
procedure nhap;
    begin
        readln(n);
        For i:=1 to n do
            read(A[i],b[i]);
    end;

procedure swap(var i,j:longint);
    var t:longint;
        begin
            t:=i;
            i:=j;
            j:=t;
        end;

procedure sort(l,r:longint);
    var i,j,m,k:longint;
        begin
            i:=l;
            j:=r;
            m:=random(r-l)+l+1;
            k:=b[m];
            repeat
                while k>b[i] do inc(i);
                while k<b[j] do dec(j);
                if not (i>j) then
                    begin
                        swap(a[i],a[j]);
                        swap(b[i],b[j]);
                        inc(i);
                        dec(j);
                    end;
            until i>j;
                if l<j then sort(l,j);
                if i<r then sort(i,r);
        end;

function chat(l,r:longint):longint;
var mid,res:longint;
begin
res:=-1;
while l<=r do
begin
         mid:=(l+r) div 2;
            if b[mid]>=a[i] then
             begin
                  r:=mid-1;
                 res:=mid;
            end 
    else l:=mid+1;
       end;
       exit(res);
end;

procedure xuli;
begin
sort(1,n);
     For i:=2 to n do
       begin
           res:=chat(1,i-1);
            if res<>-1 then kq:=kq+i-res;
      end;
      writeln(kq);
end;

begin
    assign(input,'intervals.inp');reset(input);
    assign(output,'intervals.out');rewrite(output);
    nhap;
    xuli;
end.

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.