Under construction

Still being built. Things may change

Choreography

Time limit0.5sMemory limit1024 MB

Summary
Apply cyclic shifts, pair swaps, and permutation moves to a line of dancers, then report where a given dancer stands after each prefix of moves.
Level

Hard8 of 10

Topics
Array, Simulation, Math
Solved
No attempts yet

Problem

The Hungarian National Dance Ensemble is practicing a new choreography. There are NN dancers numbered from 00 to N−1N - 1, where NN is even.

At practice, the choreographer first lines up the dancers. Positions are numbered from 00 to N−1N - 1, and at the start, position ii is occupied by dancer P[i]P[i].

After the line is formed, the choreographer calls a sequence of moves. Each move finishes before the next one begins. There are four types.

  1. Every dancer moves KK positions to the right cyclically, where 0≤K<N0 ≤ K < N. For each ii from 00 to N−K−1N-K-1, the dancer at position ii moves to position i+Ki+K. For each ii from N−KN-K to N−1N-1, the dancer at position ii moves to position i+K−Ni+K-N.

  2. Every dancer moves KK positions to the left cyclically, where 0≤K<N0 ≤ K < N. For each ii from KK to N−1N-1, the dancer at position ii moves to position i−Ki-K. For each ii from 00 to K−1K-1, the dancer at position ii moves to position i−K+Ni-K+N.

  3. For each ii from 00 to N/2−1N/2-1, the dancers at positions 2i2i and 2i+12i+1 swap places.

  4. Suppose position ii (0≤i<N0 ≤ i < N) is occupied by dancer rir_i before this move. For each jj from 00 to N−1N-1, dancer jj moves to position rjr_j.

The dancers occupy distinct positions after every move.

The choreographer plans a sequence of MM moves and adds them one at a time. Before adding the next move, he sometimes asks which position a given dancer occupies after all moves added so far. Simulate the planned moves and answer these questions.

Constraints

  • 1≤N≤1000001 ≤ N ≤ 100000
  • 0≤P[i]<N0 ≤ P[i] < N for each ii with 0≤i<N0 ≤ i < N
  • P[i]≠P[j]P[i] ≠ P[j] for each ii and jj with 0≤i<j<N0 ≤ i < j < N
  • 0≤M≤1000000 ≤ M ≤ 100000
  • 1≤Q≤2000001 ≤ Q ≤ 200000
  • 0≤K<N0 ≤ K < N
  • 0≤D<N0 ≤ D < N
  • Time limit: 500 ms. Memory limit: 1024 MB.

Examples1

  1. Example 1

    Input
    2
    0 1
    0
    1
    0
    
    Expected output
    0