Problem:

Swap

 

Write a program which reads a sequence of integers $A = \{a_0, a_1, ..., a_{n-1}\}$ and swap specified elements by a list of the following operation:

  
swapRange($b, e, t$): For each integer $k$ ($0 \leq k < (e - b)$, swap element $(b + k)$ and element $(t + k)$.

Input

  The input is given in the following format.

$n$
$a_0 \; a_1 \; ...,\; a_{n-1}$
$q$
$b_1 \; e_1 \; t_1$
$b_2 \; e_2 \; t_2$
:
$b_{q} \; e_{q} \; t_{q}$

  In the first line, $n$ (the number of elements in $A$) is given. In the second line, $a_i$ (each element in $A$) are given. In the third line, the number of queries $q$ is given and each query is given by three integers $b_i \; e_i \; t_i$ in the following $q$ lines.

Output

    Print all elements of $A$ 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.

Constraints

  
$1 \leq n \leq 1,000$

  
$-1,000,000,000 \leq a_i \leq 1,000,000,000$

  
$1 \leq q \leq 1,000$

  
$0 \leq b_i < e_i \leq n$

  
$0 \leq t_i < t_i + (e_i - b_i) \leq n$

  
Given swap ranges do not overlap each other

Sample Input 1

11
1 2 3 4 5 6 7 8 9 10 11
1
1 4 7

Sample Output 1

1 8 9 10 5 6 7 2 3 4 11



Sample input:
11
1 2 3 4 5 6 7 8 9 10 11
1
1 4 7


Sample output:
1 8 9 10 5 6 7 2 3 4 11

Submission status: Wrong Answer

Solution code:
# coding=utf-8


def partial_swap(origin, begin, end, begin2):
    list1 = origin[:begin]
    list2 = origin[begin:end]
    list3 = origin[end:begin2]
    list4 = origin[begin2:begin2+end-begin]
    list5 = origin[begin2+end-begin:]

    return list1 + list4 + list3 + list2 + list5


if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())

    for i in range(Q):
        b, e, t = map(int, input().split())
        A = partial_swap(A, b, e, t)

    print(' '.join(map(str, A)))

Submission status: Wrong Answer

Solution code:
# AOJ ITP2_4_A: Reverse
# Python3 2018.6.24 bal4u

n = int(input())
a = list(map(int, input().split()))
q = int(input())
for i in range(q):
	b, e, t = map(int, input().split())
	a = a[:b] + a[t:t+e-b] + a[e:t] + a[b:e] + a[t+e-b:]
print(*a)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split(' ')))
q = int(input())
for i in range(q):
    b, e, t = list(map(int, input().split(' ')))
    for k in range(e-b):
        a[b+k], a[t+k] = a[t+k], a[b+k]
print(' '.join(list(map(str, a))))

Submission status: Accepted

Solution code:
# coding=utf-8


def partial_swap(origin, begin, end, begin2):
    for i in range(end-begin):
        origin[begin+i], origin[begin2+i] = origin[begin2+i], origin[begin+i]

    return origin


if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())

    for i in range(Q):
        b, e, t = map(int, input().split())
        A = partial_swap(A, b, e, t)

    print(' '.join(map(str, A)))

Submission status: Accepted

Solution code:

def swap_range(li, b, e, t):
    """Swap element (b + k) and element (t + k) (0 <= k < e-b).

    >>> li = ['1', '2', '3', '4', '5', '6', '7', '8']
    >>> swap_range(li, 1, 3, 5)
    >>> " ".join(li)
    '1 6 7 4 5 2 3 8'
    >>> swap_range(li, 3, 5, 4)
    >>> " ".join(li)
    '1 6 7 5 2 4 3 8'
    """
    for k in range(e-b):
        li[b+k], li[t+k] = li[t+k], li[b+k]


def run():
    n = int(input())
    li = input().split()
    assert(n == len(li))

    q = int(input())
    for _ in range(q):
        b, e, t = [int(x) for x in input().split()]
        swap_range(li, b, e, t)

    print(" ".join(li))


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
# AOJ ITP2_4_C: Swap
# Python3 2018.6.24 bal4u

