• ZOJ 1610 Count the Colors


     图片

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

    仅仅只是一道线段树水题。。。。。
    本来想根据先序遍历找线段个数。。。。但是最后越调越麻烦。。。。。。 
    最后改成,整个扫一遍。。。。。。


    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



    Author: Standlove
    Source: ZOJ Monthly, May 2003


    #include <iostream>
    #include <cstdio>
    #include <cstring>

    using namespace std;

    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1

    const int MAXN=8008;
    int col[MAXN<<2],ak[8008],q[8008];
    int mmmx=-1;
    void pushDOWN(int rt)
    {
        if(col[rt]!=-1)
        {
            col[rt<<1]=col[rt<<1|1]=col[rt];
            col[rt]=-1;
        }
    }

    void build(int l,int r,int rt)
    {
        col[rt]=-1;
        if(l==r) return ;
        int m=(l+r)>>1;
        build(lson);
        build(rson);
    }

    void update(int L,int R,int c,int l,int r,int rt)
    {
        if(L<=l&&r<=R)
        {
            col[rt]=c;
            return;
        }
        int m=(l+r)>>1;
        if(col[rt]!=-1)
           pushDOWN(rt);
        if(L<=m) update(L,R,c,lson);
        if(R>m) update(L,R,c,rson);
        return ;
    }

    void query(int l,int r,int rt)
    {
        if(col[rt]!=-1)
        {
            for(int i=l;i<=r;i++)
            {
                q=col[rt];
            }
            return ;
        }
        if(l!=r&&col[rt]==-1)
        {
            int m=(l+r)>>1;
            query(lson);
            query(rson);
        }
    }

    int main()
    {

        int t;
    while(scanf("%d",&t)!=EOF)
    {
        memset(col,-1,sizeof(col));
        memset(q,-1,sizeof(q));
        memset(ak,0,sizeof(ak));
        int n=8008;
        build(1,n,1);
        int a,b,c;
    for(int i=0;i<t;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        update(a+1,b,c,1,n,1);
    }

    //show(1,n,1);
        query(1,n,1);

        for(int i=0;i<=8000;i++)
        {
            if(q[i+1]!=q&&q!=-1)
            {
                ak[q]++;
            }
        }

        for(int i=0;i<=8000;i++)
        {
            if(ak)
            {
                printf("%d %d ",i,ak);
            }
        }
        putchar(10);
    }
        return 0;
    }
    * This source code was highlighted by YcdoiT. ( style: Emacs )


    友情提供测试数据:
    2
    0 1 2
    3 4 2

    4
    789 864 521
    727 742 8
    100 8000 9
    24 766 0
     
    8
    123 324 0
    213 545 0
    23 7884 0
    2314 2939 888
    623 3324 32
    313 5445 2
    233 7884 0
    2314 2939 888 

    答案:  2 2
                
                 0 1 
                 9 1

                 0  2
                 888  1 



  • 相关阅读:
    文件流:"fopen","fclose",“ftell”"fseek","fgets","fprintf" ,“feof”,"fwrite","fread"
    “/”和“\”和feof();
    VS快捷键说明
    vs2015安装VAssistX以后,去除中文注释会有红色下划线方法
    QT5.8+vs2015配置以及qt creater中出现中文乱码解决办法之一
    将ascll码转换成数值进行运算
    二维数组---指针数组和数组指针
    source insight 4.0.086破解
    make clean 和make distclean的区别
    长歌行
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350907.html
Copyright © 2020-2023  润新知