Write a program which reads a sequence of integers $A = \\{a0, a1, ..., a_{n-1}\\}$ and…
- User
Write a program which reads a sequence of integers and swap specified elements by a list of the following operation:
Assistant
Write a program which reads a sequence of integers and swap specified elements by a list of the following operation:
Constraints
The input is given in the following format.
:
In the first line, (the number of elements in ) is given. In the second line, (each element in ) are given. In the third line, the number of queries is given and each query is given by three integers in the following lines.
Output
Print all elements of in a line after performing the given operations. Put a single space character between adjacency elements and a newline at the end of the last element.
Example
Input
11 1 2 3 4 5 6 7 8 9 10 11 1 1 4 7
Output
1 8 9 10 5 6 7 2 3 4 11
We are to implement a program that reads an array A of size n, reads q swapRange operations, and performs them in order. However, there is an important constraint: "Given swap ranges do not overlap each other." That simplifies things, but we must ensure we handle operations correctly.
The swapRange(b, e, t): For each integer k (0 <= k < e - b), swap element (b + k) and element (t + k). This is essentially swapping two subarrays of equal length (e - b). The subarrays are A[b...e-1] and A[t...t+(e-b)-1]. The operation says: "swap element (b + k) and element (t + k)" for each k. So this is swapping pairwise? Wait, "swap element (b + k) and element (t + k)" means we take each pair and swap them. However, careful: If we do this for each k sequentially, swapping each pair, we would effectively exchange the two subarrays? Actually, if we swap each pair individually, and the two ranges are distinct and non-overlapping (though they may be overlapping? The problem states "swap ranges do not overlap each other" across queries? Possibly meaning within a single operation the two ranges might overlap? The constraints say: 0 ≤ t_i < t_i + (e_i - b_i) ≤ n. But there is no explicit condition that b_i...e_i-1 and t_i...t_i+(e_i-b_i)-1 are disjoint. However, typical "swap ranges" operation could have overlapping ranges; if they overlap, swapping pairwise could have a different effect. For example, if ranges partially overlap, swapping element by element might produce a rotation? Actually, if ranges are the same, swapping each pair does nothing (swap element with itself). If they overlap partially, consider swapping A[0..2] with A[1..3]. Then swapping (0,1), then (1,2), then (2,3) would produce some effect. But it's not simply exchanging the blocks; the result could be like shifting? We need to examine what exactly is meant.
The operation description: "For each integer k (0 ≤ k < (e - b)), swap element (b + k) and element (t + k)." So it does swap pairs (b + k, t + k) for each k. If the ranges overlap, then for some k, b+k and t+k may refer to the same index? They could be equal if the ranges intersect at indices. But if they are equal, swapping does nothing. However, if they overlap but not identical indices, say b=0, e=3, t=1, then pairs: k=0: (0,1); k=1: (1,2); k=2: (2,3). Notice index 1 appears in two swaps: first swapped with index0, then later swapped with index2. That would have a net effect like rotating? Indeed, if we swap pairwise in order, the result might be different from simply swapping the two blocks as a whole (i.e., exchanging the contents). But note: The problem states "Given swap ranges do not overlap each other." This likely means that for each query, the two ranges do not overlap? Or across different queries they do not overlap? It says "Given swap ranges do not overlap each other." Could be ambiguous. Usually, in such problems, they guarantee that the two ranges in each operation do not overlap, because then the swap operation is just exchanging two disjoint blocks. But it's safer to read the constraints: "0 ≤ b_i < e_i ≤ n" and "0 ≤ t_i < t_i + (e_i - b_i) ≤ n". That ensures both ranges are within array. But there is no explicit condition that [b_i, e_i) and [t_i, t_i + (e_i - b_i)) are disjoint. However, they say "Given swap ranges do not overlap each other." Possibly they refer to the ranges across different operations? Because if operations are performed sequentially, overlapping ranges across operations could cause complexity but not impossible. But they explicitly say swap ranges do not overlap each other, meaning maybe each operation involves two ranges that are either the same or disjoint? Actually, "swap ranges" could refer to the intervals being swapped? Alternatively, the phrase might be: "Given swap ranges do not overlap each other" meaning that for the list of operations, the intervals (the two ranges in each operation?) do not overlap across operations? That would simplify that each element is affected by at most one operation? Hmm.
Let's parse the problem statement carefully: "Write a program which reads a sequence of integers and swap specified elements by a list of the following operation:
Constraints
The last line: "Given swap ranges do not overlap each other." I think it means that for each operation, the two ranges (the one from b to e-1 and the one from t to t+len-1) do not overlap each other. That is, they are disjoint. This is typical for a "swap ranges" operation: swapping two disjoint subarrays. Because if they overlapped, the pairwise swapping would be more complicated and might not be what they intend. Additionally, they might also guarantee that across operations, the ranges (maybe the swapped positions) don't overlap with those of other operations? But the phrase "each other" might refer to the ranges within a single operation? But it says "Given swap ranges" meaning the list of swapRange operations? Actually, it's ambiguous.
Let's look at the sample: n=11, A=1..11, one operation: b=1, e=4, t=7. So len = e-b = 3. Range1: indices 1,2,3 (values 2,3,4). Range2: indices 7,8,9 (values 8,9,10). They are disjoint. Output: 1 8 9 10 5 6 7 2 3 4 11. That's exactly swapping the two subarrays as whole blocks. But note: If we perform pairwise swaps for each k sequentially, we would get the same result because the ranges are disjoint and swapping each pair individually is equivalent to exchanging them. However, if the ranges overlapped, the result might differ; but the sample doesn't test that. So for disjoint ranges, simple element-wise swapping yields the correct exchange.
Given constraints up to 1000 n and q up to 1000, we can just simulate each operation by iterating k and swapping A[b+k] and A[t+k]. That's O(q * len) total, which in worst case could be up to q * n = 1e6, which is fine.
But is there any trick? The constraint "Given swap ranges do not overlap each other" might be to ensure that no element is swapped multiple times in a way that could cause unintended overwriting? Actually, if ranges overlap, the pairwise swap algorithm (swapping in order) might produce a result that is not simply the exchange of the two blocks. But if they guarantee no overlap, we don't have to worry about that case.
However, what about overlapping across different operations? If two operations involve overlapping ranges, then order matters, but that's still fine to simulate straightforwardly. The phrase might be that "swap ranges" refer to the two ranges per operation, and they don't overlap each other meaning the two ranges are disjoint? Alternatively, it might be that the set of indices covered by all operations (like the union of all swapped intervals) are non-overlapping across operations. For instance, maybe they guarantee that each element is involved in at most one operation? That would also be helpful for certain optimizations but not needed.
Let's examine typical problems from AIZU or other judges: The problem might be from the "Range" category. There's a known problem "Swap Range" that appears in JOI? I'm not sure.
But given simplicity and the sample, I'm confident that we can implement simulation directly.
We must handle input format: first line n, second line the n integers, third line q, then q lines each with b e t. Note that they use b_i e_i t_i; but in sample, operation is "1 4 7". So b=1, e=4, t=7. We must interpret correctly: b is start index, e is end index (exclusive), t is start index of other range. So length L = e - b.
Edge cases: n up to 1000, int values within 32-bit signed? a_i up to 1e9, so fits in 32-bit signed, but we can use long long just to be safe but not needed.
We need to read input, process operations in order, and output final array separated by spaces.
That's all.
But before finalizing, consider the phrase "Given swap ranges do not overlap each other." If it means the two ranges in each operation are disjoint, then the algorithm is correct. If it means across operations, we still can simulate as described. So no extra handling needed.
Therefore, solution: Read n, read list a[0..n-1], read q, then for each query read b, e, t. Compute len = e - b. Then for k in range(len): swap a[b+k] and a[t+k]. After processing all queries, print a.
Complexities: O(n + q * n) worst case but with n=1000 and q=1000, len sum could be up to 1e6, which is fine.
One nuance: The constraints say t_i + (e_i - b_i) <= n, so the second range is within array. And b_i < e_i. So len>=1.
Implementation in Python (or C++). Since the problem statement doesn't specify language, we can produce a solution in Python, which is acceptable for many online judges. However, typical competitive programming solutions are expected in languages like C, C++, Java, etc. But we can write in Python as it's simple.
Let's craft answer: We'll provide explanation and then the code.
But the instruction: "Write a program which reads ...". So we need to output the program code.
Given the conversation, the user asks: "Write a program ...". So the assistant should output the program. Possibly the assistant should output a full program in a language (like C++ or Python). Since the problem is from an online judge, they might accept either. We can choose Python for simplicity.
But careful: The sample Input/Output show numbers. The program must handle up to 1000 integers, so Python is fine.
I'll produce Python code:
def main():
import sys
data = sys.stdin.read().split()
if not data:
return
it = iter(data)
n = int(next(it))
a = [int(next(it)) for _ in range(n)]
q = int(next(it))
for _ in range(q):
b = int(next(it))
e = int(next(it))
t = int(next(it))
length = e - b
for k in range(length):
a[b + k], a[t + k] = a[t + k], a[b + k]
print(' '.join(map(str, a)))
if __name__ == "__main__":
main()
That's it.
But we should also consider that indices are 0-based? The problem says a0, a1, ..., a_{n-1}, and operations refer to element indices starting at 0. So yes, 0-based.
Now, what about the non-overlapping guarantee? Not used. The code works for overlapping as well, but the result for overlapping might not be what they intend if they guarantee non-overlap, but that's fine.
Thus final answer.
But the assistant might also explain the solution. However, the prompt says "Write a program". So we should output the program. Possibly include brief explanation? The system instructions: "You are an AI assistant. Provide a well-reasoned, step-by-step solution." So we need to provide both reasoning and code. The final answer should contain the code.
Thus I'll produce a response that includes explanation and then the Python code.
Given typical format, I'll output:
# solution code
Let's provide final. We'll simulate each swapRange operation directly. For each query, we determine the length L = e - b, then swap elements at positions b + k and t + k for k = 0 .. L-1. Since n and q are at most 1000, the total number of swaps is at most 1e6, which is easily handled.
Here is a Python implementation:
import sys
def main():
data = sys.stdin.read().split()
if not data:
return
it = iter(data)
n = int(next(it))
a = [int(next(it)) for _ in range(n)]
q = int(next(it))
for _ in range(q):
b = int(next(it))
e = int(next(it))
t = int(next(it))
length = e - b
# pairwise swap of the two ranges
for k in range(length):
a[b + k], a[t + k] = a[t + k], a[b + k]
# output the final array
print(' '.join(map(str, a)))
if __name__ == "__main__":
main()