Order statistic on intervals

前端 未结 2 1625
予麋鹿
予麋鹿 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: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...

提交回复
热议问题