Problem p03426 (python)

Fixed submission:
h, w, d = map(int, input().split())
arr = [0] * (h * w + 1)

a = [list(map(int, input().split())) for _ in range(h)]

for i in range(h):
    for j in range(w):
        s = a[i][j]
        arr[s] = (i, j)

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

cnt = h * w
result = [[0] for _ in range(d)]
for i in range(d):
    l = i
    score = 0
    while l + d <= cnt:
        if l == 0:
            result[i].append(score)
            l += d
            continue
        (ux, uy) = arr[l]
        (vx, vy) = arr[l + d]
        score += (abs(vx - ux) + abs(vy - uy))
        result[i].append(score)
        l += d


for l, r in lr:
    ans = result[l % d]
    print(ans[r // d] - ans[l // d])

Buggy submission (verdict: Runtime Error):
import numpy as np

h, w, d = map(int, input().split())
arr = np.zeros([h, w], dtype=np.int64)

for i in range(h):
    arr[i] = list(map(int, input().split()))

q = int(input())
lr = np.zeros([q, 2], dtype=np.int64)

for i in range(q):
    lr[i] = list(map(int, input().split()))


cnt = h * w
result = [[0] for _ in range(d)]
for i in range(d):
    l = i
    score = 0
    while l + d <= cnt:
        if l == 0:
            result[i].append(score)
            l += d
            continue
        u = np.where(arr == l)
        v = np.where(arr == l + d)
        score += abs(v[0][0] - u[0][0]) + abs(v[1][0] - u[1][0])
        result[i].append(score)
        l += d


result = solve(h, w, d, arr)


for l, r in lr:
    ans = result[l % d]
    print(ans[r // d] - ans[l // d])

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
from itertools import accumulate
import numpy as np

h, w, d = map(int, input().split())
arr = np.zeros([h, w], dtype=np.int64)

for i in range(h):
    arr[i] = list(map(int, input().split()))

q = int(input())
lr = np.zeros([q, 2], dtype=np.int64)

for i in range(q):
    lr[i] = list(map(int, input().split()))


result = defaultdict(list)

for i in range(d):
    l = i
    while l + d <= h * w:
        if l == 0:
            result[i].append(0)
            l += d
            continue
        u = np.where(arr == l)
        v = np.where(arr == l + d)
        result[i].append(abs(v[0][0] - u[0][0]) + abs(v[1][0] - u[1][0]))
        l += d

for i in range(d):
    result[i] = list(accumulate(result[i]))

ans = []
for l, r in lr:
    arr = result[l % d]
    ans.append(arr[r // d - 1] - arr[l // d - 1])

print(*ans, sep="\n")

Buggy submission (verdict: Runtime Error):
import numpy as np
from numba import njit, i8

h, w, d = map(int, input().split())
arr = np.zeros([h, w], dtype=np.int64)

for i in range(h):
    arr[i] = list(map(int, input().split()))

q = int(input())
lr = np.zeros([q, 2], dtype=np.int64)

for i in range(q):
    lr[i] = list(map(int, input().split()))


@njit
def solve(arr, lr, d):
    ans = []
    for l, r in lr:
        score = 0
        while l != r:
            tmp = l + d
            u = np.where(arr == l)
            v = np.where(arr == tmp)
            score += (abs(v[0][0] - u[0][0]) + abs(v[1][0] - u[1][0]))
            l += d
        ans.append(score)
    return ans


print(*solve(arr, lr, d), sep="\n")