Problem p02763 (python)

Fixed submission:
import bisect
n = int(input())
s = list(input())
alp = 'abcdefghijklmnopqrstuvwxyz'
cnt = {c: [] for c in alp}

for i, c in enumerate(s):
    cnt[c].append(i)

q = int(input())
for _ in range(q):
    t, x, y = input().split()
    ans = 0
    if t == '1':
        i, c = int(x) - 1, y
        if s[i] == c:
            continue
        a = bisect.bisect_left(cnt[s[i]], i)
        cnt[s[i]].pop(a)
        s[i] = c
        b = bisect.bisect_left(cnt[c], i)
        cnt[c].insert(b, i)
    else:
        l, r = int(x) - 1, int(y) - 1
        for c in alp:
            a = bisect.bisect_left(cnt[c], l)
            if a < len(cnt[c]) and cnt[c][a] <= r:
                ans += 1

        print(ans)

Buggy submission (verdict: Runtime Error):
import heapq, bisect
n = int(input())
s = list(input())
alp = 'abcdefghijklmnopqrstuvwxyz'
cnt = {c: set() for c in alp}

for i in range(n):
    cnt[s[i]].add(i)

q = int(input())
for _ in range(q):
    t, x, y = input().split()
    ans = 0
    if t == '1':
        i, c = int(x) - 1, y
        cnt[s[i]].remove(i)
        cnt[c].add(i)
    else:
        l, r = int(x) - 1, int(y) - 1
        for c in alp:
            sort_cnt = sorted(cnt[c])
            a = bisect.bisect_left(sort_cnt, l)
            if a < len(cnt[c]) and sort_cnt[a] <= r:
                ans += 1
        print(ans)

Buggy submission (verdict: Runtime Error):
import heapq, bisect
n = int(input())
s = list(input())
alp = 'abcdefghijklmnopqrstuvwxyz'
cnt = {c: [] for c in alp}

for i in range(n):
    heapq.heappush(cnt[s[i]], i)

q = int(input())
for _ in range(q):
    t, x, y = input().split()
    ans = 0
    if t == '1':
        i, c = int(x) - 1, y
        cnt[s[i]].remove(i)
        heapq.heappush(cnt[c], i)
    else:
        l, r = int(x) - 1, int(y) - 1
        for c in alp:
            a = bisect.bisect_left(cnt[c], l)
            if a < len(cnt[c]) and cnt[c][a] <= r:
                ans += 1
        print(ans)

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

n=int(input())
s=input()
q=int(input())

a='abcdefghijklmnopqrstuvwxyz'
ch={i:[] for i in a}

for i in range(n):
    insort_left(ch[s[i]],i)
    
for i in range(q):
    m,l,r=input().split()
    if m=='1':
        l=int(l)-1
        x=bisect_left(ch[s[l]],l)
        ch[s[l]].pop(x)
        insort_left(ch[r],l)
    else:
        l,r=int(l)-1,int(r)-1
        res=0
        for i in a:
            y=bisect_left(ch[i],l)
            if y>=len(ch[i]):
                continue
            else:
                if ch[i][y]<=r:
                  res+=1
        print(res)

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

n=int(input())
s=input()
q=int(input())

a='abcdefghijklmnopqrstuvwxyz'
ch={i:[-1,n+1] for i in a}

for i in range(n):
    insort_left(ch[s[i]],i)
    
for i in range(q):
    m,l,r=input().split()
    if m=='1':
        l=int(l)-1
        x=bisect_left(ch[s[l]],l)
        ch[s[l]].pop(x)
        insort_left(ch[r],l)
    else:
        l,r=int(l)-1,int(r)-1
        res=0
        for i in a:
            y=bisect_left(ch[i],l)
            if y>=len(ch[i]):
                continue
            else:
                if ch[i][y]<=r:
                  res+=1
        print(res)

Buggy submission (verdict: Runtime Error):
import bisect
n=int(input())
s=input()
q=int(input())
ch={i:[-1,n+1] for i in 'abcdefghijklmnopqrstuvwxyz'}

for i in range(n):
    bisect.insort(ch[s[i]],i)
    
for i in range(q):
    m,l,r=input().split()
    if m=='1':
        l=int(l)-1
        ch[s[l]].remove(l)
        bisect.insort(ch[r],l)
    else:
        l,r=int(l)-1,int(r)-1
        res=0
        for i in 'abcdefghijklmnopqrstuvwxyz':
            if len(ch[i])>0:
                res+=(ch[i][bisect.bisect_left(ch[i],l)]<=r)
        print(res)