Problem p02599 (python)

Fixed submission:
from collections import defaultdict
import sys
def read():
    return sys.stdin.readline().rstrip()
def main():
    N, Q = map(int, read().split())
    C = list(map(int, read().split()))
    E = defaultdict(list)

    D = [-1] * (N + 1)
    for i in range(N):
        D_i = D[C[i]]
        if D_i != -1:
            E[D_i].append((D_i, i))
        D[C[i]] = i

    F = defaultdict(list)
    for i in range(Q):
        l, r = map(int, input().split())
        F[l-1].append((r - 1, i))



    # Binary Indexed Tree (Fenwick Tree)
    class BIT:
        def __init__(self, n):
            self.n = n
            self.data = [0] * (n + 1)
            self.el = [0] * (n + 1)

        def sum(self, i):
            s = 0
            while i > 0:
                s += self.data[i]
                i -= i & -i
            return s

        def add(self, i, x):
            # assert i > 0
            self.el[i] += x
            while i <= self.n:
                self.data[i] += x
                i += i & -i

        def get(self, i, j=None):
            if j is None:
                return self.el[i]
            return self.sum(j) - self.sum(i)

    ANS = [0] * Q
    d = BIT(N)
    for x in range(N - 1, -1, -1):
        for e in E[x]:
            d.add(e[1], 1)
        for f in F[x]:
            ANS[f[1]] = f[0] - x + 1 - d.sum(f[0])

    for i in range(Q):
        print(ANS[i])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from operator import itemgetter
import sys
def read():
    return sys.stdin.readline().rstrip()
def main():
    N, Q = map(int, read().split())
    C = list(map(int, read().split()))
    E = []

    D = [-1] * N
    for i in range(N):
        D_i = D[C[i]]
        if D_i != -1:
            E.append((D_i, i, -1))
        D[C[i]] = i

    for i in range(Q):
        l, r = map(int, input().split())
        E.append((l - 1, r - 1, i))

    def f(tup):
        return tup[0]
    E.sort(key=f, reverse=True)

    # Binary Indexed Tree (Fenwick Tree)
    class BIT:
        def __init__(self, n):
            self.n = n
            self.data = [0] * (n + 1)
            self.el = [0] * (n + 1)

        def sum(self, i):
            s = 0
            while i > 0:
                s += self.data[i]
                i -= i & -i
            return s

        def add(self, i, x):
            # assert i > 0
            self.el[i] += x
            while i <= self.n:
                self.data[i] += x
                i += i & -i

        def get(self, i, j=None):
            if j is None:
                return self.el[i]
            return self.sum(j) - self.sum(i)

    ANS = [0] * Q
    d = BIT(N)
    for e in E:
        if e[2] == -1:
            d.add(e[1], 1)
        else:
            ANS[e[2]] = e[1] - e[0] + 1 - d.sum(e[1])

    for i in range(Q):
        print(ANS[i])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import bisect
def main():
    N, Q = map(int, input().split())
    C = list(map(int, input().split()))
    E = []

    D = {C[i]: -1 for i in range(N)}
    for i in range(N):
        if D[C[i]] == -1:
            D[C[i]] = i
        else:
            E.append((D[C[i]], i, -1))
            D[C[i]] = i

    for i in range(Q):
        l, r = map(int, input().split())
        E.append((l - 1, r - 1, i))

    def f(tup):
        return tup[0]
    E.sort(key=f, reverse=True)

    ANS = [0] * Q
    y_list = [500005]
    for e in E:
        ans = bisect.bisect_right(y_list, e[1])
        if e[2] == -1:
            y_list[ans] = e[1]
        else:
            ANS[e[2]] = e[1] - e[0] + 1 - ans

    for i in range(Q):
        print(ANS[i])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import bisect
N, Q = map(int, input().split())
C = list(map(int, input().split()))
L = [0] * N
R = [0] * N
E = []

D = {C[i]: -1 for i in range(N)}
for i in range(N):
    if D[C[i]] == -1:
        D[C[i]] = i
    else:
        E.append((D[C[i]], i, -1))
        D[C[i]] = i

for i in range(Q):
    l, r = map(int, input().split())
    L[i] = l
    R[i] = r
    E.append((l - 1, r - 1, i))

def f(tup):
    return tup[0]
E.sort(key=f, reverse=True)

ANS = [0] * Q
y_list = []
for e in E:
    if e[2] == -1:
        y_list.append(e[1])
        y_list.sort()
    else:
        ans = bisect.bisect_right(y_list, e[1])
        ANS[e[2]] = e[1] - e[0] + 1 - ans

for i in range(Q):
    print(ANS[i])

Buggy submission (verdict: Runtime Error):
import bisect
N, Q = map(int, input().split())
C = list(map(int, input().split()))
L = [0] * N
R = [0] * N
E = []

D = {C[i]: -1 for i in range(N)}
for i in range(N):
    if D[C[i]] == -1:
        D[C[i]] = i
    else:
        E.append((D[C[i]], i, -1))
        D[C[i]] = i

for i in range(Q):
    l, r = map(int, input().split())
    L[i] = l
    R[i] = r
    E.append((l - 1, r - 1, i))

def f(tup):
    return tup[0]
E.sort(key=f, reverse=True)

ANS = [0] * Q
y_list = []
for e in E:
    if e[2] == -1:
        y_list.append(e[1])
        y_list.sort()
    else:
        ans = bisect.bisect_right(y_list, e[1])
        ANS[e[2]] = e[1] - e[0] + 1 - ans

for i in range(Q):
    print(ANS[i])