Problem p02620 (python)

Fixed submission:
from bisect import bisect_left,insort_left

#入力
D = int(input())
C = [int(c) for c in input().split()]
S = []
for _ in range(D):
    s = [int(x) for x in input().split()]
    S.append(s)
T = []
for _ in range(D):
    t = int(input()) - 1
    T.append(t)
    
#スコア計算
score = 0
last_day = [[-1] for _ in range(26)]
for d in range(D):
    test = T[d]
    score += S[d][test]
    last_day[test].append(d)
minus = [0] * 26
for i in range(26):
    last_day[i].append(D)
    l = last_day[i]
    for j in range(len(l)-1):
        dd = l[j+1] - l[j] - 1
        minus[i] += C[i] * (dd * (dd + 1)) // 2
    score -= minus[i]

M = int(input())
for _ in range(M):
    d,q = map(int,input().split())
    d -= 1
    q -= 1
    old = T[d]
    T[d] = q
    #日程dでの元のテストold
    l = last_day[old]
    ind_old = l.index(d)
    dd_left = l[ind_old] - l[ind_old-1] - 1
    dd_right = l[ind_old+1] - l[ind_old] - 1
    dd = l[ind_old+1] - l[ind_old-1] - 1
    dif_old = C[old] * (((dd_left * (dd_left + 1)) // 2) + ((dd_right * (dd_right + 1)) // 2) - ((dd * (dd + 1)) // 2))
    #日程dでのテストq
    l = last_day[q]
    ind_q = bisect_left(last_day[q],d)
    dd_left = d - l[ind_q-1] - 1
    dd_right = l[ind_q] - d - 1
    dd = l[ind_q] - l[ind_q-1] - 1
    dif_q = C[q] * (((dd * (dd + 1)) // 2) - ((dd_left * (dd_left + 1)) // 2) - ((dd_right * (dd_right + 1)) // 2))
    #新しいスコア
    score += (dif_old + dif_q - S[d][old] + S[d][q])
    #情報の更新
    minus[old] -= dif_old
    minus[q] -= dif_q
    del last_day[old][ind_old]
    last_day[q].insert(ind_q,d)
    print(score)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left,insort_left

#入力
D = int(input())
C = [int(c) for c in input().split()]
S = []
for _ in range(D):
    s = [int(x) for x in input().split()]
    S.append(s)
T = []
for _ in range(D):
    t = int(input()) - 1
    T.append(t)
    
#スコア計算
score = 0
last_day = [[-1] for _ in range(26)]
for d in range(D):
    test = T[d]
    score += S[d][test]
    last_day[test].append(d)
minus = [0] * 26
for i in range(26):
    last_day[i].append(D)
    l = last_day[i]
    for j in range(len(l)-1):
        dd = l[j+1] - l[j] - 1
        minus[i] += C[i] * (dd * (dd + 1)) // 2
    score -= minus[i]

M = int(input())
for _ in range(M):
    d,q = map(int,input().split())
    d -= 1
    q -= 1
    old = T[d]
    T[d] = q
    #日程dでの元のテストold
    l = last_day[old]
    ind_old = l.index(d)
    dd_left = l[ind_old] - l[ind_old-1] - 1
    dd_right = l[ind_old+1] - l[ind_old] - 1
    dd = l[ind_old+1] - l[ind_old-1] - 1
    dif_old = C[old] * (((dd_left * (dd_left + 1)) // 2) + ((dd_right * (dd_right + 1)) // 2) - ((dd * (dd + 1)) // 2))
    #日程dでのテストq
    ind_q = bisect_left(last_day[q],d)
    dd_left = d - l[ind_q-1] - 1
    dd_right = l[ind_q] - d - 1
    dd = l[ind_q] - l[ind_q-1] - 1
    dif_q = C[q] * (((dd * (dd + 1)) // 2) - ((dd_left * (dd_left + 1)) // 2) - ((dd_right * (dd_right + 1)) // 2))
    #新しいスコア
    score += (dif_old + dif_q - S[d][old] + S[d][q])
    #情報の更新
    minus[old] -= dif_old
    minus[q] -= dif_q
    del last_day[old][ind_old]
    last_day[q].insert(ind_q,d)
    print(score)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left,insort_left

#入力
D = int(input())
C = [int(c) for c in input().split()]
S = []
for _ in range(D):
    s = [int(x) for x in input().split()]
    S.append(s)
T = []
for _ in range(D):
    t = int(input()) - 1
    T.append(t)
    
#スコア計算
score = 0
last_day = [[-1] for _ in range(26)]
for t in range(D):
    test = T[t]
    score += S[t][test]
    last_day[test].append(t)
minus = [0] * 26
for i in range(26):
    last_day[i].append(D)
    l = last_day[i]
    for j in range(len(l)-1):
        dd = l[j+1] - l[j] - 1
        minus[i] += C[i] * (dd * (dd + 1)) // 2
    score -= minus[i]

M = int(input())
for _ in range(M):
    d,q = map(int,input().split())
    d -= 1
    q -= 1
    old = T[d]
    T[d] = q
    #日程dでの元のテストold
    l = last_day[old]
    ind_old = l.index(d)
    dd_left = l[ind_old] - l[ind_old-1] - 1
    dd_right = l[ind_old+1] - l[ind_old] - 1
    dd = l[ind_old+1] - l[ind_old-1] - 1
    dif_old = C[old] * (((dd_left * (dd_left + 1)) // 2) + ((dd_right * (dd_right + 1)) // 2) - ((dd * (dd + 1)) // 2))
    #日程dでのテストq
    ind_q = bisect_left(last_day[q],d)
    dd_left = d - l[ind_q] - 1
    dd_right = l[ind_q+1] - d - 1
    dd = l[ind_q+1] - l[ind_q] - 1
    dif_q = C[q] * (((dd * (dd + 1)) // 2) - ((dd_left * (dd_left + 1)) // 2) - ((dd_right * (dd_right + 1)) // 2))
    #新しいスコア
    score += (dif_old + dif_q - S[d][old] + S[d][q])
    #情報の更新
    minus[old] -= dif_old
    minus[q] -= dif_q
    del last_day[old][ind_old]
    last_day[q].insert(ind_q,d)
    print(score)