/// <summary> /// return the number of intervals containing 'x' in /// Complexity: o(log(n)) (+ o(n log(n) ) preparation time) /// (where 'n' is the total number of interval) /// </summary> /// <param name="x"></param> /// <returns>number of intervals containing x</returns> public int CountIntervalsContaining(int x) { if (x < _center || (x == _center && !_endOfIntervalIsIncludedInInterval)) { return((_left?.CountIntervalsContaining(x) ?? 0) + MaximumValidIndex(0, _intervalsSortedByStart.Count, idx => (idx == 0) || _intervalsSortedByStart[idx - 1].Item1 <= x)); } else { return((_right?.CountIntervalsContaining(x) ?? 0) + MaximumValidIndex(0, _intervalsSortedByEnd.Count, idx => (idx == 0) || _intervalsSortedByEnd[_intervalsSortedByEnd.Count - idx].Item2 > x || (_endOfIntervalIsIncludedInInterval && _intervalsSortedByEnd[_intervalsSortedByEnd.Count - idx].Item2 == x) )); } }