zoj--1610

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

题目大意:一条线段,长8000,把它的某些部分涂上颜色,注意涂的是子线段,而不是点(可以看一下下面给的数据),涂n个子线段,问最后可以看到哪些颜色,每种颜色占了几段。

提供数据:2

                   0    1      1

                   2    3      1

输出:    1   2

这道题由于数据值最大最为8000,所以模拟一遍也可以过

代码如下:

#include<stdio.h>
#include<string.h>
int color[8010];
int ans[8010];
int main(){
	int n,a,b,c,max,sum;
	while(scanf("%d",&n)!=EOF){
		max=-1;sum=-1;
		memset(color,0,sizeof(color));
		memset(ans,0,sizeof(ans));
		for(int i=1;i<=n;i++){
			scanf("%d%d%d",&a,&b,&c);
			if(b>max)max=b;
			if(c>sum)sum=c;
		  for(int j=a;j<b;j++){
			color[j]=c+1;//记录每个区间段的颜色信息 
		}
		}
		for(int i=0;i<max;i++){
			while(i!=0&&i<max&&color[i]==color[i-1])
			i++;
			if(color[i]){
				ans[color[i]-1]++;
			}
		}
		for(int i=0;i<=sum;i++){
			if(ans[i])
			printf("%d %d\n",i,ans[i]);
		}
		printf("\n");
	}
	return 0;
}  

下面用的是线段树做的

代码如下:
#include<stdio.h>
#include<string.h>
#define MAX 8000
struct stu{
	int l,r,c;
};
stu node[MAX<<2];
int color[MAX+10];
int temp;
void PUTDOWN(int rt){
	if(node[rt].c>=0){//此处注意,不是node[rt]!=-1;
		node[rt<<1].c=node[rt].c;
		node[rt<<1|1].c=node[rt].c;
		node[rt].c=-2;
	}
}
void build(int l,int r,int rt){
	node[rt].l=l;
	node[rt].r=r;
	node[rt].c=-1;
	if(l+1==r)return ;//对线段染色 
	int mid=(l+r)>>1;
	build(l,mid,rt<<1);
	build(mid,r,rt<<1|1);
}
void updata(int l,int r,int c,int rt){
	if(node[rt].c==c)
	 return ;
	if(node[rt].l==l&&node[rt].r==r){
		node[rt].c=c;
		return ;
	}
	PUTDOWN(rt);
	int mid=(node[rt].l+node[rt].r)>>1;
	if(r<=mid) updata(l,r,c,rt<<1);
	else if(l>=mid) updata(l,r,c,rt<<1|1);
	else{
		updata(l,mid,c,rt<<1);
		updata(mid,r,c,rt<<1|1);
	}
	node[rt].c=-2;
}
void Count(int rt){
	if(node[rt].c==-1){
		temp=-1;//有隔离带了,temp重新赋制为-1 
		return ;
	}
	if(node[rt].c!=-2){
		if(node[rt].c!=temp){
			temp=node[rt].c;//temp记录前一段的颜色 
			color[node[rt].c]++;
		}
		return ;
	}
	if(node[rt].l+1!=node[rt].r){//判断是否到叶结点 
		Count(rt<<1);
		Count(rt<<1|1);
	}
}
int main(){
	int n,a,b,c,max;
	while(scanf("%d",&n)!=EOF){
		build(0,8000,1);
		max=-1;
		for(int i=1;i<=n;i++){
			scanf("%d%d%d",&a,&b,&c);
			updata(a,b,c,1);
			if(c>max)max=c;
		}
	    temp=-1;
	    memset(color,0,sizeof(color));
	    Count(1);
	    for(int i=0;i<=max;i++){
	    	if(color[i])
	    	printf("%d %d\n",i,color[i]);
	    }
	    printf("\n");
	}
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值