Segment tree: number of numbers less than x
1 answer
It's pretty simple:
-
Store a sorted array of all numbers in the range covered by a specific node memory
(O(n * log n)
and initialization time). -
To answer the query, decompose the query segment into nodes
O(log n)
(just as you do for the standard min / max / sum tree) and do a binary search through the array stored in each of these nodes to find the number of elements lessx
. He gives timeO(log^2 n)
for each request. You can also achieveO(log n)
with fractional cascading, but this is optional.
+4
source to share