Count The colors_zoj1610_线段树

Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.

Your task is counting the segments of different colors you can see at last.

 

Input

 

The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments. 

Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces: 

x1 x2 c 

x1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment. 

All the numbers are in the range [0, 8000], and they are all integers. 

Input may contain several data set, process to the end of file. 

 

Output 

Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index. 

If some color can't be seen, you shouldn't print it. 

Print a blank line after every dataset. 

 

Sample Input 

5

0 4 4

0 3 1

3 4 2

0 2 2

0 2 3

4

0 1 1

3 4 1

1 3 2

1 3 1

6

0 1 0

1 2 1

2 3 1

1 2 0

2 3 0

1 2 1

  

Sample Output 

1 1

2 1

3 1

 

1 1

 

0 2

1 1

 

翻完一堆英语,讲讲题目大意 

已知有n条不同颜色的线段且新线段可以覆盖在旧线段上。求各颜色的线段各出现多少次


思路:

颜色覆盖!线段树!color记录当前节点的颜色,最后统计输出。比较简单而且和例3类似就不细说了。


源代码/pas:

type
tree=record
  l,r,c:longint;
end;
var
  tmp,n:longint;
  t,g:array[0..40000]of tree;
  ans:array[0..10000]of longint;
procedure insert(f,x,y,color:longint);
var
  mid:longint;
begin
  mid:=(t[f].l+t[f].r)div 2;
  if (t[f].c<>-1)and(t[f].l+1<>t[f].r) then
  begin
    t[f*2].c:=t[f].c;
    t[f*2+1].c:=t[f].c;
  end;
  if t[f].l+1<>t[f].r then t[f].c:=-1;
  if (x=t[f].l)and(y=t[f].r) then
  t[f].c:=color
  else
  begin
    if (x>=mid) then
    insert(f*2+1,x,y,color)
    else
    if (y<=mid) then
    insert(f*2,x,y,color)
    else
    begin
      insert(f*2,x,mid,color);
      insert(f*2+1,mid,y,color);
    end;
  end;
end;
procedure count(f:longint);
var
  mid:Longint;
begin
  if t[f].c<>-1 then
  begin
    if tmp<>t[f].c then
    begin
      inc(ans[t[f].c]);
      tmp:=t[f].c;
    end;
    exit;
  end;
  if t[f].l+1<>t[f].r then
  begin
    count(f*2);
    count(f*2+1);
  end
  else
  tmp:=-1;
end;
procedure build(f:longint);
var
  mid:longint;
begin
  if t[f].r-t[f].l=1 then exit;
  mid:=(t[f].l+t[f].r)div 2;
  t[f*2].l:=t[f].l;
  t[f*2].c:=t[f].c;
  t[f*2].r:=mid;
  t[f*2+1].l:=mid;
  t[f*2+1].r:=t[f].r;
  t[f*2+1].c:=t[f].c;
  build(f*2);
  build(f*2+1);
end;
procedure init;
var
  n,m,i,j,x,y,z:longint;
begin
  t[1].l:=0;
  t[1].r:=8000;
  t[1].c:=-1;
  build(1);
  g:=t;
  while not eof do
  begin
    readln(m);
    for i:=1 to m do
    begin
      readln(x,y,z);
      insert(1,x,y,z);
    end;
    tmp:=-1;
    count(1);
    for i:=0 to 8001 do
    if ans[i]<>0 then
    writeln(i,' ',ans[i]);
    writeln;
    t:=g;
    fillchar(ans,sizeof(ans),0);
  end;
end;
begin
  init;
end.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值