ZOJ1610 Count the Colors(计数能看到的不同颜色段及段数)

本文介绍了一种使用线段树解决区间染色问题的方法。通过实例详细解析了如何利用线段树进行区间更新,并统计最终可见的颜色段数量。为解决此类问题提供了一个高效的实现方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610

题目

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

题意

给定m个区间和要染成的颜色,问染完后能看到的每个颜色的段数。

分析

线段树区间修改,最后暴力统计每个叶子结点的颜色。因为染色的特殊性,需要将区间与线段树的对应进行特殊处理。比如[1,4]染1,[1,2]染2,[3,4]染3.正确答案是有1,2,3三种颜色段。但是如果将区间与线段树维护的区间直接对应的话,答案是2,3两种颜色段。即区间[2,3]的颜色因为叶子结点2、3被染成其他颜色而计数不出来。
故将区间[l,r]对应到线段树中的区间[2l,2r]。

AC代码

//60ms 13.6MB
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
#define lson p<<1
#define rson p<<1|1
using namespace std;

const int maxn=2e5+100;
struct node
{
    int l,r;//结点所维护的区间
    int mark;//延迟标记
} T[maxn<<2]; //线段树要开四倍空间
int x[maxn],y[maxn],a[maxn];
int vis[maxn];//叶子染色的颜色

void down(int p)
{
    if(T[p].mark==-1)
        return ;
    T[lson].mark=T[rson].mark=T[p].mark;
    T[p].mark=-1;
}
void build(int p,int l,int r)//p表示结点编号,l、r表示结点p所表示的区间
{
    T[p].mark=-1;
    T[p].l=l,T[p].r=r;//确定结点p所表示的区间[l,r]
    if(l==r) //确定叶子结点所表示的信息
    {
        return ;
    }
    int mid=(l+r)>>1;
    build(lson,l,mid); //递归创建左子树
    build(rson,mid+1,r); //递归创建右子树
}
void update(int p,int x,int y,int v)
{
    if(T[p].l==x && T[p].r==y) //区间恰好重合
    {
        T[p].mark=v; //标记信息修改到此结点打止
        return ;
    }
    //区间在p的子树结点中,要调用子树结点信息,必须先下传延迟标记
    down(p); //下传标记将p的左右子结点修改,只修改了这两个结点,延迟标记放在了这两个结点上
    int mid=(T[p].l+T[p].r)>>1;
    if(y<=mid) //区间[x,y]一定在左子树上
        update(lson,x,y,v);
    else if(x>mid) //区间[x,y]一定在右子树上
        update(rson,x,y,v);
    else
    {
        //必须将区间分成两份小区间[x,mid]和[mid+1,y]
        update(lson,x,mid,v);
        update(rson,mid+1,y,v);
    }
}
void query(int p)
{
    if(T[p].l==T[p].r)
    {
        vis[T[p].l]=T[p].mark;
        return ;
    }
    down(p);
    query(lson);
    query(rson);
}
int ans[maxn];
int main()
{
    int m,n,tot;
    while(~scanf("%d",&m))
    {
        n=16000;
        tot=0;
        memset(vis,-1,sizeof(vis));
        build(1,0,n);
        for(int i=1; i<=m; i++)
        {
            int l,r,c;
            scanf("%d%d%d",&l,&r,&c);
            l*=2;
            r*=2;
            update(1,l,r,c);
        }
        query(1);//将延迟标记传递到每个叶子结点
        memset(ans,0,sizeof(ans));
        int pre=-1;//上一个颜色段的颜色
        for(int i=0; i<=n; i++)
        {
            if(vis[i]==pre) continue;
            pre=vis[i];
            if(vis[i]==-1) continue;
            ans[pre]++;
        }
        for(int i=0;i<=n;i++)
            if(ans[i])
            printf("%d %d\n",i,ans[i]);
        putchar(10);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值