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

题目大意是给定区间0~8000进行涂色,之后输出各个颜色的段数(不是被涂色点的数量,而是区间量)。用col记录色彩号码,没涂色时记录-1,该区间涂了多种颜色时记录-2。注意,在算颜色段数的时候要用全局变量temp记录上一段的颜色,以此判断是否加1。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
int n,col[32100],num[8100],temp=-1;
void pushdown(int k)
{
	if(col[k]>=0){
		col[k<<1]=col[k<<1|1]=col[k];
		col[k]=-2;
	}
}
void build(int s,int t,int k)
{
	col[k]=-1;
	if(s+1==t){
		num[s]=0;
		return;
	}int m=(s+t)>>1;
	build(s,m,k<<1);
	build(m,t,k<<1|1);
}
void update(int s,int t,int k,int l,int r,int c)
{
	if(col[k]==c)return;
	if(l<=s&&t<=r){
		col[k]=c;
		return;
	}pushdown(k);
	int m=(s+t)>>1;
	if(l<m)update(s,m,k<<1,l,r,c);
	if(m<r)update(m,t,k<<1|1,l,r,c);
	col[k]=-2;
}
void query(int s,int t,int k)
{
	if(s==t)return;
	if(col[k]==-1){
		temp=-1;
		return;
	}
	if(col[k]!=-2){
		if(col[k]!=temp){
			num[col[k]]++;
			temp=col[k];
		}
		return;
	}
	if(s+1!=t){
		int m=(s+t)>>1;
		query(s,m,k<<1);
		query(m,t,k<<1|1);
	}
}
int main()
{
	freopen("data.txt","r",stdin);
	freopen("test.out","w",stdout);
	while(~scanf("%d",&n)){
		build(0,8000,1);
		int maxnum=0;
		for(;n;n--){
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			update(0,8000,1,a,b,c);
			maxnum=max(maxnum,c);
		}temp=-1;
		query(0,8000,1);
		for(int i=0;i<=maxnum;i++)
			if(num[i])printf("%d %d\n",i,num[i]);
		puts("");
	}
	return 0;
}



 

posted @ 2016-07-01 16:33  keshuqi  阅读(125)  评论(0编辑  收藏  举报