n = int(input())
a = list(map(int, input().split()))
q = int(input())
for i in range(q):
	b, e, t = map(int, input().split())
	s = t+e-b
	if t > b:
		a = a[:b] + a[t:s] + a[e:t] + a[b:e] + a[s:]
	else:
		a = a[:t] + a[b:e] + a[s:b] + a[t:s] + a[e:]
print(*a)

Submission status: Accepted

Solution code:
n=int(input())
s=list(map(int,input().split()))
m=int(input())
for i in range(m):
    b,e,t=map(int,input().split())
    for k in range(e-b):
        s[b+k],s[t+k]=s[t+k],s[b+k]
print(*s)

Submission status: Accepted

Solution code:
n = input()
A = list(input().split())
for i in range(int(input())):
    b, e, t = map(int, input().split())
    for k in range(e - b):
        A[b+k], A[t+k] = A[t+k], A[b+k]

print(*A, sep=' ')

Submission status: Accepted

Solution code:
def resolve():
    from collections import deque
    N = int(input())
    A = [int(i) for i in input().split()]
    Q = int(input())
    ans = [i for i in range(N)]
    for _ in range(Q):
        b, e, t = [int(i) for i in input().split()]
        tmp = deque(ans[t:t + (e - b)])
        for i in range(b, e):
            ans[i + t - b] = ans[i]
            ans[i] = tmp[i - b]
    prt = [0] * N
    for i in range(N):
        prt[i] = A[ans[i]]
    print(*prt)


resolve()

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())

for i in range(q):
    queryi = list(map(int, input().split()))
    b = queryi[0]
    e = queryi[1]
    t = queryi[2]

    for i in range(e-b):
        temp = A[b+i]
        A[b+i] = A[t+i]
        A[t+i] = temp

print(*A)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
for j in range(q):
  b, e, t = map(int,input().split())
  for k in range(e - b):
      aa = a[b+k]
      a[b+k] = a[t+k]
      a[t+k] = aa
  
print(a[0],end="")
for aa in a[1:]:
  print(" {}".format(aa),end="")
print()

Submission status: Accepted

Solution code:

from sys import stdin

n = int(stdin.readline())
l = list(stdin.readline().split())
q = int(stdin.readline())

for i in range(q):
    inp_l = list(map(int,stdin.readline().split()))
    displacement = inp_l[1] - inp_l[0]
    for i in range(displacement):
        l[inp_l[0] + i],l[inp_l[2] + i] =l[inp_l[2] + i],l[inp_l[0] + i]

print(' '.join(l))

Submission status: Accepted

Solution code:
from copy import deepcopy
n = int(input())
a = list(map(int,input().split()))

for i in range(int(input())):
    b,e,t = map(int,input().split())
    tmp = deepcopy(a)
    for k in range(e-b):
        a[t+k] = tmp[b+k]
        a[b+k] = tmp[t+k]

print(*a)

Submission status: Accepted

Solution code:
input()
*a,=map(int,input().split())
for i in range(int(input())):
    l,r,t=map(int,input().split())
    a[l:r],a[t:t+r-l]=a[t:t+r-l],a[l:r]
print(*a)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
for i in range(q):
    b,e,t = map(int,input().split())
    for k in range(e-b):
        a[b+k],a[t+k] = a[t+k],a[b+k]
print(*a)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A = list(map(int,input().split()))
    m = int(input())
    for _ in range(m):
        a,b,c = map(int,input().split())
        A[a:b],A[c:c+(b-a)] = A[c:c+(b-a)],A[a:b]
    print (' '.join(map(str,A)))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
for i in range(q):
    b, e, t = map(int, input().split())
    if b < t:
        a = a[:b] + a[t:t+e-b] + a[e:t] + a[b:e] + a[t+e-b:]
    else:
        a = a[:t] + a[b:e] + a[t+e-b:b] + a[t:t+e-b] + a[e:]
print(*a)

Submission status: Accepted

Solution code:
n=input()
l=list(map(int,input().split()))
n=int(input())
for i in range(n):
    b,e,t=map(int,input().split())
    l1=l[b:e]
    l2=l[t:t+e-b]
    l[b:e]=l2
    l[t:t+e-b]=l1
    
l3=map(str,l)
print(' '.join(map(str,l3)))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))

