Count the Colors (线段树,暴力)

上题:

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.


<b< dd="">

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.


<b< dd="">

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


<b< dd="">

Sample Output


1 1
2 1
3 1

1 1

0 2
1 1

题意:题目的意思就是,给你1-8000的区间,之后让你在这个区间中染色,涂色 可以覆盖,问你最后可以看到多少种颜色
做法,暴力for循环是可以水过的,但是正确的做法是线段树,从区间更新这里应该是可以看出来用线段树的吧,那做法就是普通的区间更新,还是一样 更新不更新到底,到用的时候在更新,没什么可说的,接下来就是暴力统计各种颜色就好了上代码吧 
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1//我右子树的孩子忘了加一,卡了一中午。。。 
const int maxn=8888;
using namespace std;
int col[maxn<<2],sum[maxn<<2],ans[maxn];
void pushdown(int rt)
{
	if(col[rt]!=-1)
	{
		col[rt<<1]=col[rt<<1|1]=col[rt];
		col[rt]=-1;
	}
}
void build(int l,int r,int rt)
 {  
    col[rt]=-1;  
    if(l==r)return;  
    int m=(l+r)>>1;  
    build(lson);  
    build(rson);  
}  
void update(int L,int R,int c,int l,int r,int rt)
{
	if(L<=l&&r<=R)
	{
		col[rt]=c;
		return ;
	}
	pushdown(rt);
	int m=(l+r)>>1;
	if(L<=m)
	{
		update(L,R,c,lson);
	}
	if(m<R)
	{
		update(L,R,c,rson);
	}
} 
void query(int L,int R,int l,int r,int rt)
{
	if(l==r)//查询的时候将放回区间的颜色 
	{
		sum[l]=col[rt];
		return ;
	}
	pushdown(rt);
	int m=(r+l)>>1;
	if(L<=m)
	{
		query(L,R,lson);
	}
	if(m<R)
	{
		query(L,R,rson);
	}
}
int main()
{
	int n;
	int a,b,c;
	int ca=0;
	while(scanf("%d",&n)!=EOF)
	{
		memset(ans,0,sizeof(ans));
		memset(col,-1,sizeof(col));//我看了看 建树是要60ms的,直接memset 20ms过,再说这道题本来就是不用建树的,更新的时候就直接覆盖掉了 
		//build(0,maxn-1,1);
		for(int i=0;i<n;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			if(a==b)
			{
				continue;
			}
			update(a,b-1,c,0,maxn-1,1);	
		
		}
		query(0,maxn-1,0,maxn-1,1);//最后全部更新到底部 
		for(int i=0;i<maxn;i++)//暴力求出颜色的信息 
		{
			while(sum[i]!=-1&&sum[i]==sum[i+1])
			{
				i++;
			}
			ans[sum[i]]++;
		}
		for(int i=0;i<maxn;i++)
		{
			if(ans[i])
			{
				printf("%d %d\n",i,ans[i]);
			}
		}
		printf("\n");// 少了就是格式错误
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值