开花 (Standard IO)

题意/Description:

       在遥远的火星上,上面的植物非常奇怪,都是长方形的,每个植物用三个数来描述:左边界L、右边界R以及高度H,如下图所示描述一个植物:L=2,R=5和H=4。


       每天都有一个新植物长出来,第一天的植物高度为1,后面每天长出的植物比前一天的高1。
  当一个新植物长出来的时候,跟其他植物的水平线段相交处会长出一朵小花(前提是之前没有长出花朵),如果线段交于端点,是不会长花的。
  下图为样例1的示意图:


       给出每天的植物的坐标,计算每天长出多少新花。

 

读入/Input

       第一行包含一个整数N(1<=N<=100000),表示天数。
  接下来N行,每行两个整数L和R(1<=L<=R<=100000),表示植物的左右边界。

 

输出/Output

       输出每天长出新植物后增加新花的数量。

 

题解/solution

就是个线段树。请看:

http://blog.csdn.net/a_loud_name/article/details/51910169

 

代码/Code

 

type
  arr=record
    l,r,cover:longint;
  end;
var
  tree:array [0..800001] of arr;
  a:array [0..100001] of longint;
  n,ans:longint;
procedure cre(p,x,y:Longint);
var
  mid:longint;
begin
  with tree[p] do
    begin
      l:=x; r:=y;
      if y=x then exit;
      mid:=(x+y) div 2;
      cre(p*2,x,mid);
      cre(p*2+1,mid+1,y);
    end;
end;

procedure count(p,x,y:longint);
var
  mid:longint;
begin
  with tree[p] do
    begin
      ans:=ans+cover;
      mid:=(l+r) div 2;
      if (l=x) and (r=y) then exit;
      if y<=mid then count(p*2,x,y) else
        if x>=mid+1 then count(p*2+1,x,y) else
          begin
            count(p*2+1,x,mid);
            count(p*2+1,mid+1,y);
          end;
    end;
end;

procedure ins(p,x,y:longint);
var
  mid:longint;
begin
  with tree[p] do
    begin
      mid:=(l+r)div 2;
      if (x=l) and (y=r) then
        begin
          inc(cover);
          exit;
        end;
        if (y<=mid) then ins(p*2,x,y) else
          if (x>=mid+1) then ins(p*2+1,x,y) else
            begin
              ins(p*2,x,mid);
              ins(p*2+1,mid+1,y);
            end;
    end;
end;

procedure init;
var
  i,x,y,t,k:longint;
begin
  readln(n);
  cre(1,1,100001);
  for i:=1 to n do
    begin
      readln(x,y);
      ans:=0;
      count(1,x,x);
      t:=ans;
      count(1,y,y);
      k:=ans-t;
      writeln(ans-a[x]-a[y]);
      a[x]:=t+1;
      a[y]:=k+1;
      ins(1,x,y);
    end;
end;

begin
  init;
end.



原文地址:https://www.cnblogs.com/zyx-crying/p/9319647.html