poj3067

求交点的个数;

容易发现,对于两条航线(xi,yi)和(xj,yj),设xi<xj

只有yi>yj时两条航线存在交点;

于是我们考虑以x为第一关键字减序,y为第二关键字为减序排序;

则对于当前航线(xi,yi),只要找之前所有yj小于yi的个数

所有交点数就是其总和,统计就要用到飘逸的树状数组了~

 var a,c:array[..] of longint;
    x,y:array[..] of longint;
    i,j,n,m,k,t:longint;
    ans:int64; procedure add(p:longint);
  begin
    while p<=m do
    begin
      inc(c[p]);
      p:=p+lowbit(p);
    end;
  end;
function ask(p:longint):longint;
  begin
    ask:=;
    while p<> do
    begin
      ask:=ask+c[p];
      p:=p-lowbit(p);
    end;
  end;
procedure sort(l,r: longint);
  var i,j,h,p: longint;
  begin
    i:=l;
    j:=r;
    h:=x[(l+r) div ];
    p:=y[(l+r) div ];
    repeat
      while (x[i]<h) or (x[i]=h) and (y[i]<p) do inc(i);
      while (h<x[j]) or (x[j]=h) and (p<y[j]) do dec(j);
      if not(i>j) then
      begin
        swap(x[i],x[j]);
        swap(y[i],y[j]);
        inc(i);
        j:=j-;
      end;
    until i>j;
    if l<j then sort(l,j);
    if i<r then sort(i,r);
  end; begin
  readln(t);
  for i:= to t do
  begin
    readln(n,m,k);
    for j:= to k do
      readln(x[j],y[j]);
    ans:=;
    sort(,k);
    fillchar(c,sizeof(c),);
    fillchar(a,sizeof(a),);
    inc(a[y[k]]);
    add(y[k]);
    for j:=k- downto do
    begin
      ans:=ans+ask(y[j]-);   //这是唯一要注意的细节,交点一定不能在城市处
      inc(a[y[j]]);
      add(y[j]);
    end;
    writeln('Test case ',i,': ',ans);
  end;
end.
上一篇:PCIE读书笔记


下一篇:【原创】leetCodeOj --- Merge k Sorted Lists 解题报告