Order statistic on intervals

前端 未结 2 1602
予麋鹿
予麋鹿 2021-01-12 16:39

Given an array of numbers a[0], a[1], ..., a[n-1], we get queries of the kind:

output k-th largest number in the range a[i], a[i+1],

相关标签:
2条回答
  • 2021-01-12 17:15

    There is an algoritm named QuickSelect which based on quick sort. It works O(n) in average case. Algorithm's worst case is O(n**2) when input revese ordered.

    It gives exact k-th biggest number. If you want range, you can write an wrapper method.

    0 讨论(0)
  • 2021-01-12 17:22

    Yes, these queries can be answered in polylog time if O(n log n) space is available.

    Preprocess given array by constructing segment tree with depth log(n). So that leaf nodes are identical to source array, next-depth nodes contain sorted 2-element sub-arrays, next level consists of 4-element arrays produced by merging those 2-element arrays, etc. In other words, perform merge sort but keep results of each merge step in separate array. Here is an example:

    root:   | 1   2   3   5   5   7   8   9 |
            | 1   2   5   8 | 3   5   7   9 |
            | 1   5 | 2   8 | 7   9 | 3   5 |
    source: | 5 | 1 | 2 | 8 | 7 | 9 | 5 | 3 |
    

    To answer a query, split given range (into at most 2*log(n) subranges). For example, range [0, 4] should be split into [0, 3] and [4], which gives two sorted arrays [1 2 5 8] and [7]. Now the problem is simplified to finding k-th element in several sorted arrays. The easiest way to solve it is nested binary search: first use binary search to choose some candidate element from every array starting from largest one; then use binary search in other (smaller) arrays to determine rank of this candidate element. This allows to get k-th element in O(log(n)^4) time. Probably some optimization (like fractional cascading) or some other algorithm could do this faster...

    0 讨论(0)
提交回复
热议问题