Блог пользователя Lavishe

Автор Lavishe, история, 12 месяцев назад, По-английски

Hi everyone, Could you please suggest some techniques and how to apply them to solve this problem?

We are given an initially empty array a and q queries of the form id x. Each query id x means inserting element x at position id in array a (it is guaranteed that id <= size(a) + 1). It is also guaranteed that q <= 10^5. The output is to print the array a after q queries

  • Проголосовать: нравится
  • +15
  • Проголосовать: не нравится

»
12 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

What is the output?

»
12 месяцев назад, скрыть # |
 
Проголосовать: нравится +14 Проголосовать: не нравится

We can solve this problem using offline query trick + fenwick tree. We can try to solve this problem backwards and determine the actual position in the array. Initially, we set all the positions to 1 (occupied) in the array. We can do binary search to find the minimum position idx such that query(1, idx) == id. After doing so, it is guaranteed that idx is the actual position in the final array. Therefore, set ans[idx] = val and set idx to 0 in the fenwick tree (unoccupied).

»
12 месяцев назад, скрыть # |
 
Проголосовать: нравится +9 Проголосовать: не нравится

what about Treap algorithm?