zoukankan      html  css  js  c++  java
  • 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


    Author: Standlove
    Source: ZOJ Monthly, May 2003

     

    题意:给一块长8000米的板上色 问最后能看见几种颜色 而每种颜色的几段

    思路:线段树。这道题有很多细节
    eg: 0 2 1
         3 4 1
    output 应该是 1 2 因为中间隔了一段空的

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int N=8010;
    
    #define L(rt) (rt<<1)
    #define R(rt) (rt<<1|1)
    
    struct node{
        int l,r;
        int flag;   //flag 等于 -1 表示什么色都没有
    }tree[N*3];
    
    int color[N];    //记录每块板上的是什么颜色
    
    void build(int l,int r,int rt){
        tree[rt].l=l;
        tree[rt].r=r;
        tree[rt].flag=-1;
        if(l==r)
            return ;
        int mid=(l+r)>>1;
        build(l,mid,L(rt));
        build(mid+1,r,R(rt));
    }
    
    void PushDown(int rt){
        tree[L(rt)].flag=tree[rt].flag;
        tree[R(rt)].flag=tree[rt].flag;
        tree[rt].flag=-1;
    }
    
    void update(int val,int l,int r,int rt){
        if(tree[rt].flag==val)
            return ;
        if(l<=tree[rt].l && tree[rt].r<=r){
            tree[rt].flag=val;
            return ;
        }
        if(tree[rt].flag!=-1)   //延迟覆盖 不然可能会TLE(最近做一直都是延迟覆盖)
            PushDown(rt);
        int mid=(tree[rt].l+tree[rt].r)>>1;
        if(r<=mid)
            update(val,l,r,L(rt));
        else if(l>=mid+1)
            update(val,l,r,R(rt));
        else{
            update(val,l,r,L(rt));
            update(val,l,r,R(rt));
        }
        if((tree[L(rt)].flag==tree[R(rt)].flag) && (tree[L(rt)].flag!=-1))  //递归回来修改父结点颜色
            tree[rt].flag=tree[L(rt)].flag;
    }
    
    void query(int rt){
        if(tree[rt].flag!=-1){
            for(int i=tree[rt].l;i<=tree[rt].r;i++)
                color[i]=tree[rt].flag;
            return ;
        }
        if(tree[rt].l==tree[rt].r)  //这是不加这个竟然会运行不起来
            return ;
        query(L(rt));
        query(R(rt));
    }
    
    void Solve(){
        int res[N];
        memset(res,0,sizeof(res));
        int pre=-1;
        for(int i=0;i<N;i++)
            if(pre!=color[i]){  //颜色块不相连就++
                pre=color[i];
                res[pre]++;
            }
        for(int i=0;i<N;i++)
            if(res[i]!=0)
                printf("%d %d\n",i,res[i]);
        printf("\n");
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int n;
        while(~scanf("%d",&n)){
            memset(color,-1,sizeof(color));
            build(0,N,1);
            int x,y,val;
            while(n--){
                scanf("%d%d%d",&x,&y,&val);
                if(x==y)
                    continue;
                update(val,x,y-1,1);    //后面一个不涂就能避免那种情况
            }   
            query(1);
            Solve();
        }
        return 0;
    }
  • 相关阅读:
    ABAP Webdynpro Interface View的用法
    ABAP Webdynpro的跟踪工具WD_TRACE_TOOL
    git 速查
    Python 解析含有命名空间(xmlns)的xml文件(基于ElementTree)
    完全显示DataFrame中行、列内容
    解决Jupyter Notebook中for循环输出DataFrame不够美观
    git配置别名
    元素可拖拽(移动端与pc端)
    pointer network和recursive神经网络
    ELMO,BERT和GPT简介
  • 原文地址:https://www.cnblogs.com/jackge/p/3040034.html
Copyright © 2011-2022 走看看