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

思路:

因为此题中x1,x2表示的是以x1为起点,x2为终点的区间,x1,x2表示的是点,而不是线段,所以我们不能直接构造线段树,我们可以将x1+1,或者x2-1,这样就可以使点变成线段了,因为范围减小了1,我当时做的时候是x2-1,结果越界了。然后就用的x1+1。

利用区间更新来覆盖颜色。

然后通过区间查询寻找答案,这里的区间查询比较特殊,是将所有线段的颜色都记录下来,然后看看有多少连续的线段。

如果只是看到一段区间有颜色就记录下来的话,会使几段可以连成一段连续线段分别记录,就使 结果变大。

代码如下:

 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=8*1e3+5;
int n;
int tree[maxn<<2];
int ans[maxn<<2];
int col[maxn<<2];
void push_down(int rt)
{
    if(tree[rt]!=-1)
    {
        tree[rt<<1]=tree[rt<<1|1]=tree[rt];
        tree[rt]=-1;
    }
}
void update (int L,int R,int x,int l,int r,int rt)
{
    if(l>=L&&r<=R)
    {
        tree[rt]=x;
        return;
    }
    int m=(l+r)>>1;
    push_down(rt);
    if(L<=m) update (L,R,x,l,m,rt<<1);
    if(R>m) update (L,R,x,m+1,r,rt<<1|1);
}
void query (int l,int r,int rt)
{
    if(tree[rt]!=-1)
    {
        for (int i=l;i<=r;i++)
        {
            col[i]=tree[rt];
        }
        return;
    }
    if(l==r) return;
    int m=(l+r)>>1;
    push_down(rt);
    query(l,m,rt<<1);
    query(m+1,r,rt<<1|1);
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset (col,-1,sizeof(col));
        memset (ans,0,sizeof(ans));
        memset (tree,-1,sizeof(tree));
        while(n--)
        {
            int x,y,c;
            scanf("%d%d%d",&x,&y,&c);
            if(x<y) update (x+1,y,c,1,8000,1);
        }
        query(0,8000,1);
        for (int i=0;i<=8000;i++)
        {
            if(col[i]==-1) continue;
            while(col[i]!=-1&&col[i+1]==col[i]&&i<=8000) i++;
            ans[col[i]]++;
        }
        for (int i=0;i<=8000;i++)
        {
            if(ans[i]) printf("%d %d\n",i,ans[i]);
        }
        printf("\n");
    }
    return 0;
}

 

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值