Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are doing multiple queries on the same array then you could use a Segment Tree. They are generally used to do range minimum/maximum and range sum queries but you can change it to do range median.</p> <p>A segment tree for a set with n intervals uses O(n log n) storage and can be built in O(n log n) time. A range query can be done in O(log n).</p> <p>Example of median in range segment tree:</p> <p>You build the segment tree from the bottom up (update from the top down):</p> <pre><code> [5] [3] [7] [1,2] [4] [6] [8] 1 2 3 4 5 6 7 8 </code></pre> <p>Indices covered by node:</p> <pre><code> [4] [2] [6] [0,1] [3] [5] [7] 0 1 2 3 4 5 6 7 </code></pre> <p>A query for median for range indices of 4-6 would go down this path of values:</p> <pre><code> [4] [5] 0 1 2 3 4 5 6 7 </code></pre> <p>Doing a search for the median, you know the number of total elements in the query (3) and the median in that range would be the 2nd element (index 5). So you are essentially doing a search for the first node which contains that index which is node with values [1,2] (indices 0,1).</p> <p>Doing a search of the median of the range 3-6 is a bit more complicated because you have to search for two indices (4,5) which happen to lie in the same node.</p> <pre><code> [4] [6] [5] 0 1 2 3 4 5 6 7 </code></pre> <p><a href="http://en.wikipedia.org/wiki/Segment_tree" rel="nofollow">Segment tree</a></p> <p><a href="http://community.topcoder.com/tc?module=Static&amp;d1=tutorials&amp;d2=lowestCommonAncestor" rel="nofollow">Range minimum query on Segment Tree</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload