如何找到点是否在一个设定的时间间隔的? [英] How to find if a point is within a set of intervals?
问题描述
我要寻找确定一个线上的点是否是此行的一个子集内的最快方式。 我给一个整数点,我也有一个清单之一:
I am looking for the fastest way to decide whether or not a point on a line is within a subset of this line. I am given an integer Point, and I also have a "list" of either:
- 点,用一个整数(3,10,1000,等等)psented重新$ P $
- 间隔,使我重新present 2的整数(2:10是从2到10 inluded,50:60,等所有的整数)
在这个例子中,如果我的点值是5的话,我回到正确的,因为它被包含在一个区间,同样为55。如果我的观点是等于1000,我也返回true,因为它相匹配的列表点。
In this example, if the value of my point is 5, then I return true because it is included in an interval, same for 55. If my point is equal to 1000, I also return true because it matches the list of points.
我要寻找一个快速的方式(而不是线性快),以检查此条件,而不必因为有可能的点(即实例化尽可能多的整数,为1:1000的间隔,我不希望实例化1000整数)。 可以在对数时间这样做?
I am looking for a fast way (quicker than linear) to check for this condition, WITHOUT having to instanciate as many integer as there are possible points (ie, for a 1:1000 interval I don't want to instanciate 1000 integers). Can this be done in a logarithmic time?
感谢
编辑: 你可以考虑采取以pre-过程数据的列表中的任何时间等于0,因为一旦我最初的时间间隔被处理,我需要这个测试适用于10,000点
edit : you can consider that any time taken to pre-process the list of data is equal to 0, because once my initial intervals are processed I need to apply this test to 10k points
推荐答案
嗯,也许你可以使用间隔或段树:
Hm, maybe you can use an interval or a segment tree:
- http://en.wikipedia.org/wiki/Segment_tree
- <一个href="http://en.wikipedia.org/wiki/Interval_tree">http://en.wikipedia.org/wiki/Interval_tree
- http://en.wikipedia.org/wiki/Segment_tree
- http://en.wikipedia.org/wiki/Interval_tree
这篇关于如何找到点是否在一个设定的时间间隔的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!