Zoj 1610 Count the Colors 详细题解(区间染色,求染色段)

11 篇文章 1 订阅


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


题目大意:

在一条长度为8000的线段上染色,每次把区间[a,b]染成c颜色。显然,后面染上去的颜色会覆盖掉之前的颜色。

求染完之后,每个颜色在线段上有多少个间断的区间。

思路: 一开始把所有颜色都赋值-1,每次更新对颜色赋值,如果 color[rt] != -1,就向下更新。。 pushdown里把左右子树更新后,要把rt变成-1,因为我们最后查区间查的是所有叶子。。通过是不是-1判断是不是叶子, 如果某个根节点也有值就乱套了。。并且这个线段树不能向上更新。。一开始还是用mark标记的。。结果根本分不出叶子跟根。。这个染色的直接用color本身就好了。。最后暴力每一个点,求有几段

注意:

题目每次给的染色段是把[a,b]染成c,之前一直以为就是把a~b的所有点都染成c, 其实不是这样的,要染色的不是点,而是区间,例如要染[0,1],并不是把0,1两点染色,而是把[0,1]这一个单位区间进行染色。 假设有一个样例:

1  2  1

3  4  1

那么这个样例应该输出1  2. 只需要在纸上画一下就可以发现,区间【2,3】是没有被染色的,所以有两个间断的区间颜色是1。

解决这个问题的办法是,建立线段树build(1,1,8000),代表区间1~8000, 然后更新时是update(1,1,8000, a+1,b,c);

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lch rt*2,l,m
#define rch rt*2+1,m+1,r
using namespace std;
const int maxn = 8e3 + 5;
int n, cnt[maxn*4], color[maxn*4], top, res[maxn];
void pushdown(int rt)
{
    color[rt*2+1] = color[rt*2] = color[rt];
    color[rt] = -1;  //不是叶子要变成-1
}
void update(int rt, int l, int r, int i, int j, int val)
{
    if(l >= i && r <= j)
    {
        color[rt] = val;
        return ;
    }
    if(color[rt] != -1) pushdown(rt);  //向下更新
    int m = (l+r)/2;
    if(i <= m) update(lch, i, j, val);
    if(j > m) update(rch, i, j, val);  //这个下面没有pushup了。。不能向上更新
}
void query(int rt, int l, int r)
{
    if(l == r)
    {
        res[top++] = color[rt];  //记录叶子的
        return;
    }
    if(color[rt] != -1) pushdown(rt);
    int m = (l+r)/2;
    query(lch);
    query(rch);
}
int main()
{
    while(~scanf("%d", &n))
    {
        memset(cnt, 0, sizeof(cnt));
        memset(color, -1, sizeof(color));
        int x, y, c;
        top = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d%d", &x, &y, &c);
            if(x > y) continue;
            update(1, 1, maxn, x+1, y, c); //这里是x+1为了防止 重合
        }
        query(1, 1, maxn);  //从头更新一次
//        for(int i = 0; i <= maxn*4; i++)  //这样写不对。。
//        {
            if(color[i] != -1) cout << color[i] << endl;
//            if(color[i] != -1 &&  color[i] != color[i-1])
//                cnt[color[i]]++;
//        }
        int i, j;
            for(i = 0; i < top;)  //从头枚举
            {
                if(res[i] == -1)
                {
                    i++;
                    continue;
                }
                cnt[res[i]]++;
                for(j = i + 1; j < top; j++)
                {
                    if(res[j] != res[i] || res[j] == -1)
                        break;
                }
                i = j;
            }
        for(int i = 0; i < maxn; i++)
        {
            if(cnt[i] != 0)
                printf("%d %d\n", i, cnt[i]);
        }
         printf("\n");
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值