【线段树】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.


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

题目大意:给n个区间段,注意是线段不是点,把[x,y]段染成颜色z,最后输出每种颜色连续的线段的个数,有颜色的才输出;

由于这几天一直在学习线段树,刚开始一直往线段树那方面想最后试了一下暴力,结果AC了,还是很惊喜,告诉大家一道题可能有多个思路,不要一个思路死钻;

上代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<stack>
#include<ctype.h>
#include<stdlib.h>
#include<map>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int M=8005;
int main()
{
    int n,t[M];
    while(~scanf("%d",&n))
    {
        memset(t,-1,sizeof(t));
        int j,i,a,b,c,max1=-1,max2=-1;
        int ans[M]= {0};
        for(i=0; i<n; i++)
        {
            scanf("%d %d %d",&a,&b,&c);
            max1=max(max1,b);//记录染有颜色的最右端
            max2=max(max2,c);//记录染颜色的最大数值
            
            for(j=a+1; j<=b; j++)//注意这里,题目中的说的是线段,所以我们每次不标记最左端
                t[j]=c;
        }
        for(i=1; i<max1; i++)
        {
            if(t[i]!=t[i+1]&&t[i]!=-1)
                ans[t[i]]++;
        }
        ans[t[max1]]++;
        for(i=0; i<=max2; i++)
        {
            if(ans[i]>0)
                printf("%d %d\n",i,ans[i]);
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41984014/article/details/80356592