zoj 1610 Count the Colors

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610

 Count the Colors
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

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//第0个区间涂颜色1
3 4 1//第3个区间涂颜色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次操作后问每种颜色在线段上有多少个间断的区间

这道题可用线段树写,

 (0, 1)为区间0,(1, 2)为区间1,,(2, 3)为区间2   ...
 
该代码未用线段树,用一般方法写的
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
#define N 8010

using namespace std;

int color[N], counts[N];
int main()
{
    int n, i, a, b, c, Min, Max;
    while(scanf("%d", &n) != EOF)
    {
        memset(color, -1, sizeof(color));//记录区间被涂的颜色
        memset(counts, 0, sizeof(counts));//记录每种颜色的区间个数
        Min = INF;
        Max = -INF;
        while(n--)
        {
            scanf("%d%d%d", &a, &b, &c);
            Min = min(a, Min);
            Max = max(b, Max);
            for(i = a ; i < b ; i++)
                color[i] = c;//第i个区间被涂上类型为c的颜色
        }
        for(i = Min + 1 ; i <= Max - 1 ; i++)
            if(color[i] != color[i - 1] && color[i - 1] != -1)
                counts[color[i - 1]]++;//统计每种颜色的个数
        if(color[i - 1] != -1)
            counts[color[i - 1]]++;
        for(i = 0 ; i < N ; i++)
            if(counts[i] != 0)
                printf("%d %d\n", i, counts[i]);//i表示颜色种类,counts[i]涂i种颜色的区间个数
        printf("\n");
    }
    return 0;
}

 



 
posted @ 2015-08-03 10:41  午夜阳光~  阅读(144)  评论(0编辑  收藏  举报