zoukankan      html  css  js  c++  java
  • Count the Colors

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

    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
    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

     
    /*
    题意:在数轴上用不同的颜色画线段,每一次画,可以覆盖上一次的颜色,问你所有操作完之后可以有多少种颜色,每种能看见的颜色的线段数
    
    初步思路:线段树区间染色问题,实际上可以转化为一个区间set问题,每次操作就是进行一个染色
    
    #错误:因为是闭区间,所以实际染色应该是[l,r);而且查询的时候不能在区间查询了,如果这样的话,在一个连续线段上的染色可能被记录两
        次
    */
    #include <bits/stdc++.h>
    using namespace std;
    /****************************线段树基础模板*********************************/
    const int maxn=8000+5;
    
    #define lson i*2, l, m
    #define rson i*2+1, m+1, r
    int color[maxn];//用来存放每种颜色的节点数的数组
    int sum[maxn];
    struct Segtree{
    
        int setv[maxn<<2];//记录以每个节点为根节点的线段的颜色
    
        void PushDown(int i)
        {
            if(setv[i]!=-1){
                setv[i*2]=setv[i*2+1]=setv[i];
                setv[i]=-1;
            }
        }
    
        void build(int i,int l,int r)
        {
            // cout<<l<<" "<<r<<endl;
            setv[i]=-1;//将每个节点都初始化为-1也就是什么颜色都没有
            if(l==r)
                return ;
            int m=(l+r)>>1;
            build(lson);
            build(rson);
        }
        void query(int ql,int qr,int i,int l,int r)
        {
            if(l==r){
                sum[l]=setv[i];
                return ;
            }
            PushDown(i);
            int m=(l+r)>>1;
            if(ql<=m)query(ql,qr,lson);
            if(m<qr)query(ql,qr,rson);
        }
    
        void update(int ql,int qr,int val,int i,int l,int r)
        {
            if(ql<=l&&r<=qr)
            {
                setv[i]=val;//更新这个节点的值
                return ;
            }
            PushDown(i);//先向下更新
            int m=(l+r)>>1;
            if(ql<=m) update(ql,qr,val,lson);
            if(m<qr) update(ql,qr,val,rson);
        }
    };
    Segtree segtree;
    /****************************线段树基础模板*********************************/
    int l,r,c;
    int n;
    void init(){
        memset(color,0,sizeof color);
        memset(sum,-1,sizeof sum);
    }
    int main(){
        // freopen("in.txt","r",stdin);
        while(scanf("%d",&n)!=EOF){
            init();
            segtree.build(1,1,maxn-1);
            for(int i=0;i<n;i++){
                scanf("%d%d%d",&l,&r,&c);
                segtree.update(l+1,r,c,1,1,maxn-1);
            }
            segtree.query(1,maxn-1,1,1,maxn-1);
            // for(int i=0;i<=4;i++){
            //     cout<<sum[i]<<" ";
            // }cout<<endl;
            for(int i=1;i<maxn;i++){
                while(i!=0&&sum[i]!=-1&&sum[i]==sum[i-1])//跑完连续的区间
                    i++;  
                color[sum[i]]++;
            }
            for(int i=0;i<maxn;i++){
                if(color[i]>0){
                    printf("%d %d
    ",i,color[i]);
                }
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    HDFS原理分析之HA机制:avatarnode原理
    [转]Hessian——轻量级远程调用方案
    [转]成为Java顶尖程序员 ,看这11本书就够了
    [转]Java四种线程池的使用
    [转]Java多线程
    IntelliJ IDEA 快捷键
    [转]django 日志logging的配置以及处理
    [转]使用TortoiseGit处理代码冲突
    动软DbHelperSQL
    [转]Entity Framework 的实体关系
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6606650.html
Copyright © 2011-2022 走看看