Restructuring Company

Source: Codeforces 566D

  1. Read the problem and summarise the task requirements.

There are three operations in the problem. Two of them should look very familiar!

  1. How would you do the problem if there were no queries of type 2?
  2. Implement this idea in code.
  3. Does this data structure also support queries of type 2?
  4. A direct implementation of type 2 queries may perform a lot of unnecessary merges. Can we reduce this?
  5. Can we answer each individual type 2 query in less than linear time?
  6. Implement this algorithm in code.
  7. Submit your program for judging!