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

characters = [chr(ord('a') + i) for i in range(26)]

def main():
    N = int(input())
    S = input()
    Q = int(input())

    indices_dict = {a: [] for a in characters}
    for i, s in enumerate(S):
        indices_dict[s].append(i + 1)

    for i in range(Q):
        a, b, c = input().split()
        if int(a) == 2:
            l, r = int(b), int(c)
            cnt = 0
            for c in characters:
                if len(indices_dict[c]) > 0 and \
                    l <= indices_dict[c][-1] and \
                    indices_dict[c][bisect_left(indices_dict[c], l)] <= r:
                    cnt += 1
            print(cnt)
        else:
            from_c = S[ind - 1]
            ind, to_c = int(b), c
            if from_c == to_c:
                continue
            insort_left(indices_dict[to_c], ind)
            indices_dict[from_c].pop(bisect_left(indices_dict[from_c], ind))
            S[ind] = to_c

if __name__ == "__main__":
    main()


Fixed submission:
from bisect import insort_left, bisect_left
n = int(input())
s = list(input())
Q = int(input())
query = [input() for _ in range(Q)]
dic = {chr(i+ord('a')):[] for i in range(26)}
for ind, c in enumerate(s):
    dic[c].append(ind)

for q in query:
    t, a, b = q.split()
    a = int(a)-1
    if t=='1':
        if s[a]==b:
            continue
        ind = bisect_left(dic[s[a]], a)
        dic[s[a]].pop(ind)
        insort_left(dic[b], a)
        s[a] = b
    else:
        b = int(b)-1
        tmp = 0
        for li in dic.values():
            if li and a<=li[-1] and li[bisect_left(li, a)] <= b:
                tmp += 1
        print(tmp)

Buggy python submission (verdict: Runtime Error):
import bisect

characters = [chr(ord('a') + i) for i in range(26)]

def main():
    N = int(input())
    S = input()
    Q = int(input())

    indices_dict = {a: [] for a in characters}
    for i, s in enumerate(S):
        indices_dict[s].append(i + 1)

    for i in range(Q):
        a, b, c = input().split()
        if int(a) == 2:
            l, r = int(b), int(c)
            cnt = 0
            for c in characters:
                l_ind = bisect.bisect_left(indices_dict[c], l)
                r_ind = bisect.bisect_right(indices_dict[c], r)
                if l_ind != r_ind:
                    cnt += 1
            print(cnt)
        else:
            ind, to_c = int(b), c
            bisect.insort_left(indices_dict[to_c], ind)
            from_c = S[ind - 1]
            indices_dict[from_c].pop(bisect.bisect_left(indices_dict[from_c], ind))

if __name__ == "__main__":
    main()

Fixed submission:
from bisect import insort_left, bisect_left
n = int(input())
s = list(input())
Q = int(input())
query = [input() for _ in range(Q)]
dic = {chr(i+ord('a')):[] for i in range(26)}
for ind, c in enumerate(s):
    dic[c].append(ind)

for q in query:
    t, a, b = q.split()
    a = int(a)-1
    if t=='1':
        if s[a]==b:
            continue
        ind = bisect_left(dic[s[a]], a)
        dic[s[a]].pop(ind)
        insort_left(dic[b], a)
        s[a] = b
    else:
        b = int(b)-1
        tmp = 0
        for li in dic.values():
            if li and a<=li[-1] and li[bisect_left(li, a)] <= b:
                tmp += 1
        print(tmp)