• ZOJ 1610 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个区间涂色,全部涂完色后,输出最终可以看到的颜色,及其区间个数。1~2,2~3是一个区间,但是1~2,3~4是两个区间。

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    
    const int N=8010;
    
    struct node
    {
        int left, right, col;
    }no[4*N];
    int color[N], t; ///color数组记录该颜色的区间个数
    
    void Bulid(int left, int right, int root)
    {
        int mid;
    
        no[root].left = left;
        no[root].right = right;
        no[root].col = -1;
    
        if (left == right) return ;
    
        mid = (left+right)/2;
    
        Bulid(left, mid, root*2);
        Bulid(mid+1, right, root*2+1);
    }
    
    void Update(int left, int right, int col, int root)
    {
        int mid;
    
        if (no[root].col == col) return ;
    
        if (no[root].left == left && no[root].right == right)
        {
            no[root].col = col;
            return ;
        }
    
        if (no[root].col >= 0) ///若该区间被涂上了颜色,则向下更新
        {
            no[root*2].col = no[root].col;
            no[root*2+1].col = no[root].col;
            no[root].col = -2;
        }
    
        mid = (no[root].left+no[root].right)/2;
    
        if (right <= mid) Update(left, right, col, root*2);
        else if (left > mid) Update(left, right, col, root*2+1);
        else
        {
            Update(left, mid, col, root*2);
            Update(mid+1, right, col, root*2+1);
        }
    
        no[root].col = -2; ///标记该区间部分或全部被涂色
    }
    
    void Solve(int root)
    {
        if (no[root].col == -1)
        {
            t = -1;
            return ;
        }
    
        if (no[root].col != -2) ///该区间只有一种颜色
        {
            if (no[root].col != t) ///判断该区间颜色与上一区间是否相同,不同则更新color和t的值
            {
                color[no[root].col]++;
                t = no[root].col;
            }
    
            return ;
        }
    
        if (no[root].left != no[root].right) ///向下查找
        {
            Solve(root*2);
            Solve(root*2+1);
        }
    }
    
    int main ()
    {
        int n, a, b, c, i;
    
        while (scanf("%d", &n) != EOF)
        {
            Bulid(0, 8000, 1);
    
            while (n--)
            {
                scanf("%d%d%d", &a, &b, &c);
    
                Update(a+1, b, c, 1); ///区间离散化,防止两个或两个以上连续并颜色相同的区间被合并在一起
            }
    
            memset(color, 0, sizeof(color));
            t = -1;
            Solve(1);
    
            for (i = 0; i <= 8000; i++)
                if (color[i] > 0) printf("%d %d
    ", i, color[i]);
            printf("
    ");
        }
    
        return 0;
    }
  • 相关阅读:
    Java调用wcf
    登录重构小记
    从零打造一个Web地图引擎
    Vue动态组件的实践与原理探究
    揭开Vue异步组件的神秘面纱
    使用vue实现排序算法演示动画
    C#转义字符
    asp.net使用post方式action到另一个页面,在另一个页面接受form表单的值!(报错,已解决!)
    页面关闭事件onbeforeunload 记录
    执行一段sql
  • 原文地址:https://www.cnblogs.com/syhandll/p/4743507.html
Copyright © 2020-2023  润新知