zoukankan      html  css  js  c++  java
  • ZOJ1610(经典线段树涂色问题)

    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

    #include"cstdio"
    #include"cstring"
    using namespace std;
    const int MAXN=8005;
    struct node{
        int r,l;
        int color;    
    }a[MAXN*3];
    
    void build(int rt,int l,int r)
    {
        a[rt].l=l;
        a[rt].r=r;
        a[rt].color=-1;//-1表示没有涂色 
        if(l==r)    return ;
        int mid=(l+r)>>1;
        build(rt<<1,l,mid);
        build((rt<<1)|1,mid+1,r);
    }
    
    void update(int rt,int l,int r,int val)
    {
        if(a[rt].l==l&&a[rt].r==r)
        {
            a[rt].color=val;
            return ;
        }
        
        if(a[rt].color>=0)//color>=0表示子树均为一个颜色,lazy思想 
        {
            a[rt<<1].color=a[(rt<<1)|1].color=a[rt].color;    
        }
        
        int mid=(a[rt].l+a[rt].r)>>1;
        
        if(r<=mid)    update(rt<<1,l,r,val);
        else if(mid<l)    update((rt<<1)|1,l,r,val);
        else{
            update(rt<<1,l,mid,val);
            update((rt<<1)|1,mid+1,r,val);
        }
        a[rt].color=-2;//-2表示子树为多种颜色 
    }
    
    
    int Color[MAXN];
    int temp;
    void query(int rt)
    {
        if(a[rt].color==-1)
        {
            temp=-1;
            return ;
        }
        if(a[rt].color!=-2)
        {
            if(temp!=a[rt].color)
            {
                Color[a[rt].color]++;
                temp=a[rt].color;
            }
            return ;
        }
        
        if(a[rt].l==a[rt].r)    return ;
        int mid=(a[rt].l+a[rt].r)>>1;
        query(rt<<1);
        query((rt<<1)|1);
    }
    
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            int maxn=-1;
            temp=-1;
            memset(Color,0,sizeof(Color));
            build(1,1,8000);
            while(n--)
            {
                int l,r,c;
                scanf("%d%d%d",&l,&r,&c);
                update(1,l+1,r,c);
                if(c>maxn)    maxn=c;
            }
            query(1);
            for(int i=0;i<=maxn;i++)
            {
                if(Color[i])    printf("%d %d
    ",i,Color[i]);
            }    
            printf("
    ");
        }
        
    }
  • 相关阅读:
    AOP 学习
    微服务架构理解[架构图](转)
    C# TSC打印二维码和条形码(转)
    C#注册表操作类--完整优化版(转)
    C#调用TSC条码打印机打印二维码(转)
    C#调用TSC条码打印机打印条码(转)
    TSC打印机使用教程终极版(转)
    海尔电商峰值系统架构设计最佳实践(转)
    亿级Web系统搭建——单机到分布式集群(转)
    数据库扩展性设计:使用二进制解决一条记录关联多个状态的问题(转),可以尝试一下
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5124357.html
Copyright © 2011-2022 走看看