zoj1610( 线段树)(Count the Colors)

zoj1610( 线段树)(Count the Colors)
Count the Colors


Time Limit: 2 Seconds      Memory Limit: 65536 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

My  soltion:

/*2015.11.14*/

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int b;
struct  stu
{
	int color;
	int right,left;
 }tree[20000];
 int ans[20000],res[8800];
 void build(int t,int l,int r)
 {
 	int mid;
 	tree[t].color=-1;
 	tree[t].left=l;
 	tree[t].right=r;
 	if(l==r)
 	return ;
 	mid=(l+r)>>1;
 	build(t<<1,l,mid);
 	build(t<<1|1,mid+1,r);//t<<1后相当于乘于2,然后t的最后一位二进位为0,然后再与1相与,相当于加1 
 }                     //注意:t<<1|1等价于(t<<1)+1,而 t<<1+1等价于t<<(1+1)==t*4==t*2^2 
 void update(int t,int left,int right,int color)
 {
 	int mid,i;
 	if(tree[t].left==left&&tree[t].right==right)
 	{
 		for(i=left;i<=right;i++)//把对应区间的点(除右端点外)全部着色 
 		if(i!=b)//判断是不是原区间最右端的端点值,若是则不改变其值。 
 		ans[i]=color;
 	     return ;
	 }
	  mid=(tree[t].left+tree[t].right)>>1;
	  if(right<=mid)
	  update(t<<1,left,right,color);
	  else if(mid<left)
	  update(t<<1|1,left,right,color);
	  else
	  {
	  	update(t<<1,left,mid,color);
	 	update(t<<1|1,mid+1,right,color);
	  }
}
 int main()
 {
 	int i,j,k,a,c;
 	while(scanf("%d",&k)==1)
 	{
 		memset(ans,-1,sizeof(ans));
 		memset(res,0,sizeof(res));
 		build(1,0,8002);/*线段树,左起点从0开始,刚开始没注意从1开始,题目中的区间有从0开始,因此起点必须从0开始*/
 		for(i=0;i<k;i++)
 		{
 		  scanf("%d%d%d",&a,&b,&c);
 		  if(a>b)
 	      swap(a,b);
 	      update(1,a,b,c);
 	      //ans[b]=-1;见下面注释。 
		}              
		if(ans[0]!=-1) 
		res[ans[0]]++;
		for(i=1;i<8008;i++)
		{
			if(ans[i]!=ans[i-1]&&ans[i]!=-1)
			res[ans[i]]++;
		}
		for(i=0;i<8005;i++)
		if(res[i])
		printf("%d %d\n",i,res[i]);	
		printf("\n");
	 }	
 	return 0;
 }
//区间最右边的ans[right]不能被该对应区间的color赋值,ans[right]该元素之前的值是个不确定的值,可能是-1也可能不是 ,所以在这里不能恢复ans[right]之前的值.要在函数update内不改变 ans[right]之前的值,因此把b(即right)定义为全局变量,从而在 函数update内部通过判断是否是ans[b],若是,就不改变ans[right](即ans[b]) 
  例如:(先对b~c着颜色:2,再对a~b着颜色1,最后对c~d着颜色:3) 
                                                      a              b              c                d
第一次着色:                                               2              2
第二次着色:                              1              1
第三次着色:                                                               3                

数字覆盖后的结果:                  1              1             3                 3
上面的最终覆盖结果说明:a~b之间的颜色为:1,c~d之间的颜色为:3,b~c之间没有颜色(或被忽略),
最终结果只有2个颜色区间段 。 
正确结果是:a~b之间的颜色为:1,b~c之间的颜色为:2,c~d之间的颜色为:3。
最终正确结果应有3个颜色区间段.


每个区间的最右端不着色: 
                                           a               b              c                d
第一次着色:                                     2              
第二次着色:                   1              
第三次着色:                                                     3               ~

数字覆盖后的结果:       1               2              3              (-1)
上面的最终覆盖结果说明:a~b之间的颜色为:1,b~c之间的颜色为:2,c~d之间的颜色为:3。
最终正确结果有3个颜色区间段. 


若只对区间左端点着色:(先对a~b着颜色1,再对c~d着颜色:2,最后对b~d着颜色:3,) 
                                          a               b              c                d
第一次着色:                  1                            
第二次着色:                                                    2                 
第三次着色:                                   3                                  
数字覆盖后的结果:      1               3              2               (-1)

                                                                                               
上面的最终覆盖结果说明:a~b之间的颜色为:1,b~c之间的颜色为:3,c~d之间的颜色为:2。
最终结果有3个颜色区间段 。
而正确结果应为两个颜色区间端:a~b为颜色1,b~d为颜色3,原来的c~d的区间颜色被覆盖。

                                          a               b              c                d

                                                                                                
综上可得:对应颜色区间内的所有点除最右端点外全部都需着色。
注意:t<<1后相当于乘于2,然后t的最后一位二进位为0,然后再与1相与,相当于加1 
注意:t<<1|1等价于(t<<1)+1,而 t<<1+1等价于t<<(1+1)==t*4==t*2^2 

“+”、“-”的优先级高于“<<”、“>>”。

“<<”二进制位左移一位,相当于乘以2,“>>”,二进制位右移一位,相当于除以2(整除,如:int m,m/2)。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值