_amit.nehra_'s blog

By _amit.nehra_, history, 12 months ago, In English

There are N numbers(1 to N), arranged in increasing order. There are q operation that has to be performed on permutation. In ith operation, we'll have Li, Ri(two indices) and we have to reverse the permutation from arr[Li] to arr[Ri]. At last, after performing all operations, we have to return value at kth position.

N<=10^9
q<=10^5
1<= Li, Ri <=N
k<=N

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

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by _amit.nehra_ (previous revision, new revision, compare).

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Link to the question?

  • »
    »
    12 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It was in Online Assessment of SAP LAB, so I don't have the question itself.

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I think i have done similar question using Treap data structure.

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

unfortunately I can't help you :_(

»
12 months ago, # |
  Vote: I like it -9 Vote: I do not like it

You can use treap or splay tree.

»
12 months ago, # |
Rev. 2   Vote: I like it +6 Vote: I do not like it

You can use treaps as others have said but since this is from an OA, treap is definitely not the intended solution.

lets say after q queries x is at position k, then doing the q queries again in reverse would take x back to position x since it's a permutation. So what we can do is reverse query array and then find at what index k will be after q queries and this can be done quite easily. that index will be our answer.