• ZOJ 1610 Count the Colors(线段树)


    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

    题意:给了每一线段的颜色,存在颜色覆盖,求表面上看能看到的颜色种类和各种颜色的段数

     

    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    const int MAXN=8010;
    struct Node
    {
        int l,r;
        int color;
    }segTree[MAXN*3];
    int color[MAXN];
    int temp;
    void Build(int i,int l,int r)
    {
        segTree[i].l=l;
        segTree[i].r=r;
        segTree[i].color=-1;//-1表示没有颜色
        if(l+1==r)return;
        int mid=((l+r)>>1);
        Build(i<<1,l,mid);
        Build((i<<1)|1,mid,r);
    }
    void insert(int i,int l,int r,int c)
    {
        if(l==r)return;
        if(segTree[i].color==c)return;
        if(l<=segTree[i].l&&r>=segTree[i].r)
        {
            segTree[i].color=c;
            return;
        }
        if(segTree[i].color>=0)//存在颜色,往下更新
        {
            segTree[i<<1].color=segTree[i].color;
            segTree[(i<<1)|1].color=segTree[i].color;
            segTree[i].color=-2;//表示有多种颜色
        }
        int mid=((segTree[i].l+segTree[i].r)>>1);
        if(r<=mid) insert(i<<1,l,r,c);
        else if(l>=mid) insert((i<<1)|1,l,r,c);
        else
        {
            insert(i<<1,l,mid,c);
            insert((i<<1)|1,mid,r,c);
        }
        segTree[i].color=-2;
    }
    void Count(int i)//统计各颜色的段数
    {
        if(segTree[i].color==-1)
        {
            temp=-1;
            return;
        }
        if(segTree[i].color!=-2)
        {
            if(segTree[i].color!=temp)//temp存的是前一段的颜色
            {
                color[segTree[i].color]++;
                temp=segTree[i].color;
            }
            return;
        }
        if(segTree[i].l+1!=segTree[i].r)
        {
            Count(i<<1);
            Count((i<<1)|1);
        }
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int n,a,b,c;
        int Max;
        while(scanf("%d",&n)!=EOF)
        {
            Build(1,0,8000);
            Max=0;
            while(n--)
            {
                scanf("%d%d%d",&a,&b,&c);
                insert(1,a,b,c);
                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;
    }

     

     

  • 相关阅读:
    Java学习笔记
    Winform中ListView设置了ColumnHeader不显示问题
    GitHub私有仓库为他人授权
    (转)一步一步教你如何在GitHub上上传自己的项目
    【转】C#中的Explicit和Implicit
    C#对象深表复制方法
    mongodb 设置 ssl
    zabbix 4.0.1部署
    redis编译安装
    VMware网络连接模式——桥接模式、NAT模式以及仅主机模式的介绍和区别
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2631449.html
Copyright © 2020-2023  润新知