Problem p02763 (python)

Fixed submission:
import sys
input=sys.stdin.readline
n=int(input())
BIT=[[0]*n for i in range(26)]
w=[ord(x)-ord("a") for x in input()]
for i in range(n):
	k=i+1
	while k<=n:
		BIT[w[i]][k-1]+=1
		k=k+(k&-k)
q=int(input())
for i in range(q):
	a,b,c=input().split()
	if a=="1":
		k=int(b)
		while k<=n:
			BIT[ord(c)-ord("a")][k-1]+=1
			k=k+(k&-k)
		k=int(b)
		while k<=n:
			BIT[w[int(b)-1]][k-1]-=1
			k=k+(k&-k)	
		w[int(b)-1]=ord(c)-ord("a")
	else:
		b=int(b);c=int(c)
		ans=0
		for i in range(26):
			cnt=0
			k=c
			while k!=0:
				cnt+=BIT[i][k-1]
				k=k-(k&-k)
			k=b-1
			while k!=0:
				cnt-=BIT[i][k-1]
				k=k-(k&-k)
			if cnt>0:
				ans+=1
		print(ans)

Buggy submission (verdict: Runtime Error):
#query=10**4*2
#alph=26
#insert=500000
#O(|insert*query*alph|)?
def main():
	import bisect,sys
	input=sys.stdin.readline
	n=int(input())
	s=list(input().rstrip())
	q=int(input())
	word=[[]for _ in range(26)]
	for i,j in enumerate(s):
		ch=ord(j)-ord("a")
		word[ch].append(i)
	for _ in range(q):
		query=input().split()
		if query[0]=="1":
			i,x=int(query[1])-1,ord(query[2])-ord("a")
			old=ord(s[i])-ord("a")
			word[old].remove(i)
			new=bisect.bisect_left(word[x],i)
			word[x].insert(new,i)
			s[i]=x
		# else:
		# 	l,r=int(query[1])-1,int(query[2])-1
		# 	kind=0
		# 	for i in range(26):
		# 		l=bisect.bisect_left(word[i],l)
		# 		if l==len(word[i]):
		# 			continue
		# 		if word[i][l]<=r:
		# 			kind+=1
		# 	print(kind)
if __name__ == '__main__':
	main()

Buggy submission (verdict: Runtime Error):
#query=10**4*2
#alph=26
#insert=500000
#O(|insert*query*alph|)?
def main():
	import bisect,sys
	input=sys.stdin.readline
	n=int(input())
	s=list(input().rstrip())
	q=int(input())
	word=[[]for _ in range(26)]
	for i,j in enumerate(s):
		ch=ord(j)-ord("a")
		word[ch].append(i)
	for _ in range(q):
		query=input().split()
		if query[0]=="1":
			i,x=int(query[1])-1,ord(query[2])-ord("a")
			old=ord(s[i])-ord("a")
			word[old].remove(i)
			new=bisect.bisect_left(word[x],i)
			word[x].insert(new,i)
			s[i]=x
		else:
			l,r=int(query[1])-1,int(query[2])-1
			kind=0
			for i in range(26):
				l=bisect.bisect_left(word[i],l)
				if l==len(word[i]):
					continue
				if word[i][l]<=r:
					kind+=1
			print(kind)
if __name__ == '__main__':
	main()

Buggy submission (verdict: Runtime Error):
#query=10**4*2
#alph=26
#insert=500000
#O(|insert*query*alph|)?
def main():
	import bisect
	input=sys.stdin.readline
	n=int(input())
	s=list(input().rstrip())
	q=int(input())
	word=[[]for _ in range(26)]
	for i,j in enumerate(s):
		ch=ord(j)-ord("a")
		word[ch].append(i)
	for _ in range(q):
		query=input().split()
		if query[0]=="1":
			i,x=int(query[1])-1,ord(query[2])-ord("a")
			old=ord(s[i])-ord("a")
			word[old].remove(i)
			new=bisect.bisect_left(word[x],i)
			word[x].insert(new,i)
			s[i]=x
		else:
			l,r=int(query[1])-1,int(query[2])-1
			kind=0
			for i in range(26):
				l=bisect.bisect_left(word[i],l)
				if l==len(word[i]):
					continue
				if word[i][l]<=r:
					kind+=1
			print(kind)
if __name__ == '__main__':
	main()

Buggy submission (verdict: Runtime Error):
#query=10**4*2
#alph=26
#insert=500000
#O(|insert*query*alph|)?
def main():
	import bisect
	input=sys.stdin.readline
	n=int(input())
	s=list(input().rstrip())
	q=int(input())
	word=[[]for _ in range(26)]
	for i,j in enumerate(s):
		ch=ord(j)-ord("a")
		word[ch].append(i)
	for _ in range(q):
		query=input().split()
		if query[0]=="1":
			i,x=int(query[1])-1,ord(query[2])-ord("a")
			old=ord(s[i])-ord("a")
			word[old].remove(i)
			new=bisect.bisect_left(word[x],i)
			word[x].insert(new,i)
			s[i]=x
		else:
			l,r=int(query[1])-1,int(query[2])-1
			kind=0
			for i in range(26):
				l=bisect.bisect_left(word[i],l)
				if l==len(word[i]):
					continue
				if word[i][l]<=r:
					kind+=1
			print(kind)
if __name__ == '__main__':
	main()