Problem p02763 (python)

Fixed submission:
import sys
from bisect import bisect_left, bisect_right, insort

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
S = list('-' + sr())
d = [[] for _ in range(26)]
for i in range(1, N+1):
    s = S[i]
    o = ord(s) - ord('a')
    insort(d[o], i)

Q = ir()
for _ in range(Q):
    q, a, b = sr().split()
    if q == '1':
        a = int(a)
        if S[a] == b:
            continue
        prev = ord(S[a]) - ord('a')
        d[prev].remove(a)
        next = ord(b) - ord('a')
        insort(d[next], a)
        S[a] = b
    else:
        a = int(a); b = int(b)
        ans = 0
        for alpha in range(26):
            if bisect_right(d[alpha], b) - bisect_left(d[alpha], a) >= 1:
                ans += 1
        print(ans)

# 17

Buggy submission (verdict: Runtime Error):
# coding: utf-8
import sys
from bisect import bisect_left, bisect_right, insort_left
from collections import defaultdict

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 各アルファベットのindexのリストを作り,bisect_left()
N = ir()
S = sr()
dic = defaultdict(list)
for i, s in enumerate(S):
    dic[s].append(i)

Q = ir()
for _ in range(Q):
    a, b, c = sr().split()
    if a == '1':
        b = int(b) - 1
        if S[b] == c:
            continue
        dic[S[b]].remove(b)
        insort_left(dic[c], b)
    else:
        b = int(b) - 1; c = int(c) - 1
        kind = 0
        for x in range(26):
            word = chr(ord('a')+x)
            if len(dic[word]) == 0:
                continue
            i = bisect_left(dic[word], b)
            j = bisect_right(dic[word], c)
            if i != j:
                kind += 1
        print(kind)

# 07

Buggy submission (verdict: Runtime Error):
# coding: utf-8
import sys
from bisect import bisect_left, bisect_right, insort_left
from collections import defaultdict

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 各アルファベットのindexのリストを作り,bisect_left()
N = ir()
S = sr()
dic = defaultdict(list)
for i, s in enumerate(S):
    dic[s].append(i)

Q = ir()
for _ in range(Q):
    a, b, c = sr().split()
    if a == '1':
        b = int(b) - 1
        if S[b] == c:
            continue
        dic[S[b]].remove(b)
        insort_left(dic[c], b)
    else:
        b = int(b) - 1; c = int(c) - 1
        kind = 0
        for x in range(26):
            word = chr(ord('a')+x)
            i = bisect_left(dic[word], b)
            j = bisect_right(dic[word], c)
            if i != j:
                kind += 1
        print(kind)

# 07

Buggy submission (verdict: Runtime Error):
import sys
from bisect import bisect_left, bisect_right, insort

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
S = '-' + sr()
d = [[] for _ in range(26)]
for i in range(1, N+1):
    s = S[i]
    o = ord(s) - ord('a')
    insort(d[o], i)

Q = ir()
for _ in range(Q):
    q, a, b = sr().split()
    if q == '1':
        a = int(a)
        if S[a] == b:
            continue
        prev = ord(S[a]) - ord('a')
        d[prev].remove(a)
        next = ord(b) - ord('a')
        insort(d[next], a)
    else:
        a = int(a); b = int(b)
        ans = 0
        for alpha in range(26):
            if bisect_right(d[alpha], b) - bisect_left(d[alpha], a) >= 1:
                ans += 1
        print(ans)

# 17

Buggy submission (verdict: Runtime Error):
import sys
from bisect import bisect_left, bisect_right, insort
from collections import defaultdict

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
S = '-' + sr()
d = defaultdict(list)
for i in range(1, N+1):
    s = S[i]
    insort(d[s], i)

Q = ir()
for _ in range(Q):
    q, a, b = sr().split()
    if q == '1':
        a = int(a)
        if S[a] == b:
            continue
        prev = S[a]
        d[prev].remove(a)
        insort(d[b], a)
    else:
        a = int(a); b = int(b)
        ans = 0
        for alpha in d:
            if bisect_right(d[alpha], b) - bisect_left(d[alpha], a) >= 1:
                ans += 1
        print(ans)

# 17

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
S = list('-' + sr())
d = defaultdict(set)
for i in range(1, N+1):
    d[S[i]].add(i)

#print(d)
Q = ir()
for _ in range(Q):
    a, b, c = sr().split()
    if a == '1':
        b = int(b)
        s = S[b]
        d[s].remove(b)
        d[c].add(b)
    else:
        b = int(b); c = int(c)
        ans = 0
        for x in d:
            for y in d[x]:
                if b <= y <= c:
                    ans += 1
                    break
        print(ans)

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
S = list('-' + sr())
d = defaultdict(set)
for i in range(1, N+1):
    d[S[i]].add(i)

#print(d)
Q = ir()
for _ in range(Q):
    a, b, c = sr().split()
    if a == '1':
        b = int(b)
        s = S[b]
        d[s].remove(b)
        d[c].add(b)
    else:
        b = int(b); c = int(c)
        ans = 0
        for x in d:
            for y in d[x]:
                if b <= y <= c:
                    ans += 1
                    break
        print(ans)