TnT/Multi_CodeNet4Repair huggingface dataset:
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):

#import numpy as np


def acinput():
    return list(map(int, input().split(" ")))


mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


H, W, D = acinput()
# Ad={}
Ad = [0]*(H*W+1)
for i in range(H):
    ar = acinput()
    for j, sar in enumerate(ar):
        # Ad[sar]=np.array((i,j))
        Ad[sar] = (i, j)

#dp = [[0 for j in range(H*W+1)]for i in range(H*W+1)]
d = [0]*(H*W+1)

# for x in range(1, H*W+1):
#    dp[x][x]=0

y = D+1
while True:
        if y > H*W:
            break
        # print(x,y)
        #tmp = Ad[y]-Ad[y-D]
        #S = np.sum(np.abs(tmp[0])+abs(tmp[1]))
        #tmp = Ad[y]-Ad[y-D]
        S = abs(Ad[y][0]-Ad[y-D][0])+abs(Ad[y][1]-Ad[y-D][1])

        # dp[x][y]=dp[x][y-D]+S
        d[y] = d[y-D]+S
        y += 1


# print(np.array(dp))

Q = int(input())
for i in range(Q):
    l, r = acinput()
    print(d[r]-d[l])


# print(Ad)
# print(np.array(dp))
print(d)


#import numpy as np


def acinput():
    return list(map(int, input().split(" ")))


mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


H, W, D = acinput()
# Ad={}
Ad = [0]*(H*W+1)
for i in range(H):
    ar = acinput()
    for j, sar in enumerate(ar):
        # Ad[sar]=np.array((i,j))
        Ad[sar] = (i, j)

#dp = [[0 for j in range(H*W+1)]for i in range(H*W+1)]
d = [0]*(H*W+1)

# for x in range(1, H*W+1):
#    dp[x][x]=0

y = D+1
while True:
        if y > H*W:
            break
        # print(x,y)
        #tmp = Ad[y]-Ad[y-D]
        #S = np.sum(np.abs(tmp[0])+abs(tmp[1]))
        #tmp = Ad[y]-Ad[y-D]
        S = abs(Ad[y][0]-Ad[y-D][0])+abs(Ad[y][1]-Ad[y-D][1])

        # dp[x][y]=dp[x][y-D]+S
        d[y] = d[y-D]+S
        y += 1


# print(np.array(dp))

Q = int(input())
for i in range(Q):
    l, r = acinput()
    print(d[r]-d[l])


# print(Ad)
# print(np.array(dp))
#print(d)

Verdicts: Wrong Answer

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]

d = {}
for y in range(H):
    for x in range(W):
        d[A[y][x]] = (y, x)

