#include "choreography.h"
#include <vector>
using namespace std;
int n;
vector<int> p1, p2;
int pre_move_even = 0, pre_move_odd = 0, cur_even = 0, cur_odd = 0;
bool moved = false;
void init(int N, vector<int> P) {
n = N;
p1.resize(N);
p2.resize(N);
for (int i = 0; i < N; i++) {
p1[P[i]] = i;
p2[i] = P[i];
}
return;
}
void move_right(int K) {
cur_even += K;
cur_even %= n;
cur_odd += K;
cur_odd %= n;
return;
}
void move_left(int K) {
cur_even += n-K;
cur_even %= n;
cur_odd += n-K;
cur_odd %= n;
return;
}
void swap_places() {
if (cur_even % 2 == 1) cur_even = (cur_even-1+n)%n; // swap to the left
else cur_even = (cur_even+1)%n; // swap to the right
if (cur_odd % 2 == 1) cur_odd = (cur_odd+1)%n; // swap to the right
else cur_odd = (cur_odd-1+n)%n; // swap to the left
return;
}
void move_around() {
moved = !moved;
// performing move_around an even number of times is the same as not performing it at all
swap(pre_move_even, cur_even);
swap(pre_move_odd, cur_odd);
return;
}
int get_position(int D) {
// current position is equal to the original position + pre-move shifts + move + post-move shifts
int ans = D;
if ((D-pre_move_even+n) % 2 == 0) ans = (ans-pre_move_even+n) % n;
else ans = (ans-pre_move_odd+n)%n;
ans %= n;
if (!moved) ans = p1[ans];
else ans = p2[ans];
if (ans % 2 == 0) ans += cur_even;
else ans += cur_odd;
ans %= n;
return ans;
}