ZOJ-1610 Count the Colors ( 线段树 )
Posted 「空白」物語
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZOJ-1610 Count the Colors ( 线段树 )相关的知识,希望对你有一定的参考价值。
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610
Description
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 }
以上是关于ZOJ-1610 Count the Colors ( 线段树 )的主要内容,如果未能解决你的问题,请参考以下文章
ZOJ - 1610 Count the Colors 线段树区间修改
zoj 1610 Count the Colors 区间覆盖 求染色段