zoukankan      html  css  js  c++  java
  • Count the Colors-ZOJ1610(线段树区间求)

    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

    题目大意:

    在一条线上花颜色,求出最后看到的颜色种类

    分析:

    因为从0开始,所以应该用区间

    #include<stdio.h>
    #include<math.h>
    #include<algorithm>
    #include<iostream>
    #include<string.h>
    #include<stdlib.h>
    using namespace std;
    #define N 8005
    #define Lson r<<1
    #define Rson r<<1|1
    int sum[N],temp;
    
    struct node
    {
        int L,R,e;
        int mid()
        {
            return (L+R)/2;
        }
        int len()
        {
            return R-L+1;
        }
    }a[N*4];
    
    void BuildTree(int r,int L,int R)
    {
        a[r].L=L;
        a[r].R=R;
        a[r].e=-1;
        if(L==R-1)
        {
            return;
        }
        BuildTree(Lson,L,a[r].mid());
        BuildTree(Rson,a[r].mid(),R);
    }
    void Qurry(int r)
    {
        if(a[r].e!=-1)
        {
            if(a[r].e!=temp)
            {//temp表示这个区间的颜色没有被加过
                sum[a[r].e]++;
            }
            temp=a[r].e;
            return;
        }
        if(a[r].L==a[r].R-1)
        {
            temp=-1;
            return;
        }
            Qurry(Lson);
            Qurry(Rson);
    }
    void Update(int r,int L,int R,int e)
    {
    
        if(a[r].L==L && a[r].R==R)
        {
            a[r].e=e;
            return;
        }
        if(a[r].e!=-1)
        {
            a[Lson].e=a[Rson].e=a[r].e;
            a[r].e=-1;
        }
        if(R<=a[r].mid())
            Update(Lson,L,R,e);
        else if(L>=a[r].mid())
            Update(Rson,L,R,e);
        else
        {
            Update(Lson,L,a[r].mid(),e);
            Update(Rson,a[r].mid(),R,e);
        }
    }
    int main()
    {
        int n,x,y,e;
        while(scanf("%d",&n)!=EOF)
        {
            memset(sum,0,sizeof(sum));
            BuildTree(1,0,N);
            int Max=0;
            for(int i=0;i<n;i++)
            {
                scanf("%d %d %d",&x,&y,&e);
                Max=max(Max,e);
                Update(1,x,y,e);
    
            }
            temp=-1;
            Qurry(1);
            for(int i=0;i<=Max;i++)
            {
                if(sum[i])
                {
                    printf("%d %d
    ",i,sum[i]);
                }
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    HTML DOM-->内部样式表与外部样式表的读写
    HTML DOM-->行间样式表的读写
    HTML DOM-->获取文本节点
    HTML DOM-->获取属性节点
    HTML DOM-->获取标签(元素)节点
    HTML DOM-->简介
    JS 浏览器BOM-->setTimeout() 方法
    JS 浏览器BOM-->clearInterval() 方法
    JS 浏览器BOM-->setInterval() 方法
    JS 浏览器BOM-->onresize方法
  • 原文地址:https://www.cnblogs.com/linliu/p/4957224.html
Copyright © 2011-2022 走看看