Problem p02599 (python)

Fixed submission:
import numpy as np

def solve(inp):
    def bit_add(arr, n, i, x):
        while i <= n:
            arr[i] += x
            i += i & -i

    def bit_sum(arr, i):
        result = 0
        while i > 0:
            result += arr[i]
            i ^= i & -i
        return result

    N, Q = inp[:2]
    C = inp[2: 2+N]
    L, R = inp[2+N::2] - 1, inp[3+N::2] - 1

    just_left = {}
    segments = []
    for i in range(N):
        c = C[i]
        if c in just_left:
            segments.append((i, 0, just_left[c], 0))
        just_left[c] = i

    for i in range(Q):
        segments.append((R[i], 1, L[i], i))

    segments.sort()
    bit = np.zeros(N + 2, dtype=np.int64)
    ans = np.zeros(Q, dtype=np.int64)

    for r, tp, l, i in segments:
        if tp == 0:
            bit_add(bit, N + 2, l + 2, 1)
        else:
            base = r - l + 1
            ans[i] = base - (bit_sum(bit, r + 2) - bit_sum(bit, l + 1))

    return ans

def main():
    inp = np.fromstring(open(0).read(), dtype=np.int64, sep=' ')
    ans = solve(inp)
    print("\n".join(map(str,ans)))
    

def cc_export():
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('solve', '(i8[:],)')(solve)
    cc.compile()

if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        exit(0)
    from my_module import solve
    main()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
import sys
input = sys.stdin.readline
print = sys.stdout.write

class FenwickTree:
    def __init__(self, n):
        self.n = n
        self.dat = [0] * n
        
    def add(self, i, x):
        while i < self.n:
            self.dat[i] += x
            i += i & (-i)
    def sum(self, i):
        res = 0
        while i > 0:
            res += self.dat[i]
            i -= i & (-i)
        return res
    def rangesum(self, l, r):
        return self.sum(r) - self.sum(l-1)

N, Q = map(int, input().split())
C = [0] + list(map(int,input().split()))

ql, qid = defaultdict(list), defaultdict(list)

for i in range(Q):
    l, r = map(int, input().split())
    ql[r].append(l)
    qid[r].append(i)

A = [0] * (N+1)
ANS = [0] * (N+1)

FT = FenwickTree(N+1)

for i in range(1, N+1):
    if A[C[i]]:
        FT.add(A[C[i]], -1)
    FT.add(i, +1)
    A[C[i]] = i
    for j in range(len(ql[i])):
        ANS[qid[i][j]] = FT.rangesum(ql[i][j],i)

print("\n".join(map(str,ANS[:Q])))

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
import sys
input = sys.stdin.readline

class FenwickTree:
    def __init__(self, n):
        self.n = n
        self.dat = [0] * n
        
    def add(self, i, x):
        while i < self.n:
            self.dat[i] += x
            i += i & (-i)
    def sum(self, i):
        res = 0
        while i > 0:
            res += self.dat[i]
            i -= i & (-i)
        return res
    def rangesum(self, l, r):
        return self.sum(r) - self.sum(l-1)

N, Q = map(int, input().split())
C = [0] + list(map(int,input().split()))

ql, qid = defaultdict(list), defaultdict(list)

for i in range(Q):
    l, r = map(int, input().split())
    ql[r].append(l)
    qid[r].append(i)

A = [0] * (N+1)
ANS = [0] * (N+1)

FT = FenwickTree(N+1)

for i in range(1, N+1):
    if A[C[i]]:
        FT.add(A[C[i]], -1)
    FT.add(i, +1)
    A[C[i]] = i
    for j in range(len(ql[i])):
        ANS[qid[i][j]] = FT.rangesum(ql[i][j],i)

for ans in ANS[:Q]:
    print(ans)