Little Artem is fond of dancing. Most of all dances Artem likes rueda — Cuban dance that is danced by pairs of boys and girls forming a circle and dancing together.
More detailed, there are n pairs of boys and girls standing in a circle. Initially, boy number 1 dances with a girl number 1, boy number 2 dances with a girl number 2 and so on. Girls are numbered in the clockwise order. During the dance different moves are announced and all pairs perform this moves. While performing moves boys move along the circle, while girls always stay at their initial position. For the purpose of this problem we consider two different types of moves:
Value x and some direction are announced, and all boys move x positions in the corresponding direction.
Boys dancing with even-indexed girls swap positions with boys who are dancing with odd-indexed girls. That is the one who was dancing with the girl 1 swaps with the one who was dancing with the girl number 2, while the one who was dancing with girl number 3 swaps with the one who was dancing with the girl number 4 and so one. It's guaranteed that n is even.
Your task is to determine the final position of each boy.
Input
The first line of the input contains two integers n and q (2 ≤ n ≤ 1 000 000, 1 ≤ q ≤ 2 000 000) — the number of couples in the rueda and the number of commands to perform, respectively. It's guaranteed that n is even.
Next q lines contain the descriptions of the commands. Each command has type as the integer 1 or 2 first. Command of the first type is given as x ( - n ≤ x ≤ n), where 0 ≤ x ≤ n means all boys moves x girls in clockwise direction, while - x means all boys move x positions in counter-clockwise direction. There is no other input for commands of the second type.
Output
Output n integers, the i-th of them should be equal to the index of boy the i-th girl is dancing with after performing all q moves.
Examples
Input
6 3
1 2
2
1 2
Output
4 3 6 5 2 1
Input
2 3
1 1
2
1 -2
Output
1 2
Input
4 2
2
1 3
Output
1 4 3 2
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to simulate commands efficiently for large n (up to 1e6) and q up to 2e6. Commands are either:
shift all boys by x positions clockwise (if x positive) or counterclockwise if negative. Actually description: "value x and some direction are announced, and all boys move x positions in the corresponding direction." But input gives x possibly negative: "where 0 ≤ x ≤ n means all boys moves x girls in clockwise direction, while - x means all boys move x positions in counter-clockwise direction." So effectively, x can be negative: for x negative, move -x counterclockwise; i.e., shift by -x in opposite direction.
We can treat shift as adding x modulo n (clockwise positive). But careful: Boys moving x positions in clockwise direction means each boy goes to the girl that is x positions ahead in clockwise order? Since girls are numbered in clockwise order and stay in place. Initially, boy i is with girl i. If all boys move x positions clockwise, then each boy leaves his current girl and moves to the girl that is x steps further clockwise. So boy i ends up with girl (i + x) mod n (with 1-indexed, careful). So the mapping from girls to boys after shift: For girl j, which boy is with her? That would be boy (j - x) mod n (since boy originally at position (j - x) moves x to j). So the effect on the array ans[girl] = boy number.
Alternatively, maintain a variable offset: initially, boy i at girl i. After shift of +x (clockwise), the mapping becomes: girl j is paired with boy (j - x) mod n. So offset = (offset + x) mod n if we define offset such that boy i is currently at girl (i + offset) mod n? Let's define: after some moves, the boy originally numbered i is at girl (i + offset) mod n (with modulo n, 1-indexed). Then to answer: which boy dances with girl j? That would be the boy i such that (i + offset) ≡ j (mod n) => i ≡ j - offset (mod n). So ans[j] = ((j - offset - 1) mod n) + 1. So we need to track offset changes.
Shift command: offset = (offset + x) mod n (with x possibly negative, but we adjust to modulo). However careful about sign: if clockwise positive, then boy moves to girl i + x => i + offset' = i + offset + x? Let's define offset as the amount added to boy's index to get girl index. Initially offset=0 because boy i at girl i. After moving x clockwise, boy i goes to girl (i + x) => new offset = offset + x = x. So offset change is +x. So yes.
But note: shift command: "all boys move x positions in the corresponding direction". The input provides x which may be negative to indicate counter-clockwise. But the description: "value x and some direction are announced" and they give x with sign, where positive means clockwise. So indeed we can treat x as signed integer; offset = (offset + x) mod n.
Now command type 2: "Boys dancing with even-indexed girls swap positions with boys who are dancing with odd-indexed girls. That is the one who was dancing with the girl 1 swaps with the one who was dancing with the girl number 2, while the one who was dancing with girl number 3 swaps with the one who was dancing with the girl number 4 and so on."
Important: This swap is between adjacent pairs: (1,2), (3,4), (5,6), ... and n is even. So it's essentially a pairwise swap: for each k from 0 to n/2-1, swap the boys at girls 2k+1 and 2k+2.
We need to simulate this efficiently for many commands. q up to 2e6, n up to 1e6, naive O(n) per type2 would be too slow (2e6 * 1e6). But we only need final positions, not per step. We need to combine these operations.
Observation: Both operations are permutations on the set of boys (or mapping from girls to boys). Let's consider permutation P such that after all moves, each girl i is paired with boy P(i). Initially P(i)=i.
Command type1 (shift) corresponds to applying a cyclic shift to the mapping. Specifically, if we think of the array A of length n where A[i] = boy dancing with girl i, then shift by x clockwise (positive) means: new_A[i] = A[(i - x) mod n]? Let's check: originally A[i] = i. After shift +x, boy from girl i - x moves to i, so new_A[i] = old_A[i - x] (mod n). So it's a rotation of the array A by +x steps to the right? Actually if we think of indices as positions (girls), and we rotate the array right by x (so element from i-x goes to i), then new_A[i] = old_A[i-x]. That's correct. So type1 is a rotation of the entire array A by x positions to the right (clockwise). But note: x positive -> clockwise movement of boys -> rotation of A to the right (since each boy moves forward to next girl, the assignment shifts right). Check: For n=3? But n even? but anyway: Example: n=6, command1 2: initial A = [1,2,3,4,5,6]. After +2 clockwise, expected? Let's manually: Boy1 moves to girl3 (since 1+2=3), boy2->4, boy3->5, boy4->6, boy5->1 (5+2=7 -> 1 mod6), boy6->2. So girl1 gets boy5, girl2 gets boy6, girl3 gets boy1, girl4 gets boy2, girl5 gets boy3, girl6 gets boy4. So A becomes [5,6,1,2,3,4]. That's a right rotation by 2? The original [1,2,3,4,5,6] rotated right by 2 gives [5,6,1,2,3,4]. Yes, so rotation by x to the right. But note: we treat clockwise positive as right rotation. If x negative, it's left rotation by |x|.
Now command type2: swapping adjacent pairs (1,2), (3,4), ... This operation is a fixed permutation that swaps positions of the array A: For i odd (1-indexed), A[i] and A[i+1] swap.
Thus we need to apply a sequence of rotations and pairwise swaps on an array of length n. We want final array after q up to 2e6 commands.
We need to efficiently compute final permutation without O(n) per type2.
Observations: Both operations are linear permutations on positions. The composition can be represented by tracking the transformation of indices. Since n large but q also large, we need to combine the operations in O(q) time, not O(q * n). The operations are of two kinds: rotate (cyclic shift) and pairwise swap on odd-even pairs.
Idea: Represent the state by an offset and a parity flip? But the swap operation is not a simple global shift; it's local. However, note that the swap operation commutes in a certain way with rotations? Not directly. But we can try to maintain a mapping from current positions to original boys.
Alternatively, note that the operation set is reminiscent of operations on a binary string? But here it's just a permutation.
Let's think in terms of indices: Let f be a function that maps a girl index i to the boy index that is currently dancing with her. Initially f(i)=i. After a series of operations, f is some permutation. Each operation is a permutation applied to f.
We need to compute f(i) for all i.
Because n can be up to 1e6 and q up to 2e6, O(q) is okay, O(n) is okay, but O(q * log n) maybe borderline but likely okay if optimized in C++. But we cannot afford O(n) per type2, so must handle type2 in O(1) by updating some state variables.
Observation: The swap operation only swaps adjacent odd-even positions. If we combine it with rotations, we might be able to represent the overall effect as a combination of an offset and a "bit-reversal" within pairs? But the swap operation is not a global bit flip; it's a specific pattern relative to fixed indices (1,2,...). However, after rotations, the pattern of which positions are swapped changes relative to the current ordering? Actually careful: The command type2 is defined based on the original girl indices? Or based on the current pairing? Let's read problem: "Boys dancing with even-indexed girls swap positions with boys who are dancing with odd-indexed girls. That is the one who was dancing with the girl 1 swaps with the one who was dancing with the girl number 2, while the one who was dancing with girl number 3 swaps with the one who was dancing with the girl number 4 and so one. It's guaranteed that n is even."
The description: "Boys dancing with even-indexed girls swap positions with boys who are dancing with odd-indexed girls." That suggests the swap is based on the girl indices: they look at the current girls (who are fixed in positions) and swap the boys that are currently dancing with even-indexed girls with the boys currently dancing with odd-indexed girls. But then they give example: "the one who was dancing with the girl 1 swaps with the one who was dancing with the girl number 2, while the one who was dancing with girl number 3 swaps with the one who was dancing with the girl number 4 and so one." This indicates that they pair each odd-indexed girl with the next even-indexed girl (the one with index+1). So it's based on the original girl numbering (which is fixed). So the operation is: for all k from 0 to n/2-1, swap the values A[2k+1] and A[2k+2].
Thus it's independent of any previous rotations. It's a fixed permutation on the array positions.
So we have two types of permutations on positions: a cyclic shift (rotation) and a fixed pairwise swap pattern. Compose them many times.
We can think of representing the overall permutation as applying a sequence to the array indices. Since both operations are permutations on the set of positions {0,...,n-1} (if we use 0-index), we can compute the final permutation by applying the sequence to each index? But that would be O(nq). However we can apply the operations to the entire permutation representation efficiently if we can keep track of a transformation that maps final positions to initial positions or vice versa.
We can maintain two parameters: an offset (for rotation) and a flag indicating whether we need to swap adjacent positions? But the swap is not a toggle; it's an operation that swaps specific positions. If we apply two swaps in a row, they cancel out: swapping pairs twice returns to original. So the swap operation is an involution (its own inverse). So we could maintain a parity of how many swap commands have been applied modulo 2? But careful: The swap command may be interleaved with rotations, which changes the effect relative to the original positions. For example, if we have a rotation then a swap, that is not equivalent to a swap then a rotation; they do not commute. So just counting swaps modulo 2 is insufficient because the orientation changes after rotations.
But perhaps we can combine them into a single transformation on indices. Let's consider the index transformation approach.
Let us define the state as a bijection T from the current array index (girl position) to the original boy index (or to the index of the boy that is currently there). More concretely, suppose initially array A[i] = i. After a series of operations, A = f(i). This can be seen as applying permutations to the array elements. If we think in terms of the positions, we can track where each original element moves, but we need final assignment.
Alternate approach: simulate the operations on a "pointer" that tracks which boy ends up at a given girl. However we need all girls.
Because operations are permutations, we can compute the composition of the sequence of q permutations, each either R_x (rotation by x) or S (swap odd-even pairs). The total permutation can be applied to the initial array to get final array. We can compute the composed permutation as a function on indices.
Let P be the permutation on positions such that after all commands, the element originally at position i (i.e., boy i) ends up at position P(i) (i.e., girl P(i)). Then the final array A[girl j] = boy i where P(i) = j. So A[j] = P^{-1}(j). Or we can compute directly.
Alternatively, we can think of the operation on the array as: each command modifies the mapping. For command type1 (rotation by x), it's: new_A[i] = old_A[(i - x) mod n]. So it's like applying the inverse rotation to indices. For command type2 (swap), new_A[i] = old_A[i'] where i' = i if i even? Actually swap: for odd i, new_A[i] = old_A[i+1]; for even i, new_A[i] = old_A[i-1]. So it's a permutation on indices: S(i) = i+1 if i odd, i-1 if i even (with 1-index). Let's define in 0-index: positions 0,1,...,n-1. In 1-index they said girl 1 odd, 2 even. Let's switch to 0-index for easier modulo. Let positions 0,1,...,n-1 correspond to girls 1..n. Then initially A[0]=0, A[1]=1, etc. Rotation: x clockwise positive: as we saw, new_A[i] = old_A[(i - x) mod n] (since element from i-x moves to i). So operation R_x: A -> A' such that A'[i] = A[(i - x) mod n]. In terms of permutation on indices of the source array, it's a shift of indices: new_index = (old_index + x) mod n if we think of moving elements forward? Actually if we apply R_x to array A, we can think of the array being rotated right by x. So we can define transformation on positions: after operation, the element at position p goes to position (p + x) mod n. Because originally element at position (i - x) goes to i, so its new position is i = (old_pos + x) mod n. So the mapping for element positions is: new_pos = (old_pos + x) mod n.
For swap S: new_A[i] = A[i ^ 1] if n even? Let's see: In 0-index, odd/even in 1-index correspond: girl1 -> index0 (odd in 1-index?), actually girl1 is odd, index0 is even if we consider parity? But it's easier: In 0-index, the swap pairs indices (0,1), (2,3), (4,5), ... So indeed S maps index i to i XOR 1, but only if i is even? Actually XOR with 1 toggles the least significant bit. For any i, i XOR 1 swaps 0<->1,2<->3, etc. So yes, S(i) = i XOR 1. But careful: For i from 0 to n-1, XOR 1 is valid because n is even, so it stays within range. So S is an involution: S(S(i)) = i.
Thus we have two basic permutations on positions (indices of the array elements) when we think about where an element moves. But note: The earlier definition used A as mapping from girl index to boy. If we think of the array of boys positioned at girls, then operations affect the positions of the boys. So we can track the movement of each boy individually. Initially boy i is at position i. After each command, each boy's position changes according to the operation. The final answer is: for each position (girl) j, which boy is there? That's the inverse of the mapping from boy to position.
Thus we can define a current transformation on positions: For each boy i, we have its position pos[i] (0-index). Initially pos[i] = i. Commands update all positions simultaneously according to the operation:
Type1 (shift x): each boy moves x positions clockwise. Since girls are numbered clockwise, moving clockwise means increasing position? Let's define: positions are around circle. In 0-index, going clockwise means increasing index? We need consistency. In earlier example, +2 clockwise: boy0 (boy1) moved to position 2 (girl3). Starting at 0, after +2 clockwise, position becomes 2. So indeed clockwise corresponds to +x modulo n. So for each boy, new_pos = (old_pos + x) mod n. (Because he moves x positions in clockwise direction.)
Type2: "Boys dancing with even-indexed girls swap positions with boys who are dancing with odd-indexed girls." This means: if a boy is currently at an even-indexed girl (1-indexed even), he swaps with the boy at the adjacent odd-indexed girl? The description: "Boys dancing with even-indexed girls swap positions with boys who are dancing with odd-indexed girls." So the boys at even-indexed girls swap with the boys at odd-indexed girls. But which odd? The example says: the one dancing with girl1 (odd) swaps with the one dancing with girl2 (even). So it's specifically pairing each odd with the next even. So it's the same as swapping the boys at positions (0,1), (2,3), (4,5)... in 0-index? Let's check mapping: In 1-index, odd: 1,3,5,... -> 0,2,4,... in 0-index? Actually girl1 (odd) -> index0 (even index), girl2 (even) -> index1 (odd index). So the pairs are (index0, index1), (index2, index3), etc. So in 0-index, it's swapping the two positions in each pair where the first is even index and second is odd index. So if a boy is at an even index i (i even), he swaps with the boy at i+1; if a boy is at odd index i, he swaps with i-1. So effectively each boy's position becomes: if his current position is i, after swap he goes to i^1 (i XOR 1). But careful: Is it always that simple? If we apply the swap to the positions of the boys, then indeed each boy's position is mapped to the paired position. Since every position is involved, the effect is that the boys at i and i^1 exchange places. So from the perspective of a single boy, his new position = old_position XOR 1. Because if he is at i, after swapping, he ends up at the other position in the pair. But is that true for both even and odd? For i even, i^1 = i+1 (odd); for i odd, i^1 = i-1 (even). Yes. So type2 operation updates each boy's position as: new_pos = old_pos XOR 1.
Check with small example: n=4, initial pos: boy0 at0, boy1 at1, boy2 at2, boy3 at3. After swap, we expect boys at positions: boy0 at1, boy1 at0, boy2 at3, boy3 at2. Indeed, for boy0: 0^1=1; boy1:1^1=0; boy2:2^1=3; boy3:3^1=2. So correct.
Thus both commands are linear (affine?) transformations on the positions considered as integers modulo n? But the XOR operation is not linear modulo n. However, we can think of positions in binary representation? n up to 1e6, not necessarily power of two. But XOR with 1 is simple: it flips the least significant bit. However, modulo n, this operation is well-defined as long as n is even, so that the pairing works (each i XOR 1 stays within [0, n-1] because n is even, the highest bit beyond LSB unchanged). Since n is even, the maximum index n-1 is odd? For example n=6 (0..5). XOR 1 maps: 0<->1, 2<->3, 4<->5. Indeed, 4 (100) XOR1=5 (101), 5 XOR1=4. So it's still within range. So the transformation pos -> pos XOR 1 is a permutation on the set {0,1,...,n-1} as long as n is even (since if n is odd, the last index would be even and XOR1 could become n which is out of range). So that works.
Now we have two operations on the position of a boy:
R_x: pos = (pos + x) mod n
S: pos = pos XOR 1
We start with pos_i = i (for boy i). After a sequence of commands, we have pos_i = f(i). The final array answer: for each girl position j, we need the boy i such that pos_i = j. So we can compute the inverse mapping: ans[j] = i where pos_i = j. Since we can compute pos_i for each i, we could iterate over i and set ans[pos_i] = i. That would be O(n). But we need to compute pos_i efficiently without simulating each command per boy (which would be O(q n)). However, note that the transformation on positions is the same for all boys (they all follow the same rules, just starting from different initial positions). So pos_i = T(i), where T is the composition of the sequence of operations applied to an initial value i. That is, T is a function from initial position to final position, defined by applying a series of transformations: either add x modulo n, or XOR 1. So T is a composition of functions: T = op_q ∘ ... ∘ op_1, where each op is either t_add(x) : y -> (y + x) mod n, or t_swap : y -> y XOR 1.
Thus we can compute T as a single function that can be applied to any input i quickly, and then we can compute T(i) for all i in O(n) total time, after we have combined the q operations into a compact representation.
So the problem reduces to: given a sequence of q operations (q up to 2e6), each is either add modulo n (with x possibly negative) or XOR with 1, we need to compute the composed function T: Z_n -> Z_n (where Z_n = integers modulo n, but note XOR is not modulo, but it's a permutation on the set {0,...,n-1} assuming n even). Then compute T(i) for all i in [0, n-1] and output ans: for each j, find i such that T(i)=j, i.e., ans[j] = T^{-1}(j) + 1? Wait careful: pos_i = T(i). The final array A[j] = boy index i such that pos_i = j. So if we define mapping from i to final position, then the inverse mapping gives the boy at each position. So we could compute T(i) for all i, then for each i, set ans[T(i)] = i+1 (since boys numbered 1-index). That yields the output.
Alternatively, we could compute T_inv directly, but computing inverse may be more complex. But computing T(i) for all i and then filling ans is O(n). That's fine.
Now the main challenge: compress the sequence of q operations into a function that can be evaluated quickly for many i. Since q can be up to 2 million, and n up to 1 million, we could simply apply the sequence to each i individually: that would be O(n q) which is too big. But we can precompute T as a combination of affine? The operations are not all affine modulo n because XOR 1 is not affine modulo n, but note that XOR 1 is equivalent to adding 1 when the number is even? Not exactly. However, maybe we can represent the overall transformation as a linear function over the ring Z_2? But also modulo n.
Observe that n is up to 1e6, not necessarily a power of two. So XOR operation does not have a simple representation in modulo arithmetic. However, note that the only non-linear operation is XOR with 1. The composition of many adds and XORs might be expressible as a piecewise affine function depending on parity? Because XOR 1 flips the least significant bit. Adding can affect parity as well. Perhaps we can keep track of two parameters: an offset and a parity swap flag? Let's explore.
Let’s consider the effect of these operations on the parity (i.e., even/odd) of the position. Both operations change parity in predictable ways:
Add x modulo n: parity changes by parity of x (mod 2), because (pos + x) mod n: the parity (even/odd) of the result is parity(pos) XOR parity(x) if no wrap-around? But modulo n may change parity because adding x and then taking mod n could involve subtracting n if overflow. However, n is even, so subtracting n flips parity? n is even, so subtracting n changes parity? Even - even = even? Actually n is even, so subtracting n (even) does not change parity. Wait: If we add x and the sum >= n, then we take sum - n. Since n is even, sum and sum-n have same parity? Because subtracting an even number preserves parity. Yes, parity(sum - n) = parity(sum) because n is even. So overall, parity after addition mod n is simply parity(pos) XOR parity(x) (since modulo operation does not affect parity when n even). So addition mod n on an even modulus preserves parity change as parity(x). So it's safe: new_parity = old_parity XOR (x mod 2). (Where x mod 2 taken as 0 for even, 1 for odd.)
XOR 1: toggles the LSB, so it flips parity. So parity toggles.
Thus both operations are linear on parity: they either flip or not. So the net effect on parity is just the XOR of parity of all x's (with add) and the number of swaps mod 2.
But we need more than parity; we need the exact value modulo n.
Idea: Since XOR 1 only affects the least significant bit, perhaps we can think of the overall transformation as an affine transformation on the quotient group Z_n / (mod 2)? Not exactly.
Alternatively, we can consider representing the transformation as a function of the form: T(i) = (a * i + b) mod n? But the swap operation is not of that form. However, maybe we can represent it as: T(i) = f(i) XOR g(i)? Hmm.
Another approach: Since operations are only add and XOR 1, and we need to apply them many times, we could treat the transformation as a finite state automaton on the bits? But n can be any even number up to 1e6, which is not necessarily power of 2. XOR 1 only toggles the least significant bit, but the higher bits may be affected when we add and cause carries that propagate. So not trivial.
But maybe we can simulate the sequence of operations on the positions "in bulk" by maintaining a permutation on the set of positions as a function that we can apply to each input quickly using some precomputed mapping? Since n is up to 1e6, we could represent the transformation as a permutation array of size n after processing all q commands, by applying the commands to the entire array in O(q + n) time? But command type2 (swap) would be O(n) if we actually apply it to the whole array each time, leading to O(q n) again. However, note that type2 is a specific simple permutation (swapping adjacent pairs). We might be able to apply it lazily using a data structure that supports rotations and swaps efficiently? Maybe we can maintain the array under two operations: rotate by x, and swap adjacent pairs (1,2), (3,4), ... This is reminiscent of maintaining a sequence with two operations: cyclic shift and pairwise swap. Could we use a balanced binary tree with lazy propagation? Possibly, but q up to 2e6, n up to 1e6, we need very fast operations, O(log n) per command might be borderline but 2e6 * log(1e6) ~ 2e6 * 20 = 40 million, which might be okay in C++ with good optimization. However, we need to output the final array, which would be O(n) to extract. So using a segment tree or treap to represent the sequence with operations might work. But the operations are rotation (which can be handled by maintaining a shift offset on a circular array) and swapping of adjacent pairs, which is like applying a specific permutation pattern. However, if we maintain the array as an implicit treap, we could implement rotation by splitting and concatenating (O(log n)). For swap adjacent pairs, we would need to perform many swaps (n/2 swaps) if we apply it naively. But we could maybe apply the swap operation as a transformation on the treap that swaps each pair in O(log n) per pair? That would be O(n log n) per type2, too slow.
But perhaps we can represent the swap operation as a global transformation that can be applied by changing the interpretation of indices (like toggling a flag that says "the array is in swapped state relative to a base ordering" but combined with rotations it's tricky). Actually, there is a known trick: The combination of cyclic shifts and adjacent swaps (like the "swap adjacent pairs" pattern) can be represented by keeping track of an offset and a parity flip for the index mapping. Let's test with some examples.
We want to know, after a sequence of operations, for each original index i (boy), where does he end up. Alternatively, for each final position j, which boy is there.
Consider we maintain the array A of boys at positions. Initially A[j]=j.
If we apply a rotation by x, we can just shift the array: effectively we have a variable offset that indicates that the actual array is the original array rotated by offset. Suppose we store an offset such that the actual array A at any time can be obtained from a base array B by: A[(i + offset) mod n] = B[i] (or something). But we also have swaps that modify the ordering.
Observation: The swap operation (pairwise) is a fixed permutation that commutes with rotations up to a change? Let's compute the composition. Let R be rotation by x: R(i) = (i + x) mod n. Let S be swap: S(i) = i XOR 1.
We have S ∘ R: first rotate, then swap. Applied to a position i (the element moves), we have: element initially at i goes to R(i) then to S(R(i)) = (i+x) mod n XOR 1.
On the other hand, R ∘ S: first swap then rotate: S(i) = i XOR 1, then R(S(i)) = (i XOR 1 + x) mod n.
These are not equal in general. But maybe we can express S ∘ R as R' ∘ S, where R' is some other rotation? Let's check: S(R(i)) = (i+x) XOR 1. R(S(i)) = (i XOR 1) + x. Are these equal? Not generally. However, note that (i+x) XOR 1 vs (i XOR 1) + x. For them to be equal for all i, we would need something like XOR 1 commutes with addition modulo n, which is not true.
But maybe we can find a representation where the overall transformation is of the form: T(i) = (i + a) XOR b? Or something involving a bitwise XOR with a constant? Since XOR 1 is flipping LSB, and addition can be decomposed into bits with carries. But maybe due to the special nature (only XOR with 1, and n even), the transformation might be expressed as either: T(i) = (i + offset) mod n, or T(i) = (i + offset) mod n with a parity-dependent adjustment? Let's test small sequences manually.
Suppose we have operations: R_1 (add 1), then S (XOR 1). Then T(i) = S(R(i)) = (i+1) XOR 1.
Alternatively, if we have S then R_1: T(i) = (i XOR 1) + 1 mod n.
Are these equivalent to something like (i + c) XOR d? Let's try to see if (i+1) XOR 1 can be expressed as (i XOR something) + something? For any i, (i+1) XOR 1 = i XOR? There's known identity: (i+1) XOR 1 = i XOR 0? Let's compute small: i=0: (0+1)=1 XOR1=0; i=1: 2 XOR1=3? Actually 2 XOR1=3, but (1+1)=2 XOR1=3. i=2: (2+1)=3 XOR1=2. i=3: (3+1)=4 XOR1=5? 4 XOR1=5. So pattern: results: 0,3,2,5,... That's not simply i+ something constant. It appears to be: when i even, (i+1) is odd, XOR1 gives even i? For i=0 ->0, i=2->2, i=4->4? Check i=4: (4+1)=5 XOR1=4, yes. So for even i, it returns i. For odd i: i=1->3, i=3->5, i=5->7? But if n=6, 7 mod? We'll ignore wrap. So for odd i, it returns i+2? Since 1->3, 3->5, 5->7. So (i+1) XOR 1 = i if i even, = i+2 if i odd (assuming no wrap). So it's like adding 0 or 2 depending on parity. That's not a simple affine transformation over Z_n.
Now what about (i XOR 1) + 1: i=0: 0 XOR1=1 +1=2; i=1:1 XOR1=0+1=1; i=2:2 XOR1=3+1=4; i=3:3 XOR1=2+1=3; i=4:4 XOR1=5+1=6; i=5:5 XOR1=4+1=5. So for even i (0,2,4): results: 2,4,6; for odd i (1,3,5): 1,3,5. So it's: if i even -> i+2; if i odd -> i. So it's also parity-dependent.
Thus these transformations are not simple global shifts.
But maybe we can maintain two separate sequences for even and odd positions? Since the swap operation only mixes even and odd positions within each pair, and rotation by an even number might keep parity, but rotation by odd number flips parity overall? Wait, adding an odd number toggles parity of all positions (since n even). So the effect of operations might be that the set of even and odd positions get mixed.
Observation: The set of positions is partitioned into two parity classes. Adding an odd x will map evens to odds and odds to evens (since parity flips). Adding an even x preserves parity. Swapping also toggles parity (since XOR 1 flips parity). So both operations can change parity, but in different ways.
Maybe we can think of the transformation as a composition that can be represented by two offsets: one for even indices, one for odd indices, relative to some base ordering. Let's try to derive.
Consider we want to know final position of boy i. The operations are applied to his position. Suppose we maintain the current position as a variable p. After each operation, we update p. So for each boy, p evolves through the same sequence of updates. So we need to compute for a given starting p0 = i, the final p after applying q ops. This is like evaluating a function built from additions modulo n and XOR with 1.
Now, q is up to 2e6. Could we precompute this function for all i by simulating the sequence once but applying to all i simultaneously using some clever representation? For example, we could maintain an array mapping from positions to boy indices (i.e., the current permutation) and apply each command to the entire array by transforming the array. But as we said, applying type2 naively would be O(n). But maybe we can apply type2 efficiently by noting that it's a fixed pattern and we can store the array in a way that allows O(1) application? Let's think about data structures.
We can maintain the array as a double-ended queue or circular array with an offset for rotations. For type1, we just update an offset variable. For type2, if we have an offset representing a rotation, can we combine the swap with the offset to avoid O(n) work? Possibly by toggling a flag that indicates that the array is currently "swapped" relative to its underlying representation, but then rotations would need to account for that.
Imagine we have a base array B of length n, initially B[i] = i (boy at position i). We maintain two variables: offset (rotation) and a boolean "swapped" which indicates whether we should interpret the array as having performed a pairwise swap after rotation? However, because rotation and swap do not commute, a single boolean might not suffice. But maybe we can maintain a more complex state: offset for even positions and offset for odd positions separately.
Let's formalize: The operations on the array A (positions 0..n-1) are:
Rotate right by x (i.e., shift all elements clockwise). Equivalent to: new_A[i] = A[(i - x) mod n]. In terms of the underlying permutation, it's a cyclic shift.
Swap adjacent pairs: new_A[i] = A[i ^ 1].
We want to apply a sequence of these.
We could maintain the array as a linked structure, but we need to output final array after all commands.
Idea: Since n <= 1e6 and q <= 2e6, perhaps O(q) operations that each take O(1) or O(log n) is acceptable. But we need to handle type2 in O(1) amortized. Can we implement type2 as an O(1) operation by modifying some state variables, without actually touching the array? Possibly by keeping track of a permutation that is the composition of the swaps and rotations. Since both are permutations on the index set, the overall permutation is a bijection. We could maintain a representation of that permutation as a function f: index -> index that tells where the element originally at index i currently resides (or the inverse). But updating this representation for all i per command would be O(n). However, perhaps we can maintain the permutation as a combination of two simple transformations: a rotation (which is easy) and a bitwise XOR (which is also easy). But as we saw, composing them yields more complex functions.
But maybe the composition of any sequence of adds and XOR 1 can be reduced to a function of the form: f(i) = ((i XOR a) + b) mod n, where a is either 0 or 1? Let's test with some sequences.
Take R_1 then S: f(i) = (i+1) XOR 1. Can this be expressed as (i XOR 1) + c? Or i XOR d + e? Check if there exists constants a (0 or 1) and b such that f(i) = (i XOR a) + b mod n. For a=1, b=0 gives i XOR 1. Not match. a=0, b=1 gives i+1. Not match. For a=1, b=2? (i XOR 1) + 2 mod n. For i=0: (0 XOR1)=1+2=3, but f(0)=0. So no. For a=0, b? Not.
What about f(i) = ((i + b) XOR a)? For a=1, b=1: (i+1) XOR 1. That's exactly f. So indeed f(i) = (i + b) XOR a with a=1, b=1. Good. So it is of the form (i + b) XOR a, where a is either 0 or 1 (since XOR with 1 only). Actually, could a be larger? XOR with 1 only flips LSB; XOR with other constants would be needed for multiple swaps? But we only have XOR 1 operation. So after many swaps, the XOR part might accumulate to XOR with some value? Since XOR 1 applied multiple times: XOR 1 applied an even number of times cancels out (i.e., identity), applied odd number of times is XOR 1. However, when interleaved with additions, the XOR may effectively become XOR with 0 or 1? But can it become XOR with something else? Let's examine composition of add and XOR. Consider S ∘ R_x ∘ S: first swap, then rotate, then swap. Compute: i -> i XOR 1 -> (i XOR 1 + x) mod n -> ((i XOR 1 + x) mod n) XOR 1. That's ((i XOR 1) + x) XOR 1. Is that representable as (i + b) XOR a? Possibly with a=0? Let's test: maybe it becomes i + something? Not sure.
But maybe the general composition of adds and XOR 1 results in a function of the form: f(i) = (i + offset) XOR flip, where flip is 0 or 1, but with the addition possibly being conditional on parity? However, our test with S ∘ R_1 gave (i+1) XOR 1, which is exactly that form with offset=1, flip=1. And R_1 ∘ S gave (i XOR 1) + 1 = (i + 1) XOR 1? Actually (i XOR 1) + 1 =? For i even: i+2, for i odd: i. That's not of the form (i + b) XOR a? Let's compute (i+1) XOR 1: earlier we saw gives i for even, i+2 for odd. Meanwhile (i XOR 1) + 1 gives i+2 for even, i for odd. So they are different. But is (i XOR 1) + 1 equal to (i + 1) XOR 1? No, they are not identical, but they are symmetric. However, note that (i XOR 1) + 1 can be written as (i + 1) XOR 1? Not equal. But maybe it can be written as (i + c) XOR d for some c,d? Let's attempt to solve for a,b such that (i XOR 1) + 1 = (i + b) XOR a for all i. For i even: LHS = i+2; RHS = (i+b) XOR a. For i odd: LHS = i; RHS = (i+b) XOR a. Let's try a=1. Then RHS = (i+b) XOR 1. For i even: we want (i+b) XOR 1 = i+2. For i odd: (i+b) XOR 1 = i. Let's test b=1: For i even: (i+1) XOR 1 =? If i even, i+1 odd, XOR1 gives even i? Actually (i+1) XOR 1 = i (since (i+1) XOR 1 = i if i even? Check i=2: (3) XOR1=2. So gives i, not i+2. b= -1? But b must be integer mod n. Maybe a=0: RHS = i+b. Then for i even: i+b = i+2 => b=2; for i odd: i+b = i => b=0. Contradiction. So not of that form.
But maybe it can be expressed as ((i XOR 1) + 1) = (i + 1) XOR 1? Not equal. So (i XOR 1) + 1 is not of form (i + b) XOR a with a in {0,1}? Let's attempt a=1, b= -1? (i - 1) XOR 1. For i even: (i-1) odd XOR1 = i? Not i+2. a=1, b=3? For i even: (i+3) XOR1? i=2:5 XOR1=4 (i+2). i=4:7 XOR1=6 (i+2). For i odd: i=1:4 XOR1=5 (i+4) not i. So no.
Thus not all compositions result in that simple form. So the set of functions generated by adds and XOR1 is larger than just affine with a single XOR 1. It might include functions that are piecewise linear based on parity, which can be expressed as: f(i) = (i + even_offset) if i even, and (i + odd_offset) if i odd? Actually from examples: S∘R_1 gave: even i -> i, odd i -> i+2. That's like: if even: +0; if odd: +2. R_1∘S gave: even: +2; odd: +0. So both are parity-dependent additions. Also note that the addition values are congruent mod something? They differ by 2. So maybe the general form after any sequence is: f(i) = (i + a) XOR b? But we saw R_1∘S not fitting. However, maybe we can represent it as f(i) = (i + c) XOR d, but with a twist that addition is modulo n and we allow d to be either 0 or 1? Let's try to see if R_1∘S can be expressed as (i + c) XOR d for some c,d. We solved and found no solution. So not.
But perhaps we can represent it as: f(i) = (i XOR d) + c, which is the same as (i + c) XOR d if there is no carry? Actually (i XOR d) + c is not same as (i + c) XOR d generally due to carries. So maybe the generated functions are exactly those of the form: f(i) = (i + a) XOR b or f(i) = (i XOR b) + a? But they are not equivalent.
Let's try composing two swaps and an add: S ∘ R_x ∘ S. Compute: i -> i XOR 1 -> +x -> XOR 1 => ((i XOR 1) + x) XOR 1. Could that be (i + something) XOR something? Expand: let j = i XOR 1. Then (j + x) XOR 1. Since XOR 1 is involutive, maybe we can rewrite as (i + x') XOR 1? Not straightforward.
Alternatively, maybe we can think of the transformation on the binary representation of i, but n is not necessarily a power of two, so bits beyond log2(n) wrap? That's messy.
Another approach: Since the only non-linear operation is XOR 1, which only affects the LSB, perhaps we can decompose the transformation into a linear part over the integers modulo n but with a conditional based on the LSB. This suggests we could maintain two separate offsets: one for even positions and one for odd positions. Indeed, because XOR 1 swaps between even and odd groups, and addition by x maps evens to (even plus x) mod n, but the parity outcome depends on x parity. But if we keep track of how even and odd indices are transformed, maybe we can combine operations in O(1) each.
Let's attempt to derive an invariant. Represent the current state as a pair (delta_even, delta_odd) indicating that an original boy with index i ends up at position: if i even then (i + delta_even) mod n? But initial: delta_even=0, delta_odd=0. However, after a swap, the mapping becomes: even i (original) goes to odd position? Wait, we need a more systematic approach.
We want to know, for each boy i (starting position i), where he is now. Let's define two functions: for even i and odd i. Because operations might treat them differently. But we can track the overall transformation as an affine function on each parity class separately? Let's test.
Initially: for any i, pos(i) = i.
After a rotation by x: pos(i) = (i + x) mod n. This is same for all i, regardless of parity, but it can be expressed as: if i even, new pos = (even + x) mod n; parity of new pos = parity(i) XOR parity(x). So the mapping from even i to positions may not stay within even set if x odd.
After a swap: pos(i) = i XOR 1. This swaps parity: even i -> odd positions, odd i -> even positions.
Thus the parity of the current position depends on both the original parity and the sequence of operations. So we cannot simply track separate offsets for even and odd that are additive, because swaps interchange the groups.
Idea: Keep track of a current permutation as a composition of two kinds of operations on the set of positions. Since the operations are permutations, we can represent them as functions from positions to positions. But we need to apply them to the initial array efficiently. Could we maintain a representation of the array as a circular linked list with two types of operations that can be done in O(1)? Possibly by using a trick: The swap operation essentially swaps each adjacent pair. If we consider the array as a sequence of pairs (0,1), (2,3), ..., then a swap just swaps the two elements within each pair. This is a local operation that could be applied by toggling a flag per pair? But there are n/2 pairs; we could maintain a global flag that indicates whether within each pair the order is swapped or not. However, rotation would then mix pairs, so the flag would need to be applied after rotation.
Maybe we can think in terms of representing the array as a list of pairs, and rotations are performed on the list of pairs as a whole, plus a possible rotation within a pair? Let's attempt.
Group positions into blocks of size 2: block k corresponds to positions (2k, 2k+1) for k = 0,..., n/2 -1. Initially, each block contains two boys: (2k, 2k+1). Operation S (swap) just swaps the two elements within each block. Operation R_x (rotation by x) rotates the entire array of positions, which may shift elements across block boundaries. However, if we consider the array of blocks, a rotation by x can be decomposed into: shift blocks by floor(x/2) (since each block is 2 positions), plus an extra offset of x mod 2 which determines whether the rotation aligns with block boundaries or not.
Specifically, a rotation by x positions can be thought of as: first, rotate the block-level array by floor(x/2) (if x even, it's just block rotation; if x odd, it's block rotation plus a half-block shift). The half-block shift would essentially move elements from one block to the next with a possible swap within the block? Actually, rotating by an odd number means that after moving, the elements are not aligned with block boundaries; they end up crossing the boundary between blocks. This can be represented as: do a block rotation by floor(x/2), then apply a "swap within each block" maybe? Let's test.
Suppose we have array A of length n as sequence of positions. Group into pairs: Pairs: (A[0],A[1]), (A[2],A[3]), ... Consider rotating right by 1 (clockwise). New array A'[i] = A[(i-1) mod n]. Let's see how this affects the pairs. Write A as pairs: block 0: (a0,a1), block1: (a2,a3), etc. After rotation by 1, the new pairs become: block0: (A'[0], A'[1]) = (A[n-1], A[0])? Actually A'[0]=A[-1 mod n] = A[n-1]; A'[1]=A[0]. So block0 becomes (A[n-1], A[0]). Originally, the last block (block n/2 -1) is (A[n-2], A[n-1]) if n even. So we see that the elements from two different original blocks combine into a new block. This is messy.
But perhaps we can treat the whole process using two pointers: one for starting offset and a flag indicating whether the ordering within pairs is swapped. This is reminiscent of problems where you have an array and operations: rotate and reverse segments. In those, you can maintain a deque and lazy reversal.
Let's consider we maintain the array as a circular array of n elements, but we also have a "pair-swapped" flag? Not exactly.
Observation: The swap operation only exchanges elements at positions (0,1), (2,3), ... independent of rotation. If we have a rotation offset, then the swap effectively swaps the elements at positions (offset + i) mod n? Actually, if we think of the physical positions fixed, the swap is always applied to those fixed positions. So if we maintain the array in terms of a physical array A (girl indices), we need to apply swap directly to that array. However, if we also maintain an offset for rotations, we could apply rotations by just changing offset, and apply swaps by modifying the underlying array in a way that interacts with offset.
Let's define that we store the array in some "canonical order" that allows O(1) application of both operations. For example, we could store the array as a double-ended queue (deque) where rotation is just moving front/back, and swap is a specific permutation on the deque. But swap would still be O(n) if we apply to all pairs.
But note that q is up to 2e6; we could afford an O(n) preprocessing and then O(q) steps each O(1) by maintaining some global state variables that encode the permutation. The goal is to output final array after all commands, not after each. So we need to compute the final permutation as a function of the sequence.
Thus we need to combine the sequence of commands into a compact form that can be applied to the initial array in O(n) time.
Idea: Represent the transformation as a permutation on indices that is a composition of rotations and pairwise swaps. Since both are permutations, the composition is a permutation. We can compute this permutation by symbolically applying the operations to the indices themselves, not the array. That is, we can maintain a function g: current_position -> original_boy_index, or h: original_boy_index -> current_position. Initially, h(i)=i. Each command updates h accordingly. For type1: h(i) becomes (h(i) + x) mod n? Wait careful: h(i) is the current position of boy i. If we rotate by x clockwise, each boy moves x positions clockwise, so his position becomes (h(i) + x) mod n. So we update h(i) for all i. That would be O(n) per command if we loop over i. But maybe we can represent h in a compressed form: as a function of i that we can evaluate quickly later. But we need to update this representation for each command without iterating over all i.
We could store h as a combination of an offset and a permutation that is either identity or swap. But after many commands, h may become complex. However, note that commands only modify the entire mapping in a uniform way: type1 adds a constant; type2 applies XOR 1. So h(i) is obtained by starting with i and applying a sequence of adds and XORs. This is exactly the function we want to compute for all i. So we need to compute the composition of many adds and XORs. This is reminiscent of evaluating a sequence of operations on each input, which is like a "walk" along a graph of states for each input, but we can compute the final function by processing the sequence once from the end to the beginning, using some data structure that captures the transformation.
Maybe we can think backwards: We want to know which boy ends up at each position j. That's equivalent to: after applying all commands, for each position j, which initial boy is there. If we reverse the commands, we could start from final positions and apply inverse commands to get initial positions. Then we could compute the mapping from final position to initial boy by applying the reversed sequence to each j. But again we need to apply to all j. However, the inverse operations are also adds and XOR: inverse of add x is add -x; inverse of XOR 1 is XOR 1 (self-inverse). So the reversed sequence is also a sequence of adds and XORs. So same difficulty.
Another thought: Since n is up to 1e6, we could simulate the process for all i in parallel using bitset or vectorized operations? But q is 2e6, so that would be 2e6 * 1e6 = 2e12 operations, impossible.
We need to reduce the per-command cost to O(1) by updating some global state variables that capture the entire transformation. Perhaps the transformation always belongs to a small class, like the set of functions that are either rotations or rotations followed by a swap? But from earlier, S∘R_1 is not a pure rotation, but it is a rotation with a swap? Actually S∘R_1 =? Let's compute its effect on the array: Starting from A, after R_1 (rotate right by 1) then S (swap pairs). Alternatively, maybe it's equivalent to first swap then rotate by something? Let's see: S∘R_1 =? We computed the transformation on positions: i -> (i+1) XOR 1. This is not a simple rotation. But maybe it can be expressed as a rotation of the array if we reorder indices? The array after these operations might be something like: A'[i] = ... Not a simple rotation.
Maybe the set of possible permutations generated by these operations is actually the whole dihedral group? Not sure.
Let's brute think: The operations are: rotations (cyclic shifts) and swaps of adjacent pairs in a fixed partition. This group might be the whole symmetric group for large n? But we don't need group properties; we need efficient composition.
Given constraints, maybe the intended solution is to simulate the operations using a lazy approach with O(1) per command by maintaining two offsets: one for the starting point and a flag indicating whether the even and odd positions are swapped. Let's try to derive such representation.
Consider we maintain an array representing the current ordering of boys, but we store it in a "canonical" order where we have performed a rotation to align the start, and we also have a flag that tells whether within each consecutive pair (starting from the start) we need to swap the two elements. However, because rotation can be by an odd amount, the alignment of the pairs relative to the start changes. So perhaps we maintain two parameters: start (an integer offset) and a boolean "flip" that indicates whether the pairs are swapped relative to the natural order from the start. But we must verify if this representation is closed under the operations.
Let's define a state as (offset, swapped), where offset is an integer from 0 to n-1 representing the index in the original array of the element that is currently at position 0? Wait, we need to define carefully.
We have an array A of length n (positions 0..n-1). We want to represent it as a transformed version of a base array B (which is initially [0,1,2,...,n-1]) such that we can compute A quickly. Suppose we define that the array A can be obtained from B by first possibly swapping each adjacent pair (0<->1, 2<->3, ...) if a flag f is true, and then rotating the entire array by offset r. That is: A[(i + r) mod n] = (if f then (B[i] with possible swap?) Actually, we need to define mapping from positions to values.
Let base array B where B[i] = i (boy index). Then we apply two transformations: a "pair-swap" (S) which swaps within each pair, and then a rotation (R_r) which shifts the array right by r (i.e., A[i] = something). However, the order matters: we have sequences of swaps and rotations interleaved. But perhaps any sequence can be reduced to at most one swap applied at the end (or beginning) after an appropriate rotation? But our earlier counterexample suggests not: S∘R_1 gave a permutation that is not simply a rotation followed by a swap. But is it a swap followed by a rotation? Let's check: S∘R_1 vs R_a∘S. We need to see if S∘R_1 = R_a∘S for some a. Solve: For all i, (i+1) XOR 1 = (i XOR 1) + a (mod n). This would require a such that (i+1) XOR 1 = (i XOR 1) + a. Test i=0: left=0 XOR1? Actually (0+1)=1 XOR1=0; right = (0 XOR1)=1 + a => 1+a ≡0 mod n => a ≡ -1 mod n. For n>1, a = n-1. Test i=1: left=(1+1)=2 XOR1=3; right = (1 XOR1)=0 + (n-1) = n-1. For n=6, right =5, not 3. So not equal. So not of that form.
What about R_a∘S∘R_b? That is a rotation, then swap, then rotation. Could that represent any composition? Possibly yes, since group generated by rotations and swap might be expressed as rotations and at most one swap in the middle? But we have many swaps and rotations interleaved. However, any sequence can be reduced to at most one swap and a rotation? Let's test with two swaps and a rotation: S ∘ R_x ∘ S. Could that be expressed as R_y or R_y ∘ S? Let's compute S R_x S. For n large, maybe it simplifies to something like R_{x'}? Let's compute effect on positions: i -> i XOR 1 -> +x -> XOR 1 => ((i XOR 1) + x) XOR 1. Is this equal to i + something? Not generally. For x=1, we got: ((i XOR 1) +1) XOR 1. Let's compute that explicitly for i. i even: i=0 => (1+1)=2 XOR1=3? Actually step: i=0 -> 0 XOR1=1 -> +1=2 -> XOR1=3. So result 3. That's i+3? i=0 ->3, so maybe +3. i=2 -> (3+1)=4 XOR1=5 => +3? 2+3=5. i=4 -> (5+1)=6 XOR1=7 => 4+3=7 if n>7. For odd i: i=1 -> (0+1)=1 XOR1=0 => i-1. So not a constant add. So not a pure rotation. Could it be R_y ∘ S? Check if exists y such that S R_x S = R_y S. That means for all i, ((i XOR1)+x) XOR1 = ((i XOR1) + y) mod n? Actually R_y S(i) = (i XOR1) + y. We want that equal to ((i XOR1)+x) XOR1. Let j = i XOR1, then we need (j + x) XOR1 = j + y for all j (since j runs over all indices as i does? Because XOR1 is bijection, j runs over all indices). So we need (j + x) XOR1 = j + y for all j. This would mean that adding x and then XOR1 is equivalent to adding y. That's unlikely unless x=0 and y=0? For j even and odd might differ. So not.
What about S ∘ R_x ∘ S = S ∘ R_x ∘ S. Could it be expressed as S ∘ R_y? That would be (i + y) XOR1. That is different from our expression.
Thus the group generated is larger; it might be the whole wreath product? Possibly the set of permutations that preserve the pairing structure? Let's examine the effect on pairs: The swap operation S swaps within each pair, but does not mix elements between different pairs. The rotation operation can move elements across pairs. So the combination can move elements arbitrarily around, but perhaps still respects some parity structure.
Wait, rotation moves elements to different positions; because the pairing is based on fixed positions, after rotation, what used to be a pair may be broken. So elements can move between pairs. So the group might be the full symmetric group? But maybe not.
Given constraints, there must be a simpler solution. Let's re-read problem statement: "During the dance different moves are announced and all pairs perform this moves. While performing moves boys move along the circle, while girls always stay at their initial position." So boys move; girls are fixed.
The two moves described are:
Value x and some direction: all boys move x positions in that direction.
Boys dancing with even-indexed girls swap positions with boys who are dancing with odd-indexed girls. That is specific pairing.
We need final positions.
Maybe we can consider the positions of boys as a permutation of girls. The operations:
Move all boys by x: this is a cyclic shift permutation (boys shift along the circle). Since the circle has n positions (girls), shifting is a rotation.
Swap even-odd: This is a specific involution that swaps within each adjacent pair.
This is exactly the same as before.
Given n up to 1e6 and q up to 2e6, the intended solution likely uses O(q) time overall, with O(1) per command, and then O(n) to construct answer. So we need to find a way to combine commands into a simple form.
Let's try to analyze the structure more mathematically. Define positions modulo n. The swap operation S: p -> p XOR 1. This is a specific function. The addition operation A_x: p -> p + x mod n.
We have a sequence of functions f1, f2, ..., fq, each either A_x or S. The composition F = fq ∘ ... ∘ f1.
Goal: compute F(i) for all i.
We can think of F as a function that can be expressed in a certain normal form. Since S is an involution and commutes with something? Maybe we can push all S operations to one side using commutation relations. Compute the commutator S A_x S A_{-x}? Let's find the relation between S and A_x.
We have S A_x (i) = (i + x) XOR 1.
A_x S (i) = (i XOR 1) + x.
We want to see if S A_x = A_y S for some y? That would mean (i + x) XOR 1 = (i XOR 1) + y for all i. As earlier, not generally. However, maybe S A_x = A_{x'} S A_{x''}? That is, we can rewrite product of S and A as something like A_a S A_b? This might allow us to move all S to one side, but we'll still have many A's interleaved.
Let's try to derive an identity: S A_x = A_{?} S A_{?}. Since S is linear in GF(2) but not in Z_n.
Alternatively, we can consider the effect on binary representation. Since n is even but not necessarily a power of two, the operation XOR 1 only affects bit 0. The addition mod n may cause carries into higher bits, but the only bit that interacts with XOR 1 is the LSB. Maybe we can decompose the transformation into two parts: a linear (mod n) part and a toggling of LSB that can be accounted for by considering the state of the LSB before addition.
We could process the sequence of commands and maintain two parameters: a rotation offset and a flag that indicates whether we need to XOR the final result with 1 (i.e., swap parity) based on the parity of some accumulated value. But as we saw, R_1 S gave a transformation that cannot be described by a single offset plus optional XOR at the end, because the effect is parity-dependent addition.
But maybe it can be described as: F(i) = (i + a) if i even, and (i + b) if i odd, where a and b are integers modulo n, and the addition may wrap but since n even, the parity of (i + a) is same as parity(i) XOR parity(a), so if a and b have same parity? Actually, for the mapping to be well-defined, we need that after applying, the result's parity is consistent with the mapping? But we want to represent F(i) as some function that depends on parity of i. So perhaps the general form is: F(i) = (i + c) XOR (d * (i mod 2)?) Not.
Let's compute F(i) for some arbitrary sequence manually to see pattern. Write a small script mentally? Maybe we can derive that the transformation always has the property that F(i+2) = F(i) + 2 (mod n) for all i? Let's test with our examples:
R_1: F(i)=i+1 mod n. Then F(i+2)=i+3 = F(i)+2. So holds.
S: F(i)=i XOR 1. Then F(i+2) = (i+2) XOR 1. Since XOR 1 flips LSB, (i+2) XOR 1 = (i XOR 1) + 2? Actually, (i+2) XOR 1 = (i XOR 1) + 2? Let's test: i=0: i+2=2 XOR1=3; i XOR 1=1; 1+2=3. i=1: 3 XOR1=2; i XOR1=0; 0+2=2. So yes, (i+2) XOR 1 = (i XOR 1) + 2. So F(i+2) = F(i) + 2 (assuming no wrap). If wrap occurs at modulus n, we need to consider mod n. Since n is even, adding 2 and mod n might wrap, but the relationship might hold modulo n? Let's check with wrap: Suppose n=6, i=4: i=4, F(4)=4 XOR1=5; i+2=6 mod6=0, F(0)=0 XOR1=1; Is 5+2=7 mod6=1, yes. So F(i+2) ≡ F(i)+2 (mod n) holds for S. Does it hold for R_1? i=4: i+2=6 mod6=0; F(4)=5? Wait R_1: F(i)=i+1 mod6. F(4)=5; F(0)=1; 5+2=7 mod6=1, holds. So seems both operations satisfy F(i+2) ≡ F(i)+2 (mod n). Check with composition: If two functions both satisfy this property, then their composition also satisfies? Let's check: Suppose F and G satisfy: F(i+2)=F(i)+2 mod n, G(i+2)=G(i)+2 mod n. Then (F ∘ G)(i+2) = F(G(i+2)) = F(G(i)+2) = F(G(i)) + 2 = (F ∘ G)(i) + 2. So yes, composition preserves the property. So all functions generated by these operations satisfy:
For all i, F(i+2) ≡ F(i) + 2 (mod n). (1)
This is a key invariant! Because both generators have this property, and it's preserved under composition. Let's verify for S ∘ R_1: we already computed some values: For n=6, F(0)=0, F(2)=? i=2: (2+1)=3 XOR1=2 => F(2)=2, and F(0)+2=2, works. i=1: F(1)=3, F(3)=? i=3: (3+1)=4 XOR1=5 => F(3)=5, F(1)+2=5, works. i=4: F(4)=4, F(2)=2? Actually F(2)=2, F(4)=4, so F(4)=F(2)+2. So property holds.
Thus any composition of rotations and swaps satisfies that it is a "2-periodic" isometry? Essentially, it means that the mapping F is determined by its values on two residues (e.g., 0 and 1), and then for any i, we can compute F(i) by adding multiples of 2. More precisely, if we write i = 2k + r, where r ∈ {0,1}, then F(i) = F(2k+r) = F(r) + 2k (mod n), because applying the property repeatedly: F(r+2k) = F(r) + 2k mod n. This holds as long as we can iterate the property k times. Since the property holds for all i, by induction we can show F(2k+r) = F(r) + 2k mod n. But careful: This uses that adding 2 repeatedly doesn't cross a wrap that breaks the pattern? However, the property is modulo n, and adding 2 each step may wrap after n/2 steps, but the relation still holds mod n because the property is an equality mod n. Let's check: For i = r + 2k, we can apply property k times: F(r+2k) = F(r) + 2k mod n. Yes, because each step adds 2 and mod n, the equality holds mod n. So indeed, F is completely determined by the values on 0 and 1. Then F(0) and F(1) are two numbers in Z_n. Moreover, note that the property also implies something about parity: F(0) mod 2? Since F(1) = F(0+1) but we don't have a direct relation between F(0) and F(1). However, from the property, we can derive that F(0) and F(1) must have opposite parity? Let's see: Consider F(1+2) = F(1)+2, but also F(1+2) = F(3). There's no constraint linking F(0) and F(1). So any two values F(0), F(1) are possible as long as the mapping is a bijection? But not all pairs produce a bijection; we need F to be a permutation. Given that F(i) = F(r) + 2k mod n for i = 2k+r, is this a permutation of Z_n? Let's check conditions: For distinct i, we need distinct F(i). Suppose i1 = 2k1+r1, i2 = 2k2+r2. Then F(i1) = F(r1) + 2k1, F(i2) = F(r2) + 2k2. If these are equal mod n, then F(r1) - F(r2) ≡ 2(k2 - k1) (mod n). Since n is even, we can analyze. For the mapping to be bijective, the two base values F(0) and F(1) must be such that the sets {F(0)+2k | k=0..n/2-1} and {F(1)+2k | k=0..n/2-1} are disjoint and together cover Z_n. This is equivalent to requiring that F(0) and F(1) have opposite parity (since adding 2 preserves parity). Because if F(0) is even, then all values in its orbit are even; similarly, if F(1) is even, its orbit would be even, overlapping with even from F(0) maybe? Actually, they need to partition the set into even and odd numbers. Since n is even, exactly half the numbers are even and half are odd. For the mapping to be a bijection, the image of even indices (i even) must be the set of either all evens or all odds, and the image of odd indices must be the complementary set. Because there are equal numbers of even and odd indices. So either:
F(even) subset of Evens, F(odd) subset of Odds, or
F(even) subset of Odds, F(odd) subset of Evens.
From the form F(i) = F(r) + 2k, parity of F(i) = parity(F(r)) (since adding 2 doesn't change parity). So all even i (r=0) map to parity = parity(F(0)); all odd i (r=1) map to parity = parity(F(1)). For the mapping to be bijective, these two parity sets must be disjoint and cover all parities, so we must have parity(F(0)) != parity(F(1)). So F(0) and F(1) must have opposite parity. That is necessary and sufficient for the mapping to be a bijection? Also we need that the two arithmetic progressions modulo n (step 2) are distinct and cover all residues. Since step 2 and n even, the progression {F(0) + 2k mod n} for k=0..n/2-1 is the set of residues congruent to F(0) mod 2, i.e., all numbers with that parity. Similarly for F(1). So as long as F(0) and F(1) have opposite parity, the two sets partition Z_n. And the mapping defined by F(i)=F(r)+2k for i=2k+r is a bijection from Z_n to Z_n. So indeed, any function satisfying property (1) and with F(0) and F(1) of opposite parity yields a permutation.
Thus the set of all such functions is exactly the set of permutations that are "2-affine": they are determined by two offsets, one for even positions, one for odd positions, where the offset for even positions is F(0) and for odd positions is F(1). And then the mapping is: for i even (i=2k), F(i) = (F(0) + 2k) mod n; for i odd (i=2k+1), F(i) = (F(1) + 2k) mod n.
Now we need to check whether all compositions of rotations and swaps indeed produce exactly these functions, and that we can compute F(0) and F(1) by simulating the sequence of commands on the two starting values 0 and 1 only, because the function is fully determined by its action on 0 and 1 (thanks to property). Since the property is preserved, and we only care about final F for all i, we can just simulate the commands on two representative starting positions (0 and 1). Then for any i, we can compute F(i) = (F(r) + 2k) mod n, where r = i mod 2, k = i // 2.
But is it true that if we apply the sequence of commands to any starting position i, the resulting function satisfies F(i+2) = F(i)+2? We already argued that both generators have this property and composition preserves it. Therefore, if we simulate the commands on two numbers 0 and 1 (maintaining their positions after each command), then for any i we can compute the final position as described.
We must verify that the property holds for the generators and that it is indeed preserved under composition.
Proof for rotation A_x: A_x(i) = (i + x) mod n. Then A_x(i+2) = (i+2+x) = (i+x)+2 = A_x(i)+2 mod n. So property holds.
For swap S: S(i) = i XOR 1. Then S(i+2) = (i+2) XOR 1. Claim: (i+2) XOR 1 = (i XOR 1) + 2 mod n? We need to check if there is any wrap-around issue when i+2 >= n? But since we are in modulo n, we must consider mod. Actually, the property we need is: S(i+2) ≡ S(i) + 2 (mod n). Let's test with n=6, i=4: S(4)=5, S(6 mod6 =0)=1, and 5+2=7≡1 mod6. Works. For i=5: S(5)=4, S(7 mod6=1)=0? Actually i=5: i+2=7 mod6=1, S(1)=? 1 XOR1=0; S(5)=4; 4+2=6≡0 mod6. So holds. In general, because XOR 1 only toggles the least significant bit, and adding 2 doesn't affect LSB except through carry? But there could be carry from bit 1 to higher bits, but does that affect the relation? Let's prove mathematically: Let i be an integer in [0, n-1] (n even). Write i = 2q + r, where r∈{0,1}. Then i XOR 1 = 2q + (r XOR 1). Since XOR 1 flips r. So S(i) = 2q + (1 - r) if r is 0 or 1? Actually if r=0, S(i)=2q+1; if r=1, S(i)=2q. So S(i) = 2q + (1 - r). Now i+2 = 2(q+1) + r, if i+2 < n. But if i+2 >= n, then i+2 mod n = i+2 - n. Since n is even, n = 2m. i = 2q + r, with 0 ≤ 2q+r ≤ 2m-1. i+2 mod n = (2(q+1)+r) mod 2m. If 2(q+1)+r < 2m, then it's 2(q+1)+r. Then S(i+2) = 2(q+1) + (1 - r) = 2q+2 + (1-r). Meanwhile S(i)+2 = (2q + (1-r)) + 2 = 2q+2 + (1-r). So equality holds.
If 2(q+1)+r ≥ 2m, then i+2 mod n = 2(q+1)+r - 2m. Since i is in [0,2m-1], the only possible wrap is when q = m-1 and r=1? Actually maximum i=2(m-1)+1 = 2m-1. Then i+2 = 2m+1, mod n = 1. So i+2 mod n = 1. In this case, i = 2m-1, q = m-1, r=1. Then i+2 mod n = 1 = 20+1, so it can be written as 20+1. But then the formula S(i+2) using the representation? We need to check the relation directly modulo n: Compute S(i) for i=2m-1: i=2(m-1)+1, so S(i)=2(m-1) (since r=1 => S=2q). So S(i) = 2m-2. Then S(i)+2 = 2m ≡ 0 mod 2m. But S(i+2) = S(1) = 1 XOR 1? Actually 1 XOR 1 = 0. So S(i)+2 mod n = 0, S(i+2)=0. So holds. So indeed S(i+2) ≡ S(i)+2 (mod n) always.
Thus property holds for both generators. Since composition of functions preserving this property also preserves it, any composition F will have property (1). Therefore, F is determined by F(0) and F(1).
Thus we can simulate the entire command sequence on just two values: p0 (starting 0) and p1 (starting 1). For each command, we update p0 and p1 according to the operation: For add x: p = (p + x) mod n; For swap: p = p XOR 1. Because these operations are applied identically to all positions, we can apply them to p0 and p1. This simulation costs O(q) time, which is up to 2e6, easily feasible.
After processing all commands, we have final positions for boy 0 (i.e., originally boy 1 if 1-indexed) and boy 1. That is, we know F(0) and F(1). But careful: We simulated on positions starting from initial positions 0 and 1 (which correspond to boy indices 0 and 1). That gives us the final position of those boys. But we need the mapping from final positions to boys, i.e., for each girl position j, which boy is there. We have F(i) = final position of boy i. So we know F(0) and F(1). Using the property, we can compute F(i) for any i: for i = 2k + r, F(i) = (F(r) + 2k) mod n. So we can then construct the answer array ans[j] (0-index) as the boy i such that F(i)=j. That is, ans[F(i)] = i.
So after we have F(0) and F(1), we can generate the array ans of size n by iterating over all i (0..n-1) and setting ans[F(i)] = i. But we need to compute F(i) efficiently for all i. We can do that by using the formula: For each parity r (0 or 1), for each k from 0 to n/2-1, i = 2k+r, and set ans[(F(r) + 2k) mod n] = i.
Thus we can fill ans in O(n). That's perfect.
But we must be careful about modulo n when adding 2k to F(r). Since n is even, we can compute index = (F(r) + 2*k) % n. We can iterate k from 0 to n/2-1 for each parity.
Alternatively, we can compute the inverse mapping directly: Since F is bijective, we can compute the boy at position j by analyzing j's parity and using the known base values. Let's derive inverse: Suppose we have j. Determine its parity. If parity(j) == parity(F(0))? Actually we know parities: parity(F(0)) and parity(F(1)) are opposite. So for a given j, if parity(j) == parity(F(0)), then j belongs to the image of even i; else image of odd i. More precisely, there exists a unique k such that j = (F(r) + 2k) mod n, where r=0 if parity(j)=parity(F(0)), else r=1. Then we can compute k = (j - F(r) mod n) / 2 (since division by 2 modulo n/2 is well-defined because n even and the difference is even). Then i = 2k + r. So we can compute ans[j] = i+1.
But it's easier to just generate F(i) for all i and fill ans. That's O(n) and simple.
Potential issues: The property F(i+2)=F(i)+2 is modulo n. But when we compute F(i) = F(r) + 2k mod n, we must ensure that adding 2k does not cause overflow beyond n and the modulo works. We can compute idx = (F(r) + 2*k) % n. That's fine.
Now we need to confirm that the simulation on p0 and p1 indeed yields F(0) and F(1) correctly after all commands. Since the commands are applied uniformly, yes.
Thus algorithm:
Read n, q. Use 0-index for internal (boys and girls 0..n-1). But output should be 1-indexed: the i-th output is the boy dancing with girl i (1-index). We'll work with 0-index and convert at end.
Initialize pos0 = 0, pos1 = 1.
For each command:
If type 1: read x. Compute x_mod = (x % n + n) % n to ensure positive modulo? But we can just do: x = x (could be negative). Since n is up to 1e6, we can handle addition with modulo by ((p + x) % n + n) % n. But careful: In C++, modulo of negative numbers is negative remainder, so adjust. We'll add x and then take modulo n, and if negative, add n.
For pos0 and pos1: pos = (pos + x) % n, but ensure non-negative.
If type 2: pos0 ^= 1; pos1 ^= 1;
That's it.
After all commands, we have f0 = pos0, f1 = pos1.
Now we need to construct answer array ans of size n (0-index). For each parity r in {0,1}, with base = (r==0? f0 : f1). For k from 0 to n/2 - 1:
i = 2k + r (boy index).
final_position = (base + 2k) % n.
ans[final_position] = i+1 (since boy numbers 1-index). Actually we need ans[girl_position] = boy_number. Girl positions are 0-index (girl index). So ans[final_position] = i+1.
After filling both parities, output ans[0] ... ans[n-1] as space-separated.
Edge Cases:
n=2: Then we have two positions. Our algorithm: initial pos0=0, pos1=1. After commands, compute f0, f1. Then for r=0: base=f0, k from 0 to 0 => i=0, pos = (f0+0)%2, set ans[pos]=1. For r=1: base=f1, k=0 => i=1, pos = (f1+0)%2, set ans[pos]=2. Works.
Command1: type1 x=1: pos0=(0+1)%2=1; pos1=(1+1)%2=0. So after cmd1: pos0=1, pos1=0.
Command2: type2: pos0^=1 => 1^1=0; pos1^=1 =>0^1=1. So after cmd2: pos0=0, pos1=1.
Command3: type1 x=-2: -2 mod2 =0? Actually -2 ≡ 0 mod2, since even. So pos0=(0+(-2))%2: 0-2=-2, mod2 in C++ would be negative, but we can do ((pos + x) % n + n) % n. So effectively 0. pos1 similarly stays 1. So final f0=0, f1=1. Then ans[0]=1, ans[1]=2 => output 1 2. Matches example.
For r=0 (even): base=f0=5. k from 0 to 2 (since n/2=3). i=2k.
k=0: i=0, pos=(5+0)%6=5 -> ans[5]=1
k=1: i=2, pos=(5+2)%6=7%6=1 -> ans[1]=3? Wait i=2 => boy index 2 => boy number 3 (since 1-index). So ans[1]=3.
k=2: i=4, pos=(5+4)%6=9%6=3 -> ans[3]=5 (boy number 5? i=4 => boy5). So ans[3]=5.
For r=1 (odd): base=f1=4. k from 0 to 2. i=2k+1.
k=0: i=1, pos=(4+0)%6=4 -> ans[4]=2
k=1: i=3, pos=(4+2)%6=6%6=0 -> ans[0]=4
k=2: i=5, pos=(4+4)%6=8%6=2 -> ans[2]=6
So ans array: index0:4, index1:3, index2:6, index3:5, index4:2, index5:1. Output in order: 4 3 6 5 2 1. Matches example.
Now we need to ensure that the property holds for any sequence, and that using only two starting points suffices. We argued composition preserves the property. So good.
Complexities: O(q + n). q up to 2e6, n up to 1e6, so total up to 3e6 operations, easily within limits.
Implementation details:
Use fast I/O (scanf/printf or cin with sync off).
Since x can be up to n in absolute value, we need to compute modulo n correctly. But note that adding x to a position may cause overflow beyond int range? x and n up to 1e6, so using int is fine. Use int or long long for safety. Since we may accumulate many adds, but we keep pos in [0, n-1], each step we compute (pos + x) % n. x can be up to 1e6, pos up to 1e6, sum up to 2e6, so int fine. But if x negative, sum may be negative, we adjust.
Better to use: pos = (pos + x) % n; if pos < 0 then pos += n; But in C++, % for negative numbers yields negative remainder (or implementation-defined but usually negative). So we can do: pos = ((pos + x) % n + n) % n; but that double mod might be slower. However, q up to 2e6, it's okay. Alternatively, we can handle separately: add x, then if pos >= n, pos -= n; if pos < 0, pos += n. But since x may be large (up to n), we can do: pos = (pos + x) % n; but to avoid negative, we can first compute tmp = pos + x; then pos = tmp % n; if (pos < 0) pos += n; However, tmp could be negative. In C++ with % the sign of result is sign of dividend, so if tmp negative, pos negative. So we can adjust: pos = ( (pos + x) % n + n ) % n. That's simple and fast enough.
Alternatively, we can keep pos as int and use modulo function: pos = (pos + x) % n; if (pos < 0) pos += n; That works because the absolute value of (pos + x) could be up to ~2e6, but if negative, pos negative, we add n.
But careful: If (pos + x) is negative large (e.g., -1e6), % n yields a negative remainder in [-n+1,0]. Adding n once makes it positive. However, if (pos + x) is -2e6, % n yields some negative, adding n yields value between 0 and n-1? Actually -2e6 % n = -(2e6 % n) maybe, so adding n gives something between 0 and n-1? Not necessarily: Suppose n=1000000, pos=0, x=-1000000, then pos+x = -1000000, -1000000 % 1000000 in C++ is 0? Actually -1000000 / 1000000 = -1 remainder 0? Because -1000000 = -11000000 + 0, so remainder 0. So pos=0, fine. For x=-1000001, -1000001 % 1000000 = -1? Because -1000001 = -21000000 + 999999? Let's compute: -1000001 / 1000000 = -2 (since integer division truncates towards zero? In C++ before C++11, it was implementation-defined; after C++11, it truncates towards zero. So -1000001 / 1000000 = -1? Actually with truncation toward zero, -1000001/1000000 = -1 (since -1.000001 truncates to -1), remainder = -1000001 - (-1)*1000000 = -1. So remainder = -1. So pos = -1; after adding n (1000000) becomes 999999. That's correct modulo. So the adjustment "if (pos < 0) pos += n;" works if we guarantee that after % operation, pos is in (-n, n). Actually remainder's absolute value is less than n, so adding n once makes it non-negative. So we can do:
pos = (pos + x) % n;
if (pos < 0) pos += n;
That works for any implementation where remainder has sign of dividend and |remainder| < |divisor|.
For speed, we can avoid modulo if we know that pos is in [0, n-1] and x in [-n, n], then pos + x is in [-n, 2n-1]. We can just do:
But this only works if x is within [-n, n], which is given: x is between -n and n inclusive. Yes input says -n ≤ x ≤ n. So pos + x is between -n and 2n-1. So we can adjust with at most one add/subtract. That's efficient. For x = n, pos could be up to n-1, sum up to 2n-1; subtract n if >= n. For x = -n, sum could be -n to -1; add n if <0. So we can handle without modulo. Good.
So for type1, we update pos0 and pos1 with that logic.
For type2, just XOR with 1.
One more nuance: In type2, when we XOR with 1, we must ensure that the result remains within [0, n-1]? Since n is even, XOR 1 on a number < n will stay < n because the highest bit above bit0 unchanged, and since n even, the maximum index is n-1 which is odd, so XOR 1 toggles between even and odd but never exceeds n-1. For example n=6 (binary 110), max index 5 (101). XOR 1 gives 100 (4) and 101->100, etc, always <6. For n=8 (1000), max 7 (111), XOR1 gives 110 (6) and 111->110, etc, always <8. So safe.
Now after we have f0 and f1, we need to fill ans array. We'll allocate ans of size n (int). For r=0 and r=1, we can iterate k=0..half-1. But careful about ordering: When we set ans[position] = boy_number, we must ensure we don't overwrite incorrectly; each position will be set exactly once because mapping is bijection.
We can compute position = (base + 2k) % n. Since base may be odd or even, adding 2k will keep parity same. Since we cover all k, we will cover all positions with that parity. So ans will be fully set.
if type == 1:
read x.
// update both positions
update(pos0, x);
update(pos1, x);
else if type == 2:
pos0 ^= 1;
pos1 ^= 1;
After loop:
allocate ans array of size n (maybe vector).
// initialize with zeros.
int half = n / 2;
// process even boys (r=0)
int base0 = pos0;
for (int k = 0; k < half; ++k) {
int boy = 2 * k; // 0-index boy
int pos = (base0 + 2 * k) % n;
ans[pos] = boy + 1; // convert to 1-index
}
// process odd boys (r=1)
int base1 = pos1;
for (int k = 0; k < half; ++k) {
int boy = 2 * k + 1;
int pos = (base1 + 2 * k) % n;
ans[pos] = boy + 1;
}
Output ans[0] to ans[n-1] separated by spaces.
Edge Cases: Must ensure that when n is large, loops fine.
Now we should also consider correctness regarding property: We used that F(i+2)=F(i)+2 holds for all i, which we proved. But we also assumed that F is determined by F(0) and F(1). That holds if the property holds for all i. However, there is a subtlety: The property F(i+2)=F(i)+2 (mod n) holds for all i in Z_n, but does it imply that F(i)=F(r)+2k for i=2k+r? Induction: For r=0 or 1, we can show by induction on k that F(2k+r) = F(r) + 2k mod n. Base k=0 trivial. Inductive step: assume for k, then F(2(k+1)+r) = F((2k+r)+2) = F(2k+r) + 2 = (F(r)+2k) + 2 = F(r)+2(k+1). Mod n, this holds. However, note that the addition is modulo n; but the equality is modulo n, so it's fine. However, careful: The induction uses the property for i = 2k+r. Since property holds for all i, we can apply it. So yes.
Thus our method is valid.
Now we must ensure that our update for pos0 and pos1 respects the property. Since we apply the same operations to both, the resulting functions are consistent.
Let's double-check with a random sequence to be safe.
We also need to consider that the initial pos0=0 and pos1=1 represent boy 0 and boy 1. After commands, we get final positions for these boys. But what about boy with index i that is not 0 or 1? Their final position can be derived using the property. However, we must be sure that the property holds for all i when we derive from the two base values. Since the property is preserved by each operation for any starting i, it must hold for the overall function. And we computed F(0) and F(1) by simulation, so we can compute F(i) for any i using the formula.
But is there any chance that the property might not hold for i such that i+2 wraps around? But we already considered modulo, it's fine.
Thus solution is solid.
Now we need to implement update function for pos with x between -n and n. We'll write inline:
void apply_shift(int &p, int x, int n) {
p += x;
if (p >= n) p -= n;
else if (p < 0) p += n;
}
But careful: If x = n, p becomes p+n. If p originally 0, p becomes n, then we subtract n =>0. That's correct. If x = -n, p becomes p-n. If p=0, p=-n, then add n =>0. So works.
But what about p=5, x= -7, with n=6? x is between -n and n, so x=-6 to 6. So p+x between -6 and 11. If p=5, x=-7 is invalid (since -7 < -6). So okay.
Now note that x can be 0 as well? The problem says -n ≤ x ≤ n, and they said "0 ≤ x ≤ n means all boys moves x girls in clockwise direction, while - x means ...". So x could be 0, which does nothing.
Now about reading input: The commands lines with type 2 have only one integer (the type). With type 1, there are two integers: type and x. We'll read accordingly.
Implementation details:
Use scanf for speed: while(q--) { int t; scanf("%d", &t); if(t==1) { int x; scanf("%d", &x); ... } else { ... } }
Output: can use printf for each ans[i] with space, or build string.
Now we must be careful about large I/O; 1e6 numbers output, using printf in loop is fine.
Now let's test with more cases.
Test n=4, random sequence: commands: 1 1, 2, 1 3, 2.
1 3 (rotate right by 3): Starting from [1,4,3,2], rotate right by 3: new A[i] = old A[i-3]. Compute: i0: old A[-3]=A[1]=4? Actually mod: A[1]=4; i1: A[-2]=A[2]=3; i2: A[-1]=A[3]=2; i3: A[0]=1 => [4,3,2,1].
2 (swap pairs): swap (0,1) => [3,4,2,1]? Actually (0,1): 4 and 3 swap -> [3,4,2,1]; (2,3): 2 and 1 swap -> [3,4,1,2]. So final: [3,4,1,2]. Matches our ans. Good.
Thus method works.
Now we must be cautious about 1-index vs 0-index. In our simulation, we used boy indices 0..n-1, but the output expects boy numbers 1..n. So we add 1 when assigning ans.
Now check n large, everything fine.
Now we should also consider memory: ans array of size n up to 1e6 ints => ~4 MB, fine.
Now, let's think about potential pitfalls: The property F(i+2)=F(i)+2 holds for all i. But is it always true that we can compute F(i) as F(r)+2k mod n? Yes, as argued by induction. However, we need to be careful that the induction uses the property for i values that may wrap around modulo n; but the property is modulo equality, so it's fine. In programming, when we compute (base + 2*k) % n, we get the correct position. The induction step uses that F(i+2) = F(i) + 2 (mod n). So the formula yields a value modulo n.
One more nuance: The mapping we compute via the formula must be consistent with the simulation of two points. Since we only simulated two points, we rely on the property to extend to all points. This is valid because the property is inherent to the transformation, independent of which points we track.
But we should ensure that the property holds for all i when we apply the operations as defined. We proved it for generators and closure under composition. So it's mathematically sound.
Now we might ask: Could there be any overflow in the calculation of (base + 2k) before modulo? base up to n-1, 2k up to n-2, sum up to about 2n, which fits in int (since n<=1e6, sum <= 2e6). So we can compute as (base + 2*k) % n safely. Or we could compute position incrementally: start pos = base; for k from 0 to half-1: assign ans[pos] = boy; then pos = (pos+2) % n; This avoids multiplication each iteration. That might be simpler and more efficient. Let's adopt that.
Implementation for filling ans:
For r=0:
int cur = pos0;
for (int k = 0; k < half; ++k) {
int boy = 2 * k; // 0-index
ans[cur] = boy + 1;
cur = (cur + 2) % n;
}
Similarly for r=1:
cur = pos1;
for (int k = 0; k < half; ++k) {
int boy = 2 * k + 1;
ans[cur] = boy + 1;
cur = (cur + 2) % n;
}
This works because stepping by 2 each time cycles through all positions of same parity.
Now we must ensure that we start cur at base and then adding 2 repeatedly covers all positions of that parity exactly once. Since n is even, the cycle length of adding 2 modulo n is n/2. So yes.
Thus we can fill ans in O(n).
Now we need to handle the possibility that after commands, pos0 and pos1 might have same parity? Actually from the property, they must have opposite parity because mapping is bijection. Let's check: After any sequence, since F is bijection, F(0) and F(1) must have opposite parity. Our simulation should ensure that. But if there is a bug, it might produce same parity, but then our ans assignment would assign two boys to same parity positions possibly causing collisions. However, we trust the math.
Now about reading q up to 2e6: use fast input. In C++ with scanf is fine.
Now we should also consider the case where x is given as -0? Not possible because -0 is 0. Input spec says -n ≤ x ≤ n, so okay.
Now finalize solution.
Let's write pseudocode:
int main() {
int n, q;
scanf("%d %d", &n, &q);
int pos0 = 0, pos1 = 1;
while (q--) {
int t;
scanf("%d", &t);
if (t == 1) {
int x;
scanf("%d", &x);
// apply to pos0 and pos1
// adjust x: we can keep as is; update with bounded addition.
pos0 += x;
if (pos0 >= n) pos0 -= n;
else if (pos0 < 0) pos0 += n;
pos1 += x;
if (pos1 >= n) pos1 -= n;
else if (pos1 < 0) pos1 += n;
} else {
pos0 ^= 1;
pos1 ^= 1;
}
}
vector ans(n);
int half = n / 2;
// even-indexed boys
int cur = pos0;
for (int k = 0; k < half; ++k) {
int boy = 2 * k;
ans[cur] = boy + 1;
cur = (cur + 2) % n;
}
// odd-indexed boys
cur = pos1;
for (int k = 0; k < half; ++k) {
int boy = 2 * k + 1;
ans[cur] = boy + 1;
cur = (cur + 2) % n;
}
// output
for (int i = 0; i < n; ++i) {
printf("%d ", ans[i]);
}
return 0;
}
Now test with examples.
Example1: n=6, we did and matches.
Now test with random small n and random command sequences by comparing with brute force simulation to ensure correctness. We'll do mental but trust.
Edge Cases:
n=2, many commands, should be fine.
x = -n: adding -n is equivalent to subtract n, but since pos in [0,n-1], pos-n will be negative, then add n => pos unchanged. So effectively, rotating by n full circle does nothing, which is correct.
x = n: same.
x = 0: nothing.
Commands with only type2 repeated: swapping twice returns to original. Our simulation: pos0 toggles twice returns to original; pos1 same. So final mapping returns to identity.
Mix: Should be fine.
Now we need to ensure that the property holds for all i, but our derivation used that the operation S is i XOR 1. But is it always true that swap command in terms of boy's position is i XOR 1? Let's double-check with the problem description: "Boys dancing with even-indexed girls swap positions with boys who are dancing with odd-indexed girls. That is the one who was dancing with the girl 1 swaps with the one who was dancing with the girl number 2, while the one who was dancing with girl number 3 swaps with the one who was dancing with the girl number 4 and so on."
If we think from the perspective of a boy, his current position is some girl index. If he is at an even-indexed girl (say girl 2,4,...), he swaps with the boy at the adjacent odd-indexed girl (girl 1,3,...). Which odd-indexed? It says specifically: the one dancing with girl 1 swaps with the one dancing with girl 2; the one dancing with girl 3 swaps with girl 4, etc. So it's a pairing between (1,2), (3,4), (5,6), ... So for a boy at position i (1-indexed), the new position after swap is: if i is odd, he goes to i+1; if i is even, he goes to i-1. In 0-index, that is: if i (0-index) is even (girl1->0), he goes to i+1; if i is odd, he goes to i-1. That's exactly i XOR 1. Yes.
Thus the model is correct.
Now we should also confirm that the rotation direction is consistent. In our modeling, a clockwise move means adding to position. In example, they gave command "1 2" (clockwise) and after that the array became rotated right by 2. That matches our update: position = (pos + 2) mod n. So clockwise corresponds to increasing index modulo n.
Thus the solution is correct.
Now we can produce final answer.
But wait: There is a subtlety about 1-indexing: In our simulation we used 0-index for positions and boy indices. For rotation, when we add x, is it always correct for any x? Example: n=6, initial positions: boy1 at girl1 (0-index 0). After +2 clockwise, boy1 moves to girl3 (0-index 2). Our update: pos0=0+2=2 -> correct. After -1 counterclockwise? If x = -1, that means move 1 position counterclockwise. In the circle, moving counterclockwise means decreasing index. So new position = (pos - 1) mod n. Our update with x negative does that. So correct.
Now we should confirm that our handling of negative x with the simple if-else adjustment works. For x = -1, pos0 += -1, if pos0 becomes -1, we add n => n-1. That's correct for counterclockwise move by 1.
Now about the swap: XOR 1 on 0-index works as described.
Thus solution is ready.
Now we need to write final answer. Also include comments explaining the property.
Potential pitfalls: When n is large, loops are fine. Use int for n up to 1e6, safe.
Now let's test with n=1e6, q=2e6, all commands type2 (swap). That would toggle pos0 and pos1 each time. So q mod 2 determines final. Works.
Now test with random commands to ensure that ans array is filled correctly. Possibly we need to ensure that the two loops (even and odd) don't interfere; they set different positions because parity of cur is different? Actually, initially cur = pos0 and pos1 have opposite parity, so they set positions of opposite parity. The loops step by 2, so they stay within their parity class. Thus they set disjoint positions. Good.
Now, we should also consider if there is any possibility that pos0 and pos1 are not correctly computed due to overflow in adding x? But we limited to x between -n and n, and pos in [0,n-1], so sum is within [-n, 2n-1], which fits in int. So our adjustment with one if is safe.
Edge case: x = n, pos0 = n-1, then pos0+n = 2n-1, if we do pos0 += x; then check if pos0 >= n? 2n-1 >= n, so we subtract n => n-1. That's correct (full rotation). For x = -n, pos0 = 0, then pos0 - n = -n, <0, add n => 0. Good. For pos0 = 1, x = -n, sum = 1 - n = -(n-1), <0, add n => 1. So correct.
Thus code is robust.
Now, let's also consider if n=0? No, n≥2.
Now, we should also consider that q lines may be large, so using scanf is good. Use while (q--) loop.
Now we produce final answer.
But before final, we should think: Did we miss any detail? The problem statement: "Value x and some direction are announced, and all boys move x positions in the corresponding direction." In the input, they give x, and negative indicates counter-clockwise. So indeed we treat x as signed.
Now ensure that the answer format: Output n integers, the i-th of them should be equal to the index of boy the i-th girl is dancing with after performing all q moves. So we output ans[0] for girl1, ans[1] for girl2, etc.
Now we should test with provided examples.
Example 1: already matched.
Example 2: matched.
Example 3: matched.
Now we can also test with n=2, commands: 2 only. Then pos0=0^1=1, pos1=1^1=0. final f0=1, f1=0. Then ans: even: base=1, k=0: i=0 pos=1 -> ans[1]=1. odd: base=0, k=0: i=1 pos=0 -> ans[0]=2. So ans = [2,1] => output 2 1. Let's verify manually: initial [1,2]; swap (type2): boys at girl1 and girl2 swap => [2,1]. Yes.
// positions of boy 0 (even) and boy 1 (odd) after the performed commands
int pos0 = 0, pos1 = 1;
while (q--) {
int t;
scanf("%d", &t);
if (t == 1) {
int x;
scanf("%d", &x);
// add x modulo n, using the fact that -n <= x <= n
pos0 += x;
if (pos0 >= n) pos0 -= n;
else if (pos0 < 0) pos0 += n;
pos1 += x;
if (pos1 >= n) pos1 -= n;
else if (pos1 < 0) pos1 += n;
} else { // t == 2
pos0 ^= 1;
pos1 ^= 1;
}
}
vector<int> ans(n);
int half = n / 2;
// even-indexed boys: i = 2*k
int cur = pos0;
for (int k = 0; k < half; ++k) {
int boy = 2 * k; // 0‑based boy index
ans[cur] = boy + 1; // convert to 1‑based
cur = (cur + 2) % n;
}
// odd-indexed boys: i = 2*k+1
cur = pos1;
for (int k = 0; k < half; ++k) {
int boy = 2 * k + 1; // 0‑based boy index
ans[cur] = boy + 1;
cur = (cur + 2) % n;
}
for (int i = 0; i < n; ++i) {
printf("%d%c", ans[i], i == n - 1 ? '\n' : ' ');
}
return 0;