是否可以有效地计算与数轴上单个点重叠的线段P
数?
所有线段都位于一条数字线上(它是一个1-D
世界,而不是一个3-D
世界)。
每条线段都有一个起始坐标X1
和一个结束坐标X2
。
例子:
Line segment A spans from X1==1 to X2==3
Line segment B spans from X1==2 to X2==4
Line segment C spans from X1==3 to X2==5
Line segment D spans from X1==1 to X2==4
----------------------------------------
Ex1: Line segments that overlap point P==2: A,B and D >>> overlap count==3.
Ex2: Line segments that overlap point P==7: None >>> overlap count==0.
Ex3: Line segments that overlap point P==3: A,B,C and D >>> overlap count==4.
当然,如果只有4条线段,那么代码就简单了。但是,如果有一个包含 4 亿条线段的庞大空间数据库,那么搜索就会很慢。
是否有任何算法可以有效地搜索此线段列表中的重叠总数?
到目前为止我在看什么