Count the Colors (线段树,区间更新)

119 篇文章 1 订阅
113 篇文章 1 订阅

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

思路:先将区间更新,用标记记录区域的结尾,判断颜色相同的区间的头和尾是否相连,来判断是否是一个区间。
注意:区间不是一个点,例如

1  2  1
3  4  1

答案  1  2

另外题目给的样例,还有开头和结尾是一样的,例如: 1 1 2。
这种样例是不算数的,要排除。

#include<stdio.h>
#include<string.h>
#define N 8009

struct node
{
    int xia;//记录颜色
} tree[N*4+10];
struct node1
{
    int inq;//颜色的个数
    int e;//颜色区间的尾部
} dis[N];
struct node2
{
    int l,r,z;
} a[N];
void build(int num,int l,int r)//建树
{
    tree[num].xia=-1;//颜色可以是0,所以要初始为负值
    if(l==r) return ;
    int mid=(l+r)>>1;
    build(num<<1,l,mid);
    build(num<<1|1,mid+1,r);
}
void cont(int num,int l,int r)
{
    if(tree[num].xia!=-1)
    {
        int mid=(l+r)/2;
        int add=tree[num].xia;
        tree[num<<1].xia=add;
        tree[num<<1|1].xia=add;
        tree[num].xia=-1;
    }
}
void update(int x,int y,int z,int num,int l,int r)//区间更新
{
    int mid=(l+r)/2;
    if(x<=l&&r<=y)
    {
        tree[num].xia=z;//记录该区间所记录的颜色
        return ;
    }
    cont(num,l,r);
    if(mid>=x) update(x,y,z,num<<1,l,mid);
    if(mid<y)  update(x,y,z,num<<1|1,mid+1,r);
}
void sou(int num,int l,int r)//遍历所有的颜色,记录他们的个数
{
    if(tree[num].xia!=-1)
    {
        int h=tree[num].xia;
        if(dis[h].inq==0)
            dis[h].inq++;
        else if(dis[h].e+1!=l)//头和尾不同的相同颜色的区域
            dis[h].inq++;
        dis[h].e=r;//记录该颜色的尾部
        return ;
    }
    if(l==r) return ;
    int mid=(l+r)/2;
    sou(num<<1,l,mid);
    sou(num<<1|1,mid+1,r);
}
int main()
{
    int n,m,t;
    while(~scanf("%d",&n))
    {
        int s=8009,e=-1;//记录树的范围,以便建树
        int s_=8009,e_=-1;//记录颜色的范围
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].z);
            if(a[i].l==a[i].r)//排除头尾相同的样例
            {
                i--;
                n--;
                continue;
            }
            if(s>a[i].l+1) s=a[i].l+1;
            if(e<a[i].r) e=a[i].r;
            if(s_>a[i].z) s_=a[i].z;
            if(e_<a[i].z) e_=a[i].z;
        }
        build(1,s,e);
        for(int i=1; i<=n; i++)
            update(a[i].l+1,a[i].r,a[i].z,1,s,e);
        for(int i=s_; i<=e_; i++)//初始化,每种颜色的个数
            dis[i].inq=0;
        sou(1,s,e);
        for(int i=s_; i<=e_; i++)
        {
            if(dis[i].inq)
                printf("%d %d\n",i,dis[i].inq);
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值