Problem p04017 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 6)  # 変更
from bisect import bisect_left,bisect

n = int(input())
x = list(map(int,input().split()))
L = int(input())
t = n.bit_length()
prv = [n-1] * n
for i in range(n):
    prv[i] = bisect(x,x[i]+L) - 1
kprv = [prv]
S = prv
for i in range(t-1):
    kar = [n-1] * n
    for j in range(n):
        kar[j] = S[S[j]]
    kprv.append(kar)
    S = kar

q = int(input())
for _ in range(q):
    a , b = map(lambda x:int(x)-1,input().split())
    if a > b:
        a , b = b , a
    lef = 0
    rig = n-1
    while rig - lef > 1:
        mid = (rig+lef)//2
        p = mid.bit_length()
        now = a
        for i in range(p):
            if (mid>>i) & 1 == 1:
                now = kprv[i][now]
        if now >= b:
            rig = mid
        elif now < b:
            lef = mid
    print(rig)

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    sys.setrecursionlimit(10 ** 6)
    from bisect import bisect_left,bisect
    n = int(input())
    x = list(map(int,input().split()))
    t = n.bit_length()

    kprv = [[10**10 for i in range(t+1)] for j in range(n)]
    for i in range(t):
        for j in range(n):
            if j + pow(2,i) > n-1:
                break
            kprv[j][i] = x[j+pow(2**i)] - x[j]

    L = int(input())
    q = int(input())

    for _ in range(q):
        a , b = map(lambda x:int(x)-1,input().split())
        cou = 0
        if a > b:
            a , b = b , a
        now = a
        while now < b:
            cou += 1
            p = L
            while True:
                nex = bisect(kprv[now],p)
                if nex == 0:
                    break
                else:
                    p -= kprv[now][nex-1]
                    now += pow(2,nex-1)
        print(cou)

main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 6)  # 変更
from bisect import bisect_left,bisect

n = int(input())
x = list(map(int,input().split()))
L = int(input())
t = n.bit_length()
prv = [n-1] * n
for i in range(n):
    prv[i] = bisect(x,x[i]+L) - 1
kprv = [prv]
S = prv
for i in range(t-1):
    kar = [n-1] * n
    for j in range(n):
        kar[j] = S[S[j]]
    kprv.append(kar)
    S = kar

dub = []
for i in range(n):
    kar = []
    for j in range(len(kprv)):
        kar.append(kprv[j][i])
    dub.append(kar)

q = int(input())
for _ in range(q):
    a , b = map(lambda x:int(x)-1,input().split())
    if a > b:
        a , b = b , a
    now = a
    ans = 0
    while True:
        p = bisect_left(dub[now],b)
        if dub[now][p] == b:
            ans += pow(2,p)
            print(ans)
            break
        else:
            ans += pow(2,p-1)
            now = dub[now][p-1]

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 6)  # 変更

from bisect import bisect_left,bisect
n = int(input())
x = list(map(int,input().split()))
t = (n-1).bit_length()
kprv = [[10**10 for i in range(t)] for j in range(n-1)]
for i in range(t):
    for j in range(n):
        if j + pow(2,i) > n-1:
            break
        kprv[j][i] = x[j+2**i] - x[j]

L = int(input())
q = int(input())

for _ in range(q):
    a , b = map(lambda x:int(x)-1,input().split())
    cou = 0
    if a > b:
        a , b = b , a
    now = a
    while now < b:
        cou += 1
        p = L
        while True:
            nex = bisect(kprv[now],p)
            if nex == 0:
                break
            else:
                p -= kprv[now][nex-1]
                now += pow(2,nex-1)
    print(cou)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 6)  # 変更

from bisect import bisect_left,bisect
n = int(input())
x = list(map(int,input().split()))
t = (n-1).bit_length()
kprv = [[10**10 for i in range(t)] for j in range(n-1)]
for i in range(t):
    for j in range(n):
        if j + 2**i > n-1:
            break
        kprv[j][i] = x[j+2**i] - x[j]

L = int(input())
q = int(input())

for _ in range(q):
    a , b = map(lambda x:int(x)-1,input().split())
    cou = 0
    if a > b:
        a , b = b , a
    now = a
    while now < b:
        cou += 1
        p = L
        while True:
            nex = bisect(kprv[now],p)
            if nex == 0:
                break
            else:
                p -= kprv[now][nex-1]
                now += 2**(nex-1)
    print(cou)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 6)  # 変更

from bisect import bisect_left,bisect
n = int(input())
x = list(map(int,input().split()))
t = (n-1).bit_length()
kprv = [[10**10 for i in range(t)] for j in range(n-1)]
for i in range(t):
    for j in range(n):
        if j + 2**i > n-1:
            break
        kprv[j][i] = x[j+2**i] - x[j]

L = int(input())
q = int(input())

for _ in range(q):
    a , b = map(lambda x:int(x)-1,input().split())
    cou = 0
    if a > b:
        a , b = b , a
    now = a
    while now < b:
        cou += 1
        p = L
        while True:
            nex = bisect(kprv[now],p)
            if nex == 0:
                break
            else:
                p -= kprv[now][nex-1]
                now += 2**(nex-1)
    print(cou)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left,bisect
n = int(input())
x = list(map(int,input().split()))
t = (n-1).bit_length()
kprv = [[10**10 for i in range(t)] for j in range(n-1)]
for i in range(t):
    for j in range(n):
        if j + 2**i > n-1:
            break
        kprv[j][i] = x[j+2**i] - x[j]

L = int(input())
q = int(input())

for _ in range(q):
    a , b = map(lambda x:int(x)-1,input().split())
    cou = 0
    if a > b:
        a , b = b , a
    now = a
    while now < b:
        cou += 1
        p = L
        while True:
            nex = bisect(kprv[now],p)
            if nex == 0:
                break
            else:
                p -= kprv[now][nex-1]
                now += 2**(nex-1)
    print(cou)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left,bisect
n = int(input())
x = list(map(int,input().split()))
t = (n-1).bit_length()
kprv = [[10**10 for i in range(t)] for j in range(n-1)]
for i in range(t):
    for j in range(n):
        if j + 2**i > n-1:
            break
        kprv[j][i] = x[j+2**i] - x[j]

L = int(input())
q = int(input())

for _ in range(q):
    a , b = map(lambda x:int(x)-1,input().split())
    cou = 0
    if a > b:
        a , b = b , a
    now = a
    while now < b:
        cou += 1
        p = L
        while True:
            nex = bisect(kprv[now],p)
            if nex == 0:
                break
            else:
                p -= kprv[now][nex-1]
                now += 2**(nex-1)
    print(cou)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left,bisect
n = int(input())
x = list(map(int,input().split()))
t = (n-1).bit_length()
kprv = [[1000 for i in range(t)] for j in range(n-1)]
for i in range(t):
    for j in range(n):
        if j + 2**i > n-1:
            break
        kprv[j][i] = x[j+2**i] - x[j]

L = int(input())
q = int(input())

for _ in range(q):
    a , b = map(lambda x:int(x)-1,input().split())
    cou = 0
    if a > b:
        a , b = b , a
    now = a
    while now < b:
        cou += 1
        p = L
        while True:
            nex = bisect(kprv[now],p)
            if nex == 0:
                break
            else:
                p -= kprv[now][nex-1]
                now += 2**(nex-1)
    print(cou)