uttom's blog

By uttom, history, 10 years ago, In English

For preprocessing in segment tree and sparse table Both takes O(nlogn) times. And in every query both takes O(log(n)) times. But How sparse Table faster than Segment tree? Thanks in Advance.

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
10 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Segment tree's preprocessing takes O(N) and sparse table's query takes O(1).

  • »
    »
    10 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    I have read about sparse table LInk I have found that sparse tables query takes O(log(n)) times.

    • »
      »
      »
      10 years ago, hide # ^ |
       
      Vote: I like it +3 Vote: I do not like it

      I haven't seen sparse table for range sum query, maybe that's where you need O(logN) per query but for range minimum/range maximum you can achieve O(1) and for range GCD you can achieve O(GCD_Complexity).

»
10 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Lets show them with <preprocess , query , update an element>.

sparse table <O(nlgn) , O(1) , O(nlgn)>

segment tree <O(n) , O(lgn) , O(lgn) >

Here you can find something more!

  • »
    »
    10 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    How, is querying sparse tabke O(1) ? . Let's say I want to query a interval of length n. Then, the number of operations I would have to do will be equal to number of set bits in n right ? In worst case, the number of set bits in n could be log(n)

    • »
      »
      »
      10 years ago, hide # ^ |
       
      Vote: I like it +5 Vote: I do not like it

      Let's t[d][i] minimum in range [i; i+2d). Query in range l, r find minimum. Take maximal d that 2d  ≤  r - l + 1. Answer will be minimum t[d][l] and t[d][r - 2d + 1].