t = [[0] * (H * W // D + 1) for _ in range(D)]
for i in range(D):
    j = 0
    if i == 0:
        j = 1
    while i + (j + 1) * D <= H * W:
        y1, x1 = d[i + j * D]
        y2, x2 = d[i + (j + 1) * D]
        t[i][j + 1] = abs(y1 - y2) + abs(x1 - x2)
        j += 1
    for j in range(len(H * W // D + 1) - 1):
        t[i][j + 1] += t[i][j]

Q = int(input())
for _ in range(Q):
    L, R = map(int, input().split())
    i = L % D
    print(t[i][(R - i) // D] - t[i][(L - i) // D])


H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]

d = {}
for y in range(H):
    for x in range(W):
        d[A[y][x]] = (y, x)

t = [[0] * (H * W // D + 1) for _ in range(D)]
for i in range(D):
    j = 0
    if i == 0:
        j = 1
    while i + (j + 1) * D <= H * W:
        y1, x1 = d[i + j * D]
        y2, x2 = d[i + (j + 1) * D]
        t[i][j + 1] = abs(y1 - y2) + abs(x1 - x2)
        j += 1
    for j in range(H * W // D):
        t[i][j + 1] += t[i][j]

Q = int(input())
result = []
for _ in range(Q):
    L, R = map(int, input().split())
    i = L % D
    result.append(t[i][(R - i) // D] - t[i][(L - i) // D])

print(*result, sep='\n')


Verdicts: Runtime Error

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
#19:38
#20:19

H,W,D = map(int,input().split())
MAX = H*W
A = [list(map(int,input().split())) for i in range(H)]
Point = [0]*(MAX+1)
for h in range(H):
    for w in range(W):
        Point[A[h][w]] = (h,w)
K = []
for i in range(1,D+1):
    List = []
    n = i
    h,w = Point[n]
    cost = 0
    while n<=MAX:
        y,x = Point[n]
        cost += abs(h-y)+abs(w-x)
        List.append(cost)
        h,w = y,x
        n+=D
    K.append(List)
Q = int(input())
for k in K:print(k)
for i in range(Q):
    L,R = map(int,input().split())
    a,l,r = (L-1)%D,L//D,R//D
    try:
        print(K[a][r]-K[a][l])
    except:
        print(K[a][r-1]-K[a][l-1])

#19:38
#20:19

H,W,D = map(int,input().split())
MAX = H*W
A = [list(map(int,input().split())) for i in range(H)]
Point = [0]*(MAX+1)
for h in range(H):
    for w in range(W):
        Point[A[h][w]] = (h,w)
K = []
for i in range(1,D+1):
    List = []
    n = i
    h,w = Point[n]
    cost = 0
    while n<=MAX:
        y,x = Point[n]
        cost += abs(h-y)+abs(w-x)
        List.append(cost)
        h,w = y,x
        n+=D
    K.append(List)
Q = int(input())
for i in range(Q):
    L,R = map(int,input().split())
    a,l,r = (L-1)%D,(L-1)//D,(R-1)//D
    print(K[a][r]-K[a][l])

Verdicts: Wrong Answer

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
h, w, d = map(int, input().split())
x = []
z = [[0, 0] for i in range(h * w)]
for i in range(h):
    x.append(list(map(int, input().split())))
    for j in range(w):
        z[x[i][j] - 1] = [i + 1, j + 1]
print(z)
s = [0 for _ in range(d)]
for i in range(d):
    a = z[max(0, i - 1)]
    b = z[d + i - 1]
    s[i] = [abs(a[0] - b[0]) + abs(a[1] - b[1])]
    j = 1
    while(True):
        if d * (j + 1) + i <= h * w:
            a = z[d * j + i - 1]
            b = z[d * (j + 1) + i - 1]
            s[i].append(s[i][-1] + abs(a[0] - b[0]) + abs(a[1] - b[1]))
            j += 1
        else:
            break

Q = int(input())
ans = [0 for _ in range(Q)]
for i in range(Q):
    L, R = map(int, input().split())
    if L == R:
        ans[i] = 0
    elif L // d - 1 < 0:
        ans[i] = s[R % d][R // d - 1]
    else:
        ans[i] = s[R % d][R // d - 1] - s[L % d][L // d - 1]
for i in range(Q):
    print(ans[i])

h, w, d = map(int, input().split())
x = []
z = [None for i in range(h * w)]
for i in range(h):
    x.append(list(map(int, input().split())))
    for j in range(w):
        z[x[i][j] - 1] = [i + 1, j + 1]

s = [None for _ in range(d)]
for i in range(min(d, h * w - d + 1)):
    a = z[max(0, i - 1)]
    b = z[d + i - 1]
    s[i] = [abs(a[0] - b[0]) + abs(a[1] - b[1])]
    j = 1
    while(True):
        if d * (j + 1) + i <= h * w:
            a = z[d * j + i - 1]
            b = z[d * (j + 1) + i - 1]
            s[i].append(s[i][-1] + abs(a[0] - b[0]) + abs(a[1] - b[1]))
            j += 1
        else:
            break

Q = int(input())
ans = [None for _ in range(Q)]
for i in range(Q):
    L, R = map(int, input().split())
    if L == R:
        ans[i] = 0
    elif L // d - 1 < 0:
        ans[i] = s[R % d][R // d - 1]
    else:
        ans[i] = s[R % d][R // d - 1] - s[L % d][L // d - 1]
for i in range(Q):
    print(ans[i])

Verdicts: Runtime Error

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
H,W,D=map(int,input().split())
d=[[0,0] for i in range(H*W+1)]
d2=[0 for i in range(H*W+1)]
for j in range(H):
  a=[int(x) for x in input().split()]
  for k in range(W):
    d[a[k]]=[j+1,k+1]
for l in range(D+1,H*W+1-D):
  d2[l]=abs(d[l][0]-d[l-D][0])+abs(d[l][1]-d[l-D][1])
for m in range(D+1,H*W+1-D):
  d2[m]+=d2[m-D]
Q=int(input())
for i in range(Q):
  a,b=map(int,input().split())
  if a==b:
    print(0)
  else:
    ans=d2[b]-d2[a]
    print(ans)

H,W,D=map(int,input().split())
d=[[0,0] for i in range(H*W+1)]
d2=[0 for i in range(H*W+1)]
for j in range(H):
  a=[int(x) for x in input().split()]
  for k in range(W):
    d[a[k]]=[j+1,k+1]
for l in range(D+1,H*W+1):
  d2[l]=abs(d[l][0]-d[l-D][0])+abs(d[l][1]-d[l-D][1])
for m in range(D+1,H*W+1):
  d2[m]+=d2[m-D]
Q=int(input())
for i in range(Q):
  a,b=map(int,input().split())
  if a==b:
    print(0)
  else:
    ans=d2[b]-d2[a]
    print(ans)

Verdicts: Wrong Answer

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
H, W, D = map(int, input().split())
A = []
C = [0 for _ in range(H * W + 1)]
for h in range(H):
    A.append(list(map(int, input().split())))
for i in range(H):
    for j in range(W):
        C[A[i][j]] = (i, j)
h = [0 for _ in range(H * W + 1)]
for i in range(D):
    s = 0
    for j in range(i, H * W, D):
        if j != i:
            print(j)
            n = (C[j + 1][0], C[j + 1][1])
            p = (C[j + 1 - D][0], C[j + 1 - D][1])
            s += abs(n[0] - p[0]) + abs(n[1] - p[1])
        h[j + 1] = s
Q = int(input())
for _ in range(Q):
    l, r = map(int, input().split())
    print(h[r] - h[l])


H, W, D = map(int, input().split())
A = []
C = [0 for _ in range(H * W + 1)]
for h in range(H):
    A.append(list(map(int, input().split())))
for i in range(H):
    for j in range(W):
        C[A[i][j]] = (i, j)
print(A)
print(C)
h = [0 for _ in range(H * W + 1)]
for i in range(D):
    s = 0
    for j in range(i, H * W, D):
        if j != i:
            print(j)
            n = (C[j + 1][0], C[j + 1][1])
            p = (C[j + 1 - D][0], C[j + 1 - D][1])
            s += abs(n[0] - p[0]) + abs(n[1] - p[1])
        h[j + 1] = s
Q = int(input())
for _ in range(Q):
    l, r = map(int, input().split())
    print(h[r] - h[l])


H, W, D = map(int, input().split())
A = []
C = [0 for _ in range(H * W + 1)]
for h in range(H):
    A.append(list(map(lambda x: int(x) - 1, input().split())))
for i in range(H):
    for j in range(W):
        C[A[i][j]] = (i, j)
h = [0 for _ in range(H * W)]
for i in range(D):
    s = 0
    for j in range(i, H * W, D):
        if j != i:
            n = (C[j][0], C[j][1])
            p = (C[j - D][0], C[j - D][1])
            s += abs(n[0] - p[0]) + abs(n[1] - p[1])
        h[j] = s
Q = int(input())
for _ in range(Q):
    l, r = map(lambda x: int(x) - 1, input().split())
    print(h[r] - h[l])


Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
LR = [list(map(int, input().split())) for _ in range(Q)]

res = 0
for i in range(10 **7):
  res += 1
  
  
print(res)

H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
LR = [list(map(int, input().split())) for _ in range(Q)]

res = 0
for i in range(10 **8):
  res += 1
  
  
print(res)

H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
LR = [list(map(int, input().split())) for _ in range(Q)]

number_dict = {}
for h in range(H):
    for w in range(W):
        number_dict[A[h][w]] = (w+1, h+1)
        
        
number = [0 for _ in range(H*W+1)]
for i in range(H*W):
    now = H * W - i
    
    if now + D > H * W:
        pass
    
    else:
        number[now] = number[now + D] + abs(number_dict[now+D][0] - number_dict[now][0]) + abs(number_dict[now+D][1] - number_dict[now][1])
       

for i in range(Q):
    res = number[LR[i][0]] - number[LR[i][1]]
            
    print(res)
        



Verdicts: Wrong Answer

Time Limit Exceeded

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
import collections
import itertools
from pprint import pprint as pp

H,W,D = tuple(map(int, input().split()))
A = [tuple(map(int, input().split())) for _ in range(H)]
Q = int(input())
LR = [tuple(map(int, input().split())) for _ in range(Q)]

Arev = [None] * (H*W+1)
for h in range(H):
  for w in range(W):
    Arev[A[h][w]] = (h,w)

tbl = []
for i in range(D):
  i += 1
  ary = []
  mp = 0
  start = Arev[i]
  for x in range(i, H*W, D):
    next_ = Arev[x]
    mp += abs(start[0]-next_[0]) + abs(start[1]-next_[1])
    ary.append((x,mp))
    start = next_
  tbl.append(ary)
pp(tbl)
for L,R in LR:
  col1, row = divmod(L, D)
  col2 = R // D
  if row == 0:
    col1 -= 1
    col2 -= 1
  row -= 1
  pp((row,col1,col2))
  print(tbl[row][col2][1] - tbl[row][col1][1])


import collections
import itertools
from pprint import pprint as pp

H,W,D = tuple(map(int, input().split()))
A = [tuple(map(int, input().split())) for _ in range(H)]
Q = int(input())
LR = [tuple(map(int, input().split())) for _ in range(Q)]

Arev = [None] * (H*W+1)
for h in range(H):
  for w in range(W):
    Arev[A[h][w]] = (h,w)

tbl = []
for i in range(D):
  ary = []
  mp = 0
  start = Arev[i+1]
  for x in range(i, H*W, D):
    next_ = Arev[x+1]
    mp += abs(start[0]-next_[0]) + abs(start[1]-next_[1])
    ary.append((x+1,mp))
    start = next_
  tbl.append(ary)
#pp(A)
#pp(tbl)
for L,R in LR:
  col1, row = divmod(L, D)
  col2 = R // D
  if row == 0:
    col1 -= 1
    col2 -= 1
  row -= 1
  #pp((row,col1,col2))
  print(tbl[row][col2][1] - tbl[row][col1][1])


Verdicts: Runtime Error

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
H, W, D=map(int, input().split())
pos={}
for i in range(H):
    A=list(map(int, input().split()))
    for j in range(W):
        pos[A[j]]=(i, j)
p=[0]*(H*W)
for i in range(D+1,H*W+1):
    p[i]=p[i-D]+abs(pos[i][0]-pos[i-D][0])+abs(pos[i][1]-pos[i-D][1])
Q=int(input())
for _ in range(Q):
    L, R=map(int, input().split())
    print(p[R]-p[L])


H, W, D=map(int, input().split())
pos={}
for i in range(H):
    A=list(map(int, input().split()))
    for j in range(W):
        pos[A[j]]=(i, j)
p=[0]*(H*W+1)
for i in range(D+1,H*W+1):
    p[i]=p[i-D]+abs(pos[i][0]-pos[i-D][0])+abs(pos[i][1]-pos[i-D][1])
print(p)
Q=int(input())
for _ in range(Q):
    L, R=map(int, input().split())
    print(p[R]-p[L])


H, W, D=map(int, input().split())
pos={}
for i in range(H):
    A=list(map(int, input().split()))
    for j in range(W):
        pos[A[j]]=(i, j)
p=[0]*(H*W+1)
for i in range(D+1,H*W+1):
    p[i]=p[i-D]+abs(pos[i][0]-pos[i-D][0])+abs(pos[i][1]-pos[i-D][1])
Q=int(input())
for _ in range(Q):
    L, R=map(int, input().split())
    print(p[R]-p[L])


Verdicts: Runtime Error

Wrong Answer

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
H, W, D, *L = map(int, open('0').read().split())
A = L[:W*H]
Q = L[H*W]
inf = []
for l,r in zip(*[iter(L[H*W+1:])]*2):
  inf += [(l,r)]
dic = {A[i*W+j]:(i,j) for i in range(H) for j in range(W)}
ls = [[] for i in range(D)]
for i in range(D):
  s = i if i!=0 else D
  ls[i].append(0)
  m = s
  n = s
  while True:
    n += D
    if H*W<n:
      n = s
    px, py = dic[m]
    x, y = dic[n]
    ls[i].append(ls[i][-1]+abs(px-x)+abs(py-y))
    m = n
    if m==s:
      break
for l, r in inf:
  i = l%D
  if i==0:
    il = (l-D)//D
    ir = (r-D)//D
  else:
    il = (l-i)//D
    ir = (r-i)//D
  if l<r:
    print(ls[i][ir] - ls[i][il])
  elif l>r:
    print(ls[i][-1]-ls[i][il]+ls[i][ir])
  else:
    print(0)

H, W, D, *L = map(int, open(0).read().split())
A = L[:W*H]
Q = L[H*W]
inf = []
for l,r in zip(*[iter(L[H*W+1:])]*2):
  inf += [(l,r)]
dic = {A[i*W+j]:(i,j) for i in range(H) for j in range(W)}
ls = [[] for i in range(D)]
for i in range(D):
  s = i if i!=0 else D
  ls[i].append(0)
  m = s
  n = s
  while True:
    n += D
    if H*W<n:
      n = s
    px, py = dic[m]
    x, y = dic[n]
    ls[i].append(ls[i][-1]+abs(px-x)+abs(py-y))
    m = n
    if m==s:
      break
for l, r in inf:
  i = l%D
  if i==0:
    il = (l-D)//D
    ir = (r-D)//D
  else:
    il = (l-i)//D
    ir = (r-i)//D
  if l<r:
    print(ls[i][ir] - ls[i][il])
  elif l>r:
    print(ls[i][-1]-ls[i][il]+ls[i][ir])
  else:
    print(0)

Verdicts: Runtime Error

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
import numpy as np

H, W, D = map(int, input().split())
pos = [None]*(H*W)

for i in range(H):
    row = list(map(int, input().split()))
    for j in row:
        pos[row[j]] = (i, j)

Q = int(input())
for _ in range(Q):
    L, R = map(int, input().split())
    if L == R:
        print(0)
    else:
        to_be_checked = [i for i in range(L, R+1, D)]
        moves = len(to_be_checked)
        pos_list = [tpl for tpl in to_be_checked]

        MP = sum([np.absolute(pos_list[i][0] - pos_list[i+1][0]) for i in range(moves-1)]) + sum([np.absolute(pos_list[i][1] - pos_list[i+1][1]) for i in range(moves-1)])

        print(MP)

H, W, D = map(int, input().split())
position = {}

for i in range(H):
    row = list(map(int, input().split()))
    for j, value in enumerate(row):
        position[value] = (i, j)

costs = {i: 0 for i in range(1, D+1)}
for i in range(1, D+1):
    current = i + D
    while current <= H*W:
        costs[current] = costs[current-D] + abs(position[current][0] - position[current-D][0]) + abs(position[current][1] - position[current-D][1])
        current += D

Q = int(input())
for _ in range(Q):
    L, R = map(int, input().split())
    MP = costs[R] - costs[L]
    print(MP)

Verdicts: Runtime Error

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
h,w,d=map(int,input().split())
l={}
for i in range(h):
  l1=list(map(int,input().split()))
  for j in l1:
    l[j]=[l1.index(j)+1,i+1]
li=[]
for i in range(1,d+1):
  l11=[]
  c=0
  x=l[i][0]
  y=l[i][1]
  while i<=h*w:
    c+=abs(x-l[i][0])+abs(y-l[i][1])
    l11.append(c)
    x=l[i][0]
    y=l[i][1]
    i+=d
  li.append(l11)
q=int(input())
for i in range(q):
  a,b=map(int,input().split())
  r=a%d
  if r==0:
    print(li[d][b//d-1]-li[d][a//d-1])
  else:
    print(li[r-1][b//d]-li[r-1][a//d])

h,w,d=map(int,input().split())
l={}
for i in range(h):
  l1=list(map(int,input().split()))
  for j in l1:
    l[j]=[l1.index(j)+1,i+1]
li=[]
for i in range(1,d+1):
  l11=[]
  c=0
  x=l[i][0]
  y=l[i][1]
  while i<=h*w:
    c+=abs(x-l[i][0])+abs(y-l[i][1])
    l11.append(c)
    x=l[i][0]
    y=l[i][1]
    i+=d
  li.append(l11)
q=int(input())
for i in range(q):
  a,b=map(int,input().split())
  r=a%d
  if r==0:
    print(li[d-1][b//d-1]-li[d-1][a//d-1])
  else:
    print(li[r-1][b//d]-li[r-1][a//d])

Verdicts: Runtime Error

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
ai = lambda: list(map(int, input().split()))

h,w,d = ai()
ph = [0 for _ in range(h*w + 1)]
pw = [0 for _ in range(h*w + 1)]

for i in range(h):
    a = ai()
    for j in range(w):
        ph[a[j]] = i
        pw[a[j]] = j


score = [0 for _ in range(h*w + 1)]
for i in range(d+1, h*w+1):
    score[i] = score[i-d] + abs(ph[i]-ph[i-d]) + abs(pw[i]-pw[i-d])

print(score)

q = int(input())
for _ in range(q):
    l,r = ai()
    print(score[r]-score[l])

ai = lambda: list(map(int, input().split()))

h,w,d = ai()
ph = [0 for _ in range(h*w + 1)]
pw = [0 for _ in range(h*w + 1)]

for i in range(h):
    a = ai()
    for j in range(w):
        ph[a[j]] = i
        pw[a[j]] = j


score = [0 for _ in range(h*w + 1)]
for i in range(d+1, h*w+1):
    score[i] = score[i-d] + abs(ph[i]-ph[i-d]) + abs(pw[i]-pw[i-d])


q = int(input())
ans = []
for _ in range(q):
    l,r = ai()
    ans.append(score[r]-score[l])
print(*ans, sep='\n')


Verdicts: Wrong Answer

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
import numpy as np
H, W, D = input().split()
H, W, D = int(H), int(W), int(D)

px, py = np.zeros(H*W+1), np.zeros(H*W+1)
for i in range(H):
    A = input().split()
    for j in range(W):
        px[int(A[j])] = i
        py[int(A[j])] = j

d = np.zeros(H*W+1)
for i in range(D+1, H*W+1):
    d[i]= d[i-D]+abs(px[i]-px[i-D])+abs(py[i]-py[i-D])

Q = int(input())

for i in range(Q):
    L, R = input().split()
    L = int(L)
    R = int(R)
    print(d[R]-d[L])

import numpy as np
H, W, D = map(int, input().split())

px, py = np.zeros(H*W+1, dtype=int), np.zeros(H*W+1, dtype=int)
for i in range(H):
    A = map(int, input().split())
    for j in range(W):
        px[A[j]] = i
        py[A[j]] = j

d = np.zeros(H*W+1, dtype=int)
for i in range(D+1, H*W+1):
    d[i]= d[i-D]+abs(px[i]-px[i-D])+abs(py[i]-py[i-D])

Q = int(input())

for i in range(Q):
    L, R = map(int, input().split())
    print(d[R]-d[L])

import numpy as np
H, W, D = list(map(int, input().split()))

px, py = [None for i in range(H*W+1)], [None for i in range(H*W+1)]
for i in range(H):
    A = list(map(int, input().split()))
    for j in range(W):
        px[A[j]] = i
        py[A[j]] = j

d = [0 for i in range(H*W+1)]
for i in range(D+1, H*W+1):
    d[i]= d[i-D]+abs(px[i]-px[i-D])+abs(py[i]-py[i-D])

Q = int(input())

for i in range(Q):
    L, R = list(map(int, input().split()))
    print(d[R]-d[L])

Verdicts: Wrong Answer

Runtime Error

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
H, W, D = map(int, input().split())

A = [list(map(int, input().split())) for i in range(H)]
rA = [[0 for j in range(2)] for i in range(H * W + 1)]
S = [0 for i in range(H * W + 1)]
for i in range(H):
	for j in range(W):
		rA[A[i][j]][0] = i
		rA[A[i][j]][1] = j

Q = int(input())
for i in range(Q):
	L, R = map(int, input().split())
	ans = 0
	for j in range(H * W) [L::D]:
		if j == R:
			break
		if S[j]:
			ans += S[j]
			break
		S[j] -= ans
		ans += abs(rA[j + D][1] - rA[j][1]) + abs(rA[j + D][0] - rA[j][0])
	for j in range(H * W)[L::D]:
		if S[j] > 0:
			break
		if j == R
			break
		S[j] += ans
	print (ans)


H, W, D = map(int, input().split())
 
A = [list(map(int, input().split())) for i in range(H)]
rA = [[0 for j in range(2)] for i in range(H * W + 1)]
S = [0 for i in range(H * W + 1)]
for i in range(H):
	for j in range(W):
		rA[A[i][j]][0] = i
		rA[A[i][j]][1] = j
 
Q = int(input())
for i in range(Q):
	L, R = map(int, input().split())
	ans = 0
	for j in range(H * W) [L::D]:
		if j + D > H * W:
			break
		if S[j]:
			ans += S[j]
			break
		S[j] -= ans
		ans += abs(rA[j + D][1] - rA[j][1]) + abs(rA[j + D][0] - rA[j][0])
	for j in range(H * W)[L::D]:
		if S[j] > 0:
			break
		if j + D > H * W:
			break
		S[j] += ans
	print(S[L] - S[R])


Verdicts: Runtime Error

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
H,W,D=map(int,input().split())
A=[list(map(int,input().split())) for _ in range(H)]
Q=int(input())
LR=[list(map(int,input().split())) for _ in range(Q)]

c=[None]*(H*W)
for i in range(H):
  for j in range(W):
    c[A[i][j]-1] = (i, j)

d=[0]*(H*W)
for i in range(D, H*W):
  px,py = c[i-D]
  x, y = c[i]
  d[i]=d[i-D] + abs(x-px) + abs(y-py)
for l, r in LR:
  print(d[r]-d[l])

H,W,D=map(int,input().split())
A=[list(map(int,input().split())) for _ in range(H)]
Q=int(input())
LR=[tuple(map(int,input().split())) for _ in range(Q)]

pos=[None]*(H*W)
for i in range(H):
  for j in range(W):
    pos[A[i][j]-1] = (i, j)

d=[0]*(H*W)
for i in range(D, H*W):
  px,py = pos[i-D]
  x, y = pos[i]
  d[i]=d[i-D] + abs(x-px) + abs(y-py)

for l, r in LR:
  print(d[r-1]-d[l-1])

Verdicts: Runtime Error

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
# coding=utf-8
line = input().split(" ")
h = int(line[0])
w = int(line[1])
d = int(line[2])

table = [0 for _ in range(h*w+1)]
for hi in range(h):
    line = list(map(int, input().split(" ")))

    for wi in range(w):
        table[line[wi]] = (hi+1, wi+1)

def calc(l,r):
    ret = 0
    if l!=r:
        c1 = table[l]
        c2 = table[r]
        ret += abs(c1[0]-c2[0])+abs(c1[1]-c2[1])
    return ret

acc = [0 for _ in range(h*w+1)]
for dd in range(1,d+1):
    s = dd+d
    while s+d<=h*w:
        acc[s] = calc(s-d,s)+acc[s-d]
        s += d

q = int(input())
for _ in range(q):
    line = input().split(" ")
    l = int(line[0])
    r = int(line[1])

    ret = acc[r] - acc[l]
    print(ret)


h,w,d = map(int, input().split())
ad = {}
for i in range(h):
    line = list(map(int, input().split()))
    for j in range(w):
        ad[line[j]] = (i,j)
q = int(input())
lr = [list(map(int, input().split())) for _ in range(q)]

cl = [0]*(h*w+1)
for i in range(1,h*w+1):
    if i+d<len(cl):
        f = ad[i]
        t = ad[i+d]
        cl[i+d] = cl[i] + abs(f[0]-t[0]) + abs(f[1]-t[1])

for l,r in lr:
    print(cl[r]-cl[l])

Verdicts: Wrong Answer

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
b = str(input()).split()
h = int(b[0])
w = int(b[1])
d = int(b[2])
c =[]

def abs(a,b):
    if a > b:
        return(a-b)
    else:
        return(b-a)

for i in range(0,h):
    c.append(str(input()).split())

q = int(input())
lr = []
energy = 0



for i in range(0,q):
    lr.append(str(input()).split())

for i in range(0,q):
    l = int(lr[i][0])
    r = int(lr[i][1])
    for j in range(0,h):
        for k in range(0,w):
            if int(c[j][k]) == l:
                li = j
                lj = k
            if int(c[j][k]) == l + d:
                di = j
                dj = k
        if l != r:
            energy += abs(li,di) + abs(lj,dj)
            l += d
        else:
            print(energy)
            break


def list_1d(d,v):
    return [ v for i in range(d)]

def abs(c): 
    if c > 0: 
        return c 
    else: 
        return -c

def distance_2d(a,b):
    return(abs(a[0]-b[0])+abs(a[1]-b[1]))

h,w,d = [int(i) for i in input().split()]
sa = list_1d(h*w+1,[])
da = list_1d(h*w+1,0)
for i in range(h):
    a = [int(k) for k in input().split()]
    for j in range(w):
        sa[a[j]] = [i,j]
q = int(input())
lr = [[int(i) for i in input().split()] for i in range(q)]

for i in range(d+1,h*w+1):
    da[i] = da[i-d] + distance_2d(sa[i],sa[i-d])

for i in range(0,q):
    print(da[lr[i][1]]-da[lr[i][0]])


Verdicts: Runtime Error

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
h, w, d = map(int, input().split(' '))
q = int(input())
lr = [map(int, input().split(' ')) for _ in range(q)]

comp = {}
for i in range(h):
    tmp = list(map(int, input().split(' ')))
    for j in range(w):
        comp[tmp[j]] = [i, j]

dp = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
    cnt = dp[i - d]
    cnt += abs(comp[i][0] - comp[i - d][0])
    cnt += abs(comp[i][1] - comp[i - d][1])    
    dp[i] = cnt
    

for l, r in lr:
    print(dp[r] - dp[l])

h, w, d = map(int, input().split(' '))

comp = {}
for i in range(h):
    tmp = list(map(int, input().split(' ')))
    for j in range(w):
        comp[tmp[j]] = [i, j]

q = int(input())
lr = [map(int, input().split(' ')) for _ in range(q)]

dp = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
    cnt = dp[i - d]
    cnt += abs(comp[i][0] - comp[i - d][0])
    cnt += abs(comp[i][1] - comp[i - d][1])    
    dp[i] = cnt
    

for l, r in lr:
    print(dp[r] - dp[l])

Verdicts: Runtime Error

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
H,W,D=list(map(int,input().split()))
arr=[0 for i in range(H*W+1)]
for y in range(H):
    for x,n in enumerate(map(int,input().split())):
        arr[n]=[x,y]

dic={}

Q=int(input())
for i in range(Q):
    ans=0
    now,goal= list(map(int,input().split()))
    nx=arr[now][0]
    ny=arr[now][1]
    tmp=now
    for j in range((goal-now)//D):
        if dic.get(tmp*H*W+goal,0)!=0:
            dic[now*H*W+goal]=ans+dic[tmp*H*W+goal]
            print(dic[now*H*W+goal])
            break
        tmp+=D
        x=arr[tmp][0]
        y=arr[tmp][1]
        ans+=abs(x-nx)+abs(y-ny)
        nx=x
        ny=y
        dic[now*H*W+tmp]=ans
    if dic.get(now*H*W+goal,0)==0:
        dic[now*H*W+goal]=ans
        print(ans)

H,W,D=list(map(int,input().split()))
map_=[[0,0] for _ in range(H*W+1)]
for y in range(H):
    for x,A in enumerate(map(int,input().split())):
        map_[A]=[x+1,y+1]

cost=[0 for _ in range(H*W+1)]
for i in range(D+1,H*W+1):
    cost[i]+=abs(map_[i][0]-map_[i-D][0])+abs(map_[i][1]-map_[i-D][1])+cost[i-D]
Q=int(input())
for i in range(Q):
    L,R=list(map(int,input().split()))
    print(cost[R]-cost[L])

Verdicts: Wrong Answer

Accepted
Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
H, W, D = list(map(int, input().split()))
 
dic = {}
for i in range(H):
    for j,v in enumerate(list(map(int, input().split()))):
        dic[v] = (i,j)
 
Q = int(input())
 
Qs = []
mi = 100000
ma = 1
for i in range(Q):
    l,r = list(map(int, input().split()))
    Qs.append((l,r))
    mi = min(mi, l)
    ma = max(ma, r)
 
S = [0 for i in range(100000)]
acm = 0
pi, pj = dic[mi]
for start in range(1, D):
    for i in range(start, ma+1, D):
        x,y = dic[i]
        acm += abs(x-pi) + abs(y-pj)
        S[i] = acm
        pi,pj = x,y
    
for i in Qs:
    l,r = i
    print(S[r] - S[l])


H, W, D = list(map(int, input().split()))

A = [[0 for i in range(W)] for j in range(H)]
dic = {}
for i in range(H):
    for j,v in enumerate(list(map(int, input().split()))):
        A[i][j] = v
        dic[v] = (i,j)

Q = int(input())

Qs = []
mi = 10000000
for i in range(Q):
    l,r = list(map(int, input().split()))
    Qs.append((l,r))
    mi = min(mi, l)

Sum = [0 for i in range(100000)]


H, W, D = list(map(int, input().split()))
 
dic = {}
for i in range(H):
    for j,v in enumerate(list(map(int, input().split()))):
        dic[v] = (i,j)
 
Q = int(input())
 
Qs = []
mi = 100000
ma = 1
for i in range(Q):
    l,r = list(map(int, input().split()))
    Qs.append((l,r))
    mi = min(mi, l)
    ma = max(ma, r)
 
S = [0 for i in range(100000)]
acm = 0
pi, pj = dic[mi]
for start in range(1, D+1):
    for i in range(start, ma+1, D):
        x,y = dic[i]
        acm += abs(x-pi) + abs(y-pj)
        S[i] = acm
        pi,pj = x,y

for i in Qs:
    l,r = i
    print(S[r] - S[l])


Verdicts: Wrong Answer

Wrong Answer

Accepted