[英]number of elements strictly lesser than a given number
I want a data structure in which I want to insert elements in log(n) time and the elements should be sorted in the ds after every insertion. 我想要一个数据结构,其中我想在log(n)时间插入元素,并且元素应该在每次插入后在ds中排序。 I can use a multiset for this. 我可以使用multiset。
After that I want to find the numbers of elements strictly smaller than a given number again in log(n) time. 之后我想在log(n)时间内再次找到严格小于给定数字的元素数。 And yes duplicates are also present and they need to be considered. 是的重复也存在,他们需要考虑。 For example if the query element is 5 and the ds contains {2, 2, 4, 5, 6, 8, 8} then answer would be 3(2, 2, 4) as these 3 elements are stricly lesser than 5 例如,如果查询元素是5并且ds包含{2,2,4,5,6,8,8},那么答案将是3(2,2,4),因为这3个元素严格小于5
I could have used multiset but even if I use upper_bound I will have to use distance method which runs in linear time. 我可以使用multiset,但即使我使用upper_bound,我也必须使用以线性时间运行的distance方法。 How can I achieve this efficiently with c++ stl. 如何使用c ++ stl有效地实现这一目标。 Also I cannot use 我也不能用
The data structure you need is an order statistic tree: https://en.wikipedia.org/wiki/Order_statistic_tree 您需要的数据结构是订单统计树: https : //en.wikipedia.org/wiki/Order_statistic_tree
The STL doesn't have one, and they're not very common so you might have to roll your own. STL没有,它们不是很常见,所以你可能不得不自己动手。 You can find code in Google, but I can't vouch for any specific implementation. 您可以在Google中找到代码,但我无法保证任何具体的实施。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.