#include "choreography.h"
#include <vector>
using namespace std;
int n;
vector<int> p, v;
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;
p = P;
v.resize(N);
for (int i = 0; i < N; i++) v[p[i]] = 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--; // swap to the left
else cur_even++; // swap to the right
if (cur_odd % 2 == 1) cur_odd++; // swap to the right
else cur_odd--; // 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 = v[D];
if (v[D] % 2 == 0) ans += pre_move_even;
else ans += pre_move_odd;
ans %= n;
if (moved) {
int x = D;
if (((x-pre_move_even+n)%n) % 2 == 0) x = (x-pre_move_even+n)%n;
else x = (x-pre_move_odd+n)%n;
ans = p[x];
}
if (ans % 2 == 0) ans += cur_even;
else ans += cur_odd;
ans %= n;
return ans;
}