Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 多条线段上的相邻点列表?_Algorithm - Fatal编程技术网

Algorithm 多条线段上的相邻点列表?

Algorithm 多条线段上的相邻点列表?,algorithm,Algorithm,我有一条数字线,从1到100 在该数字线的范围内,我可以添加和删除许多线段。这些线段可以相互交叉和重叠 对于给定的x1和x2,我需要一个有效的算法来迭代所有相邻的点对(包括x1和x2),以便访问相邻点之间运行的所有线段的列表 黑色数字线和彩色线段的结果如下: [0-20] -> [] [20-30] -> [red] [30-40] -> [red, green] [40-50] -> [green] [50-60] -> [] [60-80] -> [pu

我有一条数字线,从1到100

在该数字线的范围内,我可以添加和删除许多线段。这些线段可以相互交叉和重叠

对于给定的x1和x2,我需要一个有效的算法来迭代所有相邻的点对(包括x1和x2),以便访问相邻点之间运行的所有线段的列表

黑色数字线和彩色线段的结果如下:

[0-20] -> []
[20-30] -> [red]
[30-40] -> [red, green]
[40-50] -> [green]
[50-60] -> []
[60-80] -> [purple]
[80-100] -> []

您想使用.p>创建一个边界记录列表,其中每个边界的格式如下

bound.type = start,finish
bound.position = 0..n
bound.color = red,green,blue...
对于每条线段,在列表中添加两条此类记录(每端为ine)。然后按位置对所有记录进行排序。 现在,如果您按如下方式遍历列表:

colors=[]
write '[0'
for each bound in list
  write '-',bound.pos,'] -> [',colours,']'
  if bound.type = start then 
    add bound.color to colors
  else
    remove bound.type from colors
  write '[',bound.pos
write '-',n'] -> []'

如果第一条线段从0开始,或者最后一条线段在n结束,您将需要进行一些整理。

您计划的线段数量是否有限制?是否有时间复杂度要求/限制?@dasblinkenlight不,没有限制,但实际上少于10000@DougRamsey只是需要快点。缓存数据是可以的。听起来你需要一个间隔树