xfstart07
Get busy living or get busy dying
Count the Colors

Time Limit: 1 Second      Memory Limit: 32768 KB

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


题目大意:给出一系列线段和线段所要覆盖的颜色,最后求每种颜色有多少条线段。

总结:

线段树的入门好题。


代码:

 1 /*
 2  * Problem: ZJU 1610
 3  * Author: Xu Fei
 4  * Time: 2010.8.13 11:43
 5  * Method: 线段树-指针实现
 6  */
 7 #include<iostream>
 8 #include<cstdio>
 9 #include<cstring>
10 using namespace std;
11 
12 const int N=8000,M=10000,Noc=-1,Muc=-2;
13 
14 struct Node
15 {
16     int left,right,cover;
17     Node *lc;
18     Node *rc;
19 }*root;
20 int color[M];
21 Node *build(Node *Tree,int left,int right)
22 {
23     Node *P=new Node;
24     P->left=left;
25     P->right=right;
26     P->cover=Noc;
27     P->lc=NULL;
28     P->rc=NULL;
29     if(left<right)
30     {
31         int mid=(left+right)>>1;
32         P->lc=build(P->lc,left,mid);
33         P->rc=build(P->rc,mid+1,right);
34     }
35     return P;
36 }
37 void insert(Node *Tree,int left,int right,int col)
38 {
39     if(Tree->left == left && Tree->right == right)
40     {
41         Tree->cover=col;
42         return;
43     }
44     if(Tree->cover >= 0)
45         Tree->lc->cover=Tree->rc->cover=Tree->cover;
46     Tree->cover=Muc;
47     int mid=(Tree->left+Tree->right)>>1;
48     if(right <= mid)
49         insert(Tree->lc,left,right,col);
50     else if(left > mid)
51         insert(Tree->rc,left,right,col);
52     else
53     {
54         insert(Tree->lc,left,mid,col);
55         insert(Tree->rc,mid+1,right,col);
56     }
57 }
58 void getcount(Node *Tree,int &col)
59 {
60     if(Tree->cover >= 0 && Tree->cover != col)
61     {
62         col=Tree->cover;
63         color[col]++;
64     }
65     else if(Tree->cover == Muc)
66     {
67         getcount(Tree->lc,col);
68         getcount(Tree->rc,col);
69     }
70     else
71         col=Tree->cover;
72 }
73 int main()
74 {
75     int n,i,a,b,c;
76     while(scanf("%d",&n)!=EOF)
77     {
78         root=build(root,1,N);
79         for(i=1;i<=n;++i)
80         {
81             scanf("%d%d%d",&a,&b,&c);
82             insert(root,a+1,b,c);
83         }
84         memset(color,0,sizeof(color));
85         int col=Noc;
86         getcount(root,col);
87         for(i=0;i<M;++i)
88             if(color[i])
89                 printf("%d %d\n",i,color[i]);
90         printf("\n");
91     }
92     return 0;
93 }

 1 /*
 2  * Problem: ZJU 1610
 3  * Author: Xu Fei
 4  * Time: 2010.8.13 15:33
 5  * Method: 线段树-静态实现
 6  */
 7 #include<iostream>
 8 #include<cstdio>
 9 #include<cstring>
10 using namespace std;
11 
12 const int N=8000,M=10000,Noc=-1,Muc=-2;
13 
14 struct Node
15 {
16     int left,right,cover;
17 }tree[N*4];
18 int color[M];
19 
20 void build(int len,int left,int right)
21 {
22     tree[len].left=left;
23     tree[len].right=right;
24     tree[len].cover=Noc;
25     if(left<right)
26     {
27         int mid=(left+right)>>1;
28         build(len<<1,left,mid);
29         build((len<<1)+1,mid+1,right);
30     }
31 }
32 void insert(int len,int left,int right,int col)
33 {
34     if(tree[len].left == left && tree[len].right == right)
35     {
36         tree[len].cover=col;
37         return;
38     }
39     if(tree[len].cover >= 0
40         tree[len<<1].cover=tree[(len<<1)+1].cover=tree[len].cover;
41     tree[len].cover=Muc; 
42     int mid=(tree[len].left+tree[len].right)>>1;
43     if(right <= mid) 
44         insert(len<<1,left,right,col);
45     else if(left > mid) 
46         insert((len<<1)+1,left,right,col);
47     else
48     {
49         insert(len<<1,left,mid,col);
50         insert((len<<1)+1,mid+1,right,col);
51     }
52 }
53 void getcount(int len,int &col)
54 {
55     if(tree[len].cover >= 0 && tree[len].cover != col) 
56     {
57         col=tree[len].cover; 
58         color[col]++;
59     }
60     else if(tree[len].cover == Muc) 
61     {
62         getcount(len<<1,col);
63         getcount((len<<1)+1,col);
64     }
65     else
66         col=tree[len].cover; 
67 }
68 int main()
69 {
70     int n,i,a,b,c;
71     while(scanf("%d",&n)!=EOF)
72     {
73         build(1,1,N); 
74         for(i=0;i<n;++i)
75         {
76             scanf("%d%d%d",&a,&b,&c);
77             insert(1,a+1,b,c); 
78         }
79         memset(color,0,sizeof(color));
80         int col=Noc;
81         getcount(1,col); 
82         for(i=0;i<M;++i)
83             if(color[i])
84                 printf("%d %d\n",i,color[i]);
85         printf("\n");
86     }
87     return 0;
88 }


posted on 2010-08-13 11:45 xfstart07 阅读(446) 评论(2)  编辑 收藏 引用 所属分类: 算法学习ZJU数据结构

FeedBack:
# re: ZJU1610
2011-04-08 17:03 | c++fans
神牛,想问一下那个插入insert(1,a+1,b,c)为什么要a+1呢。  回复  更多评论
  
# re: ZJU1610
2011-04-10 09:56 | xfstart07
@c++fans
因为我是将每个数字看成一个区间,而题目是将两个数看成区间,如我的代码中1表示一个区间,而题目0~1表示一个区间,所有需要a+1或者b-1这样的操作。  回复  更多评论
  

只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理