Q = int(input())
for _ in range(Q):
    b,e,t = map(int,input().split())
    for k in range(e-b):
        A[b+k],A[t+k] = A[t+k],A[b+k]

print(*A)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())

for i in range(q):
    b, e, t = list(map(int, input().split()))
    
    for j in range(e - b):
        A[b + j], A[t + j] = A[t + j], A[b + j]

print(*A)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
for _ in range(q):
	b,e,t = map(int, input().split())
	x = a[t:t+e-b]
	a[t:t+e-b] = a[b:e]
	a[b:e] = x
print(" ".join(map(str,a)))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    seq = [int(a) for a in input().split()]
    q = int(input())

    for _ in range(q):
        b, e, t = map(int, input().split())
        for k in range(e-b):
            tmp = seq[t+k]
            seq[t+k] = seq[b+k]
            seq[b+k] = tmp
    print(*seq)

main()

Submission status: Accepted

Solution code:
n = int(input())
L = [int(x) for x in input().split()]
n = int(input())
for _ in range(n):
    b, e, t = [int(x) for x in input().split()]
    L[b:e], L[t:t+e-b] = L[t:t+e-b], L[b:e]
print(*L)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
for _ in range(q):
    b, e, t = map(int,input().split())
    # tmp = a[:]
    for k in range(e-b):
        # a[b+k] = tmp[t+k]
        # a[t+k] = tmp[b+k]
        a[b+k], a[t+k] = a[t+k], a[b+k]

print(*a)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
q=int(input())

for i in range(q):
    b,e,t=map(int, input().split())
    if t>b:
        a=a[:b]+a[t:t+e-b]+a[e:t]+a[b:e]+a[t+e-b:]
    else:
        a=a[:t]+a[b:e]+a[t+e-b:b]+a[t:t+e-b]+a[e:]
print(*a)

Submission status: Accepted

Solution code:
def solve():
    from sys import stdin
    f_i = stdin
    
    n = f_i.readline()
    A = f_i.readline().split()
    q = int(f_i.readline())
    for i in range(q):
        b, e, t = map(int, f_i.readline().split())
        w = e - b
        if b < t:
            A = A[:b] + A[t:t+w] + A[b+w:t] + A[b:b+w] + A[t+w:]
        else:
            A = A[:t] + A[b:b+w] + A[t+w:b] + A[t:t+w] + A[b+w:]
    print(' '.join(A))

solve()

Submission status: Accepted

Solution code:
n=int(input())
a = list(map(int, input().split( )))
q=int(input())
for _ in range(q):
    b,e,t = map(int, input().split( ))
    for k in range(e-b):
        tmp = a[b+k]
        a[b+k] = a[t+k]
        a[t+k] = tmp
print(*a)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    num_a = int(input())
    a = list(map(lambda x: int(x), input().split()))
    num_query = int(input())

    for _ in range(num_query):
        begin, end, t = map(lambda x: int(x), input().split())
        for i, idx in enumerate(range(begin, end)):
            a[idx], a[t + i] = a[t + i], a[idx]

    print(" ".join([str(elem) for elem in a]))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
for i in range(q):
	b, e, t = map(int, input().split())
	s = t+e-b
	if t > b:
		a = a[:b] + a[t:s] + a[e:t] + a[b:e] + a[s:]
	else:
		a = a[:t] + a[b:e] + a[s:b] + a[t:s] + a[e:]
print(*a)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Basic Modifications - Swap
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_4_C&lang=jp

