Problem p02840 (python)

Fixed submission:
from collections import defaultdict
N,X,D = map(int,input().split())
d = defaultdict(list)
if D<0:
    D *= -1
    X *= -1

if D != 0:
    for m in range(N+1):
        a = X*m+(m*(m-1)*D)//2
        b = X*m+(m*(2*N-m-1)*D)//2
        d[a%D].append([a,b])


    ans = 0
    for k in d:
        q = d[k]
        q.sort()
        l,r = q[0]
        for i in range(len(q)):
            a,b = q[i]
            if a<=r:
                r = max(r,b)
            else:
                ans += (r-l)//D+1
                l = a
                r = b
        ans += (r-l)//D+1

if D == 0:
    ans = N+1
    if X == 0:
        ans = 1
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
H,W = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(H)]
B = [list(map(int,input().split())) for _ in range(H)]
D = [[abs(A[h][w]-B[h][w]) for w in range(W)] for h in range(H)]
dp = [[{} for _ in range(W)] for _ in range(H)]
dp[0][0][D[0][0]] = 0
for h in range(H):
    for w in range(W):
        for v in dp[h][w]:
            if w != W-1:
                v1 = v+D[h][w+1]
                v2 = v-D[h][w+1]
                if v1<0:
                    v1 *= -1
                if v2<0:
                    v2 *= -1
                dp[h][w+1][v1] = 0
                dp[h][w+1][v2] = 0
            if h != H-1:
                v1 = v+D[h+1][w]
                v2 = v-D[h+1][w]
                if v1<0:
                    v1 *= -1
                if v2<0:
                    v2 *= -1
                dp[h+1][w][v1] = 0
                dp[h+1][w][v2] = 0
ans = min(dp[H-1][W-1])
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque,defaultdict
N,X,D = map(int,input().split())
d = defaultdict(list)
if D<0:
    D *= -1
    X *= -1

if D != 0:
    for m in range(N+1):
        if m==0:
            a = 0
            b = 0
        else:
            a = X*m+(m*(m-1)*D)//2
            b = X*m+(m*(2*N-m-1)*D)//2
        d[a%D].append([a,b])


    ans = 0
    for k in d:
        q = d[k]
        q.sort()
        q = deque(q)
        while q:
            a1,b1 = q.popleft()
            if q:
                a2,b2 = q.popleft()
                if a2 <= b1:
                    q.appendleft([a1,b2])
                else:
                    ans += (b1-a1)//D+1
                    l.append([a1,b1])
                    q.appendleft([a2,b2])
            else:
                ans += (b1-a1)//D+1

if D == 0:
    ans = N
    if X == 0:
        ans = 1
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque,defaultdict
N,X,D = map(int,input().split())
d = defaultdict(list)
if D<0:
    D *= -1
    X *= -1

for m in range(N+1):
    if m==0:
        a = 0
        b = 0
    else:
        a = X*m+(m*(m-1)*D)//2
        b = X*m+(m*(2*N-m-1)*D)//2
    d[a%D].append([a,b])


ans = 0
for k in d:
    q = d[k]
    q.sort()
    q = deque(q)
    while q:
        a1,b1 = q.popleft()
        if q:
            a2,b2 = q.popleft()
            if a2 <= b1:
                q.appendleft([a1,b2])
            else:
                ans += (b1-a1)//D+1
                l.append([a1,b1])
                q.appendleft([a2,b2])
        else:
            ans += (b1-a1)//D+1


print(ans)