real_father_of_goku's blog

By real_father_of_goku, 10 years ago, In English

How can we solve spoj problem INCSEQ using segment tree?

Here is the link to the problem

http://www.spoj.com/problems/INCSEQ/

»
10 years ago, # |
Rev. 6   Vote: I like it +11 Vote: I do not like it

I think, that you can solve this task in such way :

You will use K segment trees.

1. Sort all elements of given array in non-decreasing order.

About sort : if elements are equal — the minimal element will be element which has the rightmost position.

2. You should update every segment tree in such way :

sum = getSumOnPositionInPrevSegTree(1, myElementPosition - 1)

3. Le'ts add the value of sum in the current segment tree in position myElementPosition.

My AC code here

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

Another ways to solve the problem:

  1. BIT-based: d3HGAB
  2. Merge-sort based: becXSW
  • »
    »
    9 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Can you please explain how BIT is working for this problem? Also , why we need to increment a[i] during scanning the input

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

      Increment is because BIT is 1-based structure. Bit-based solution is simply in k turns calculate on each turn number of sequences of length i ending in pos.