"""
_ = input()
A = list(input().split())
for _ in range(int(input())):
    b, e, t = map(int, input().split())
    for k in range(e-b):
        A[b+k], A[t+k] = A[t+k], A[b+k]
print(*A, sep=' ')

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
for _ in range(q):
    b, e, t = map(int, input().split())
    for k in range(e-b):
        a[t+k],a[b+k] = a[b+k],a[t+k]
             
print(*a)

Submission status: Accepted

Solution code:
def swaping(list2,q,b,e,t):

	k = e - b

	for i in range(k):
		list2[b+i],list2[t+i] = list2[t+i],list2[b+i]


if __name__ == '__main__':

	num = int(input())
	list2 = input().split()
	q = int(input())
	
	for i in range(q):
		b,e,t = map(int,input().split())

		swaping(list2,q,b,e,t)

	print(" ".join(list2))

Submission status: Accepted

Solution code:
n = int(input())

A = list(map(int, input().split()))

q = int(input())


for _ in range(q):
    
    b, e, t = map(int, input().split())

    for k in range(e - b):

        A[b + k], A[t + k] = A[t + k], A[b + k]


# OUTPUT
print(*A)

Submission status: Accepted

Solution code:
n = int(input())
box = list(map(int, input().split()))
q = int(input())
for _ in range(q):
    b, e, t = map(int, input().split())
    for k in range(e-b):
        box[t+k],box[b+k] = box[b+k],box[t+k]
            
print(*box)

Submission status: Accepted

Solution code:
input()
nums = list(map(int, input().split(' ')))

n = int(input())
for _ in range(n):
    f, m, l = list(map(int, input().split(' ')))
    m = m-f
    if f > l:
        f, l = l, f
    sb_1 = list(nums[f:f+m])
    sb_2 = list(nums[l:l+m])

    nums = nums[:f] + sb_2 + nums[f+m:l] + sb_1 + nums[l+m:]

print(' '.join(map(str, nums)))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
for i in range(q):
    b, e, t = map(int, input().split())
    s = t+e-b
    if t > b:
        a = a[:b] + a[t:s] + a[e:t] + a[b:e] + a[s:]
    else:
        a = a[:t] + a[b:e] + a[s:b] + a[t:s] + a[e:]
print(*a)

Submission status: Accepted

Solution code:
def swapRange(A,b,e,t):
    for k in range(e-b):
        A[b+k],A[t+k]=A[t+k],A[b+k]
    return A

n=int(input())
A=list(map(int,input().split()))
q=int(input())

for i in range(q):
    query=list(map(int,input().split()))
    A=swapRange(A,query[0],query[1],query[2])
print(' '.join(map(str,A)))

Submission status: Accepted

Solution code:
n = int(input())
arr = input().split()
nq = int(input())

for i in range(nq):
    begin, end, tgt = map(int, input().split())
    length = end - begin
    if begin < tgt:
        arr = arr[:begin] + arr[tgt:tgt+length] + arr[end:tgt] + arr[begin:end] + arr[tgt+length:]
    else:
        arr = arr[:tgt] + arr[begin:end] + arr[tgt+length:begin] + arr[tgt:tgt+length] + arr[end:]

print(*arr)

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_4_C&lang=jp
# Swap

from collections import deque
import sys
input = sys.stdin.readline

def main():
    n1 = int(input())
    l1 = list(map(int,input().split()))
    for i in range(int(input())):
        (b,e,t) = map(int,input().split())
        for k in range(e-b):
            l1[k+b],l1[t+k] = l1[t+k] ,l1[k+b]
    print (" ".join(map(str,l1)))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
num = list(map(int, input().split()))

q = int(input())
for _ in range(q):
    b, e, t = map(int, input().split())
    for i in range(e-b):
        num[b+i], num[t+i] = num[t+i], num[b+i]

print(' '.join(str(n) for n in num))

Submission status: Accepted

Solution code:
def my_swap(ary, b, e, t):
    k = 0
    while k < (e - b):
        temp = ary[b+k]
        ary[b+k] = ary[t+k]
        ary[t+k] = temp
        k += 1
    return ary


def main():
    input()
    ary = [int(x) for x in input().split(' ')]
    q = int(input())
    for i in range(q):
        b, e, t = [int(x) for x in input().split(' ')]
        ary = my_swap(ary, b, e, t)
    print(' '.join([str(x) for x in ary]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
readline = open(0).readline
N = int(readline())
*A, = map(int, readline().split())
Q = int(readline())
for _ in range(Q):
    b, e, t = map(int, readline().split())
    for k in range(e-b):
        A[b+k], A[t+k] = A[t+k], A[b+k]
print(*A)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
for i in range(q):
  b, e, t = list(map(int, input().split()))
  for k in range(e-b):
    a[b+k], a[t+k] = a[t+k], a[b+k]
print(" ".join(map(str, a)))