【ZOJ 1610】Count the Colors

来源:互联网 发布:php是最好的语言 编辑:程序博客网 时间:2024/05/14 17:49

问题描述
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
原创粉丝点击