• ZOJ 1610 Count the Colors (线段树区间更新与统计)


    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

    ZOJ挂了,我还没交没不知道对不对。应该没问题,按照kuangbin大神的模板改的。这个题又是区间更新,不过看数据范围不用离散化。

    因为每个节点都代表区间的端点,kuangbin大神的处理方法在建树的时候与普通线段树模板做了微小的改动。

    代码如下:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 #define M 8010
     5 struct segTree
     6 {
     7     int l,r,col;
     8 }tree[M<<2];
     9 int color[M],temp,n;
    10 void buildTree (int now,int l,int r)
    11 {
    12     tree[now].l=l,tree[now].r=r;
    13     tree[now].col=-1;//-1代表没有颜色
    14     if (l+1==r)
    15     return ;
    16     int m=((l+r)>>1);
    17     buildTree(now<<1,l,m);
    18     buildTree(now<<1|1,m,r);//因为节点代表端点,二分建树时是[l,m],[m,r],而不是[m+1,r]。
    19 }
    20 void upDate (int now,int l,int r,int c)
    21 {
    22     if (l==r||l>r)
    23     return ;
    24     if (tree[now].col==c)
    25     return ;
    26     if (l<=tree[now].l&&tree[now].r<=r)
    27     {
    28         tree[now].col=c;
    29         return ;
    30     }
    31     if (tree[now].col>=0)
    32     {
    33         tree[now<<1].col=tree[now].col;
    34         tree[now<<1|1].col=tree[now].col;
    35         tree[now].col=-2;
    36     }
    37     int mid=(tree[now].l+tree[now].r)>>1;
    38     if (r<=mid)
    39     upDate(now<<1,l,r,c);
    40     else if (l>=mid)
    41     upDate(now<<1|1,l,r,c);
    42     else
    43     {
    44         upDate(now<<1,l,mid,c);
    45         upDate(now<<1|1,mid,r,c);
    46     }
    47     tree[now].col=-2;//-2代表有多种颜色
    48 }
    49 void Count (int now)
    50 {
    51     if (tree[now].col==-1)
    52     {
    53         temp=-1;//temp是前一段的颜色
    54         return ;
    55     }
    56     if (tree[now].col!=-2)
    57     {
    58         if (tree[now].col!=temp)
    59         {
    60             color[tree[now].col]++;
    61             temp=tree[now].col;
    62         }
    63         return ;
    64     }
    65     if (tree[now].l+1!=tree[now].r)
    66     {
    67         Count(now<<1);
    68         Count(now<<1|1);
    69     }
    70 }
    71 int main()
    72 {
    73     //freopen("de.txt","r",stdin);
    74     while (~scanf("%d",&n))
    75     {
    76         int x,y,z,maxn=0;
    77         buildTree(1,0,8000);
    78         for (int i=0;i<n;++i)
    79         {
    80             scanf("%d%d%d",&x,&y,&z);
    81             upDate(1,x,y,z);
    82             maxn=max(maxn,z);
    83         }
    84         temp=-1;
    85         memset(color,0,sizeof color);
    86         Count(1);
    87         for (int i=0;i<=maxn;++i)
    88         {
    89             if (color[i])
    90             printf("%d %d
    ",i,color[i]);
    91         }
    92         printf("
    ");
    93     }
    94     return 0;
    95 }
  • 相关阅读:
    OCP-1Z0-053-V12.02-69题
    OCP-1Z0-053-V12.02-170题
    OCP-1Z0-053-V12.02-117题
    OCP-1Z0-053-V12.02-222题
    OCP-1Z0-053-V12.02-528题
    OCP-1Z0-053-V12.02-531题
    OCP-1Z0-053-V12.02-532题
    OCP-1Z0-053-V12.02-533题
    OCP-1Z0-053-V12.02-542题
    OCP-1Z0-053-V12.02-68题
  • 原文地址:https://www.cnblogs.com/agenthtb/p/5886432.html
Copyright © 2020-2023  润新知