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())
fields = []
amaris = [[0] * (H*W) for i in range(D)] 
for i in range(H):
    row = list(map(int, input().split()))
    がいくつかで分類
    for j,e in enumerate(row):
        n = (e-1) // D
        m = (e-1) % D
        amaris[m][n] = (i,j)

# print(amaris[:10])
ごとに累積和を取っておく
acc = [[0] * (H*W) for i in range(D)]
for i in range(D):
    cnt = 0
    x_pre, y_pre = amaris[i][0]
    for j in range(1,(H*W)//D+1):
        coords = amaris[i][j]
        if isinstance(coords,int): break
        x = coords[0]
        y = coords[1]
        cnt += abs(x-x_pre)+abs(y-y_pre)
        acc[i][j] = cnt
        x_pre = x
        y_pre = y


Q = int(input())
for i in range(Q):
    l,r  = map(int, input().split())
    m = (l-1) % D
    s = (l-1) // D
    g = (r-1) // D
    ans = acc[m][g] - acc[m][s]
    print(ans)



H,W,D = map(int, input().split())
fields = []
amaris = [[0] * (H*W) for i in range(D)] 
for i in range(H):
    row = list(map(int, input().split()))
    がいくつかで分類
    for j,e in enumerate(row):
        n = (e-1) // D
        m = (e-1) % D
        amaris[m][n] = (i,j)

# print(amaris[:10])

Q = int(input())
for i in range(Q):
    l,r  = map(int, input().split())
    m = (l-1) % D
    s = (l-1) // D
    g = (r-1) // D
    coords = amaris[m][s:g+1]
    ans = 0
    x_pre,y_pre = coords[0]
    for x, y in coords[1:]:
        ans += abs(x-x_pre)+abs(y-y_pre)        
        x_pre = x
        y_pre = y
    print(ans)


H, W , D = map(int, input().split())
fields = []
X = []


def calc_dist(a,b):
    return abs(a//W-b//W) + abs(a%W-b%W)
for i in range(H):
    # fields.append(list(map(int, input().split())))
    X.extend(list(map(int, input().split())))
pairs = [(x-1,i) for i,x in enumerate(X)]
pairs.sort()
numbers = [p[1] for p in pairs]

# print(numbers)
from itertools import accumulate
from collections import defaultdict


d = defaultdict(list)
for i in range(D):
    prev = numbers[i]
    acc = 0
    d[i] = [0]
    for num in numbers[i+D:len(numbers):D]:
        acc += calc_dist(prev,num)
        d[i].append(acc)
        prev = num
# print(d)    
    


Q = int(input())
for i in range(Q):
    L1,R1 = map(int, input().split())
    L1 -= 1
    R1 -= 1
    acc = d[L1%D]
    # print("acc",acc)
    print(acc[R1//D]-acc[L1//D])        


Verdicts: Runtime Error

Time Limit Exceeded

Accepted