Ehsan_sShuvo's blog

By Ehsan_sShuvo, history, 9 years ago, In English

Here is two implementation of this problem

Using Sparse Table: Submission Using Segment Tree: Submission

Time took by Sparse Table is 0.336 s ,where Segment Tree took 0.324 s. Memory took by Sparse Table is 8732 KB ,where Segment Tree took 3640 KB.

Problem is static,thats why Sparse Table should be fast!But here i couldn't see this..Where is the problem?Could anyone please find that out ?

Thanks in advance :)

  • Vote: I like it
  • -11
  • Vote: I do not like it

| Write comment?
»
9 years ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

Well, the construction of the segment tree is in O(N) and solve every query in O(logN), in the sparse table the construction is in O(NlogN) and every query in O(1) supposing that you find the log2 in constant time but the complexity of the function log2 of C++ is not constant, so in your code every query is solved in O(logN), you can try to use :
__builtin_clz(1) — __builtin_clz(len)
Or preprocess the log2 of every number.