ZOJ1610 Count the Colors----线段树

本文介绍了一种通过分段更新和查询实现的颜色段计数算法。该算法用于解决多个颜色段覆盖问题,确保最终能够正确统计每个可见颜色段的数量。通过对输入数据进行逐段处理并利用递归思想进行区间更新和查询,有效地解决了复杂的数据覆盖问题。

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

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

题意:求多次绘制后可见的不同颜色的不连续的个数。这道题其实不难,但一定要注意题目要求,题目中说的是对片段涂色,而不是对区间内的几个点涂色,例如[1,3]涂颜色1,[4,7]涂颜色1。正确答案是1 2。如果是对区间内的点涂色,则会显示两个区间之间没有间隔,答案会是1 1,如下图所示:

如果是对区间线段涂色,则会显示两个区间之间有间隔,答案会是1 2,如下图所示:

所以下面在调用updata函数时  updata(a+1,b,c,1,8000,1)  其中要a+1。

这道题其实和另一道张贴海报的题目有些类似,但是这道题数据不大,不需要离散化。

这是那道题:https://blog.youkuaiyun.com/sinat_41233888/article/details/81508132

#include <iostream>
#include <stdio.h>
#include <string.h>
#define maxn 8000+10
using namespace std;
int vis[maxn<<2],num[maxn<<2],last;
void pushdown(int rt)
{
    if(vis[rt]!=-1)
    {
        vis[rt<<1]=vis[rt<<1|1]=vis[rt];
        vis[rt]=-1;
    }
}
void updata(int L,int R,int C,int l,int r,int rt)
{
    if(L<=l&&R>=r){
        vis[rt]=C;
        return ;
    }
    pushdown(rt);
    int m=(l+r)>>1;
    if(L<=m)updata(L,R,C,l,m,rt<<1);
    if(R>m)updata(L,R,C,m+1,r,rt<<1|1);
}
void query(int l,int r,int rt)
{
    if(l==r){
        if(vis[rt]!=-1&&vis[rt]!=last)
            num[vis[rt]]++;
        last=vis[rt];
        return;
    }
    pushdown(rt);
        int m=(l+r)>>1;
        query(l,m,rt<<1);
        query(m+1,r,rt<<1|1);
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int a,b,c;
        memset(vis,-1,sizeof(vis));
        memset(num,0,sizeof(num));
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            if(a<b)updata(a+1,b,c,1,8000,1);
        }
        int last=-1;
        query(1,8000,1);
        for(int i=0;i<=8000;i++)
            if(num[i])printf("%d %d\n",i,num[i]);
        printf("\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值