【ZOJ 1610】Count the Colors

问题描述
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.
输入
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.
输出
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.
样例输入
5 //五条线段
0 4 4 //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
样例输出
1 1
2 1
3 1

1 1

0 2
1 1
算法讨论
题目大概就是说一段1-8000的线段,现在给一堆线段覆盖它,问每种颜色有多少条线段。我们定义t[p,3]=-1时为没被完全覆盖,若被完全覆盖则表上颜色序号,统计时记录上一个点的颜色,若当前点和上一个点是同一颜色则不加,否则加一。

const
  maxn=1000000;
var
  t:array[0..maxn,1..3] of longint;
  f:array[0..maxn] of longint;
  i,n,m,x,y,s,c:longint;

function count(p:longint):longint;
var
  tl,tr:longint;
begin
  if t[p,3]=-2
    then begin
           c:=-2;
           exit
         end
    else if (c<>t[p,3]) and (t[p,3]<>-1)
           then begin
                  c:=t[p,3];
                  inc(f[c])
                end
           else if (t[p,2]-t[p,1]=1) and (c=t[p,3])
                  then exit
                  else if t[p,3]=-1
                         then begin
                                count(p*2);
                                count(p*2+1)
                              end;
end;

procedure insert(p,a,b:longint);
var
  m:longint;
begin
  m:=(t[p,1]+t[p,2]) div 2;
  if (t[p,1]=a) and (t[p,2]=b)
    then t[p,3]:=c
    else begin
           if t[p,3]<>-1
             then begin
                    t[p*2,3]:=t[p,3];
                    t[p*2+1,3]:=t[p,3];
                    t[p,3]:=-1
                  end;
           if b<=m
             then insert(p*2,a,b)
             else if a>=m
                    then insert(p*2+1,a,b)
                    else begin
                           insert(p*2,a,m);
                           insert(p*2+1,m,b)
                         end;
         end;
end;

procedure create(p:longint);
var
  m:longint;
begin
  if t[p,2]-t[p,1]>1
    then begin
           m:=(t[p,1]+t[p,2]) div 2;
           t[p*2,1]:=t[p,1]; t[p*2,2]:=m;
           t[p*2+1,1]:=m; t[p*2+1,2]:=t[p,2];
           create(p*2);
           create(p*2+1)
         end;
end;

begin
  t[1,1]:=0; t[1,2]:=8000;
  create(1);
  while not eof do
    begin
      fillchar(f,sizeof(f),0);
      for i:=1 to maxn do
        t[i,3]:=-2;
      readln(m);
      for i:=1 to m do
        begin
          readln(x,y,c);
          insert(1,x,y)
        end;
      c:=-3;
      count(1);
      for i:=0 to maxn do
        if f[i]<>0
          then writeln(i,' ',f[i]);
      writeln
    end;
end.

这里写图片描述
Pixiv ID:36408671

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值