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

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAX 8005
struct Tree{int l,r;}tree[MAX << 2];
int cover[MAX << 2];//用于标记每一段区间被涂上的颜色
int num[MAX << 2];//每种颜色所占区间
int ans;
void build(int l,int r,int rt)
{
    tree[rt].l = l;
    tree[rt].r = r;
    if(l == r) return;
    int mid = (l + r) >> 1;
    build(l,mid,rt << 1);
    build(mid + 1,r,rt << 1 | 1);
}
void pushdown(int rt)//下推操作
{
    if(cover[rt] != -1){//cover用来延迟标记,这里不用再开lazy
        cover[rt << 1] = cover[rt << 1 | 1] = cover[rt];//大的区间涂上颜色c,则它的子区间也涂上颜色c
        cover[rt] = -1;
    }
}
void update(int rt,int L,int R,int C)         
{
    if(L <= tree[rt].l && R >= tree[rt].r){//这段区间涂上颜色C
        cover[rt] = C;
        return;
    }
    pushdown(rt);//下推操作
    int mid = (tree[rt].l + tree[rt].r) >> 1;
    if(L <= mid) update(rt << 1,L,R,C);//往左子树搜
    if(R > mid) update(rt << 1 | 1,L,R,C);//往右子树搜
}
void query(int l,int r,int rt)
{
    if(l == r){
        if(cover[rt] != -1 && cover[rt] != ans) num[cover[rt]]++;//每种不同的颜色计数
        ans = cover[rt];//防止相邻
        return;
    }
    pushdown(rt);//下推操作
    int mid = (l + r) >> 1;
    query(l,mid,rt << 1);
    query(mid + 1,r,rt << 1 | 1);
}
int main()
{
    int n,c;
    while(~scanf("%d",&n)){
        memset(cover,-1,sizeof(cover));
        build(1,8000,1);
        for(int i = 0;i < n;i++){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            update(1,x + 1,y,z);//区间涂色
        }
        memset(num,0,sizeof(num));//刚开始每种颜色为0段区间
        ans = -1;
        query(1,8000,1);
        for(int i = 0;i <= 8000;i++){
            if(num[i]) cout << i << " " << num[i] << endl;
        }
        cout << endl;
    }
    return 0;
}

题意:
在一段长8000的线段上染色,每次对[a,b]区间染颜色c,后面染的颜色会覆盖前面的颜色。问你每个颜色在线段上有多少个间断区间。
思路:
这是一道线段树区间覆盖问题,而且给的线段不需要进行离散化,注意一点:给你的n是操作步骤而不是线段的总长度,线段的总长度当作8000即可。所以一开始先对区间[1,8000]建立线段树。每次输入一组数据就进行修改操作,最后查询。还有一点,这里要求得是间隔区间得颜色数量,所以为了防止连续的颜色,你每次查询完都需要标记一下。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值