Count the Colors+ZOJ+线段树成段更新

本文介绍了一种基于线段树的数据结构算法,用于处理区间更新和查询的问题。通过使用懒惰传播(lazy propagation)技巧,有效地解决了彩色线段覆盖的问题,并提供了两种实现方式的代码示例。

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

Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu

Description

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*st,2*en-1,最后运用lazy标记思想即可。
code:
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define lson rt<<1,L,M
#define rson rt<<1|1,M+1,R
using namespace std;
const int maxn=8010;
int C[maxn*8+10];///区间标记,lazy标记
int Set[2*maxn];///记录每个点的颜色
int seg[2*maxn];///记录每个颜色的段数
void push_down(int rt)
{
    if(C[rt]!=-1)
    {
        C[rt<<1]=C[rt<<1|1]=C[rt];
        C[rt]=-1;
    }
}
void push_up(int rt)
{

    C[rt]=(C[rt<<1]==C[rt<<1|1]?C[rt<<1]:-1);

}
void update(int rt,int L,int R,int sl,int sr,int v)
{

    if(sl<=L&&sr>=R)
    {
        C[rt]=v;
        return ;
    }
    else
    {
        int M=(L+R)>>1;
        push_down(rt);
        if(sl<=M) update(lson,sl,sr,v);
        if(sr>M) update(rson,sl,sr,v);
        push_up(rt);
    }

}
void query(int rt,int L,int R)
{
    if(L==R)
    {
        Set[L]=C[rt];
    }
    else
    {
        int M=(L+R)>>1;
        push_down(rt);
        query(lson);
        query(rson);
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(C,-1,sizeof(C));
        memset(seg,0,sizeof(seg));
        memset(Set,-1,sizeof(Set));
        int s=0;
        for(int i=0; i<n; i++)
        {
            int st,en,c;
            scanf("%d%d%d",&st,&en,&c);
            st=st*2,en=en*2-1;
            update(1,0,2*maxn,st,en,c);
        }
        query(1,0,2*maxn);
        for(int i=0; i<2*maxn; i++)
        {

            if(Set[i]!=Set[i+1]&&Set[i]>=0)
            {
                seg[Set[i]]++;
            }

        }
        for(int i=0; i<maxn; i++)
        {
            if(seg[i])
                printf("%d %d\n",i,seg[i]);
        }
        printf("\n");
    }
    return 0;
}

解决方案2:对于更新查询的都是段的情况,我们可以这样子来写线段树:lson: rt<<1,L,M;rson: rt<<1|1,M,R;然后以长度为1的一小段作为最小元区间,最后查询时的截止条件为L+1==R;成段更新运用lazy标记思想。
code:
#include <iostream>
#include<cstdio>
#include<cstring>
#define lson rt<<1,L,M
#define rson rt<<1|1,M,R
using namespace std;
const int maxn=8010;
int C[maxn<<2];
int seg[maxn+10];
int Set[maxn+10];
int k;
void push_down(int rt)
{

    if(C[rt]>=0)
    {
        C[rt<<1]=C[rt<<1|1]=C[rt];
        C[rt]=-1;
    }

}
void update(int rt,int L,int R,int sl,int sr,int v)
{
    if(sl<=L&&sr>=R)
    {
        C[rt]=v;
    }
    else
    {
        int M=(L+R)>>1;
        push_down(rt);
        if(sl<M)
            update(lson,sl,sr,v);
        if(sr>M)
            update(rson,sl,sr,v);
    }

}
void query(int rt,int L,int R)
{
    if(L+1==R)
    {
        Set[k++]=C[rt];
    }
    else
    {
        int M=(L+R)>>1;
        push_down(rt);
        query(lson);
        query(rson);
    }


}
int main()
{
    int n;
    while(~scanf("%d",&n)){
        memset(C,-1,sizeof(C));
        memset(seg,0,sizeof(seg));
        memset(Set,-1,sizeof(Set));
        for(int i=0;i<n;i++){
            int st,en,v;
            scanf("%d%d%d",&st,&en,&v);
            st++,en++;
            update(1,1,maxn,st,en,v);
        }
        k=0;
        query(1,1,maxn);
        for(int i=0;i<k;i++){
            if(Set[i]!=Set[i+1]&&Set[i]>=0){
                seg[Set[i]]++;
            }
        }
        for(int i=0;i<maxn;i++){
            if(seg[i]){
                printf("%d %d\n",i,seg[i]);
            }
        }
        printf("\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值