ZOJ-1610 Count the Colors ( 线段树 )

本文介绍了一种区间涂色问题的解决方法,通过构建区间树并进行插入操作,实现对区间进行不同颜色的涂色,并统计最终每个颜色的可视区间数量。文章详细解释了代码实现细节,包括树的构建、插入及查询过程。

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610

Description

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

 

在[0, 8000]涂色,后涂的会覆盖前面涂的,求每种颜色最后能分辨的的区块有多少个。

建树时,树的单位是区间,而不是点 如[0, 8000]的左右儿子应该分别是[0, 4000], [4000,8000];

询问时,需要判断相邻区间的颜色是否相同 用temp记录前一区间所记录的颜色来解决这个问题。

 

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 
 5 int color[8010];
 6 int mark[20000];
 7 int temp;
 8 
 9 void Build( int i, int l, int r ){
10     mark[i] = -1;//-1表示没有涂
11 
12     if( l + 1 == r ) return;
13 
14     int mid = ( l + r ) >> 1;
15     Build( i << 1, l , mid );
16     Build( ( i << 1 ) | 1, mid, r );
17 }
18 
19 void Insert( int i, int l, int r, int z, int y, int c ){
20     if( l == r ) return;
21     if( z <= l && y >= r ){
22         mark[i] = c;
23         return;
24     }
25     if( mark[i] == c ) return;
26     if( mark[i] >= 0 ){
27         mark[i << 1] = mark[i];
28         mark[( i << 1) | 1] = mark[i];
29         mark[i] = -2;//该区间含有多个颜色
30     }
31     int mid = ( l + r ) >> 1;
32     if( y <= mid ) Insert( i << 1, l, mid, z, y, c );
33     else if( z >= mid ) Insert( ( i << 1 ) | 1, mid, r, z, y, c );
34     else{
35         Insert( i << 1, l, mid, z, mid, c );
36         Insert( ( i << 1 ) | 1, mid, r, mid, y, c );
37     }
38     mark[i] = -2;
39 }
40 
41 void Queue( int i, int l, int r){
42     if( mark[i] == -1 ){
43         temp = -1;
44         return;
45     }
46     if( mark[i] >= 0 ){
47         if( temp != mark[i] ){
48             temp = mark[i];//记录前一段的颜色
49             color[mark[i]]++;
50         }
51         return;
52     }
53     if( l + 1 != r ){
54         int mid = ( l + r ) >> 1;
55         Queue( i << 1, l, mid );
56         Queue( ( i << 1) | 1, mid, r ); 
57     }
58 }
59 
60 int main(){
61     ios::sync_with_stdio( false );
62 
63     int n;
64     while( cin >> n ){
65 
66         Build( 1, 0, 8000 );
67         memset( color, 0, sizeof( color ) );
68 
69         int a, b, c, max = 0;
70         for( int i = 1; i <= n; i++ ){
71             cin >> a >> b >> c;
72             Insert( 1, 0, 8000, a, b, c );
73             max = c > max ? c : max;
74         }
75 
76         temp = -1;
77         Queue( 1, 0, 8000 );
78 
79         for( int i = 0; i <= max ; i++ )
80             if( color[i] ) cout << i << " " << color[i] << endl;
81 
82         cout << endl;
83     }
84 
85     return 0;
86 }

 

转载于:https://www.cnblogs.com/hollowstory/p/5334860.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值