Problem:


Score : 500 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

Process Q queries of the following two types:

Type 1: change the i_q-th character of S to c_q. (Do nothing if the i_q-th character is already c_q.)

Type 2: answer the number of different characters occurring in the substring of S between the l_q-th and r_q-th characters (inclusive).

Constraints

N, Q, i_q, l_q, and r_q are integers.

S is a string consisting of lowercase English letters.

c_q is a lowercase English letter.

1 \leq N \leq 500000

1 \leq Q \leq 20000

|S| = N

1 \leq i_q \leq N

1 \leq l_q \leq r_q \leq N

There is at least one query of type 2 in each testcase.

Input

Input is given from Standard Input in the following format:

N
S
Q
Query_1
\vdots
Query_Q

Here, Query_i in the 4-th through (Q+3)-th lines is one of the following:

1 i_q c_q

2 l_q r_q

Output

For each query of type 2, print a line containing the answer.

Sample Input 1

7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7

Sample Output 1

3
1
5

In the first query, cdbb contains three kinds of letters: b , c , and d, so we print 3.

In the second query, S is modified to abcdzbd.

In the third query, a contains one kind of letter: a, so we print 1.

In the fourth query, S is modified to abcazbd.

In the fifth query, S does not change and is still abcazbd.

In the sixth query, abcazbd contains five kinds of letters: a, b, c, d, and z, so we print 5.



Sample input:
7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7


Sample output:
3
1
5

Submission status: Runtime Error

Solution code:
from bisect import bisect_left, bisect_right
N = int(input())
S = input()
Q = int(input())

D = {chr(ord('a') + i): [] for i in range(26)}

for i, s in enumerate(S):
    D[s].append(i)

for q in range(Q):
    query = list(input().split())
    if query[0] == '1':
        i, x = int(query[1]), query[2]
        i -= 1
        s = S[i]
        if s == x:
            continue

        D[s].remove(i)
        next_i = bisect_left(D[x], i)
        D[x].insert(next_i, i)

    if query[0] == '2':
        l, r = int(query[1]), int(query[2])
        l, r = l - 1, r - 1
        ans = 0
        for i in range(26):
            s = chr(ord('a') + i)

            li = bisect_left(D[s], l)
            ri = bisect_right(D[s], r)

            if ri - li > 0:
                ans += 1

        print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
q = int(input())
chars= [[] for i in range(26)]
for i, k in enumerate(s):
    chars[ord(k) - 97].append(i)
for j in range(q):
    n, a, b = input().split()
    if n == '1':
        i = int(a)
        c = b
        t = chars[ord(s[i-1])-97].remove(i-1)
        chars[ord(c)-97].append(i-1)
    if n == '2':
        l = int(a)
        r = int(b)
        t = 0
        for i in range(26):
            u = chars[i]
            if not (u == [] or u[-1] < l-1 or r-1 < u[0]):
                t += 1
        print(t)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(500000)
def input():
    return sys.stdin.readline()[:-1]

class BIT:
            
    def __init__(self, L):
        self.N = len(L)
        #for i,l in enumerate(L):
        #    self.add(i,l)
        self.N0 = 1
        while self.N0 <= self.N:
            self.N0 *= 2
        self.bit = [0]*self.N0

    def add(self, a, w):
        x = a + 1
        for i in range(1000):
            self.bit[x-1] += w
            x += x & -x
            if x > self.N:
                break

    def sum(self, a):
        x = a+1
        ret = 0
        for i in range(1000):
            ret += self.bit[x-1]
            x -= x & -x
            if x <= 0:
                break        
        return ret

    #you can use this function when the BIT has only non-negative values.
    def lower_bound(self, w):
        if w<=0:
            return 0
        x = 0
        k = self.N0
        while k>0:
            if x+k<=self.N:
                if self.bit[x+k-1]<w:
                    w-=self.bit[x+k-1]
                    x+=k
            k//=2
        return x+1



def main():
    #da=BIT([1,2,3])
    #print(da.bit)
    #print(da.sum(0))
    N = int(input())

    B = [BIT([0]*N) for i in range(26)]

    S = list(input())

    for i in range(N):
        B[ord(S[i]) - ord("a")].add(i, 1)

    Q = int(input())

    for i in range(Q):
        a,b,c = input().split()
        if a=="1":
            #print(B[0].bit)
            index = int(b)-1
            c = c
            if not S[i]==c:
                old = ord(S[i]) - ord("a")
                num = ord(c) - ord("a")
                B[num].add(index, 1)
                B[old].add(index, -1)
                S[i] = c
        else:
            #print(B[0].bit)
            #print(B[0].sum(1))
            l = int(b)-1
            r = int(c)-1
            ans = 0
            for j in range(26):
                if l==0:
                    if B[j].sum(r)>0:
                        ans+=1
                else:
                    if B[j].sum(r)-B[j].sum(l-1)>0:
                        ans+=1

            print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        if i == 0:
            return 0
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
aas = "abcdefghijklmnopqrstuvwxyz"
N = int(input().strip())
S = list(input().strip())
Q = int(input().strip())
cbt = dict()
MX = 10**5 + 2
for c in aas:
    cbt[c] = Bit(MX)
    for i, a in enumerate(S):
        if a == c:
            cbt[c].add(i+1, 1)

for _ in range(Q):
    t, i, c = input().split()
    t = int(t)
    if t == 1:
        i = int(i)
        if S[i-1] != c:
            cbt[S[i-1]].add(i, -1)
            cbt[c].add(i, 1)
            S[i-1] = c
    else:
#        print(S)
        l, r = int(i), int(c)
#        print(l, r)
        s = 0
        for a in aas:
            bt = cbt[a]
#            print(a, bt.sum(r),bt.sum(l-1))
            s += (bt.sum(r)-bt.sum(l-1)>0)
        print(s)

Submission status: Runtime Error

Solution code:
from bisect import bisect_left, bisect_right
N = int(input())
S = input()
Q = int(input())

D = {chr(ord('a') + i): [] for i in range(26)}
for i, s in enumerate(S):
    D[s].append(i)

for q in range(Q):
    query = list(input().split())
    if query[0] == '1':
        i, x = int(query[1]), query[2]
        i -= 1
        s = S[i]
        if s == x:
            continue

        D[s].remove(i)
        next_i = bisect_left(D[x], i)
        D[x].insert(next_i, i)

    if query[0] == '2':
        l, r = int(query[1]), int(query[2])
        l, r = l - 1, r - 1
        ans = 0
        for i in range(26):
            s = chr(ord('a') + i)

            li = bisect_left(D[s], l)
            ri = bisect_right(D[s], r)

            if ri - li > 0:
                ans += 1

        print(ans)

print(D)

Submission status: Runtime Error

Solution code:
from collections import Counter
N = int(input())
S = list(str(input()))
Q = int(input())
#print(Counter("abdf"))
for i in range(Q):
  x = str(input())
  if int(x[0]) == 1:
    S[int(x[2]) - 1] = x[4]
  if int(x[0]) == 2:
    l, r = int(x[2]), int(x[4])
    S1 = S[l - 1 : r]
    #print(S1, "yes")
    c = Counter(S1)
    print(len(c))
    
#print(len(S))

Submission status: Runtime Error

Solution code:
from collections import Counter
N = int(input())
S = list(str(input()))
Q = int(input())
#print(Counter("abdf"))
for i in range(Q):
  x = str(input())
  if int(x[0]) == 1:
    S[int(x[2]) - 1] = x[4]
  if int(x[0]) == 2:
    l, r = int(x[2]), int(x[4])
    S1 = S[l - 1 : r]
    #print(S1, "yes")
    c = Counter(S1)
    print(len(c))
    
print(len(S))

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
s = []
A = [[] for i in range(26)]
for i in range(N):
    x = S[i]
    s.append(x)
    n = ord(x) - 97
    A[n].append(i)
Q = int(input())
for i in range(Q):
    q,a,b = map(str,input().split())
    q = int(q)
    a = int(a)
    a -= 1
    if q == 1:
        x = S[a]
        s[a] = b
        A[ord(x)-97].remove(a)
        A[ord(b)-97].append(a)
    if q == 2:
        ans = 0
        b = int(b)
        b -= 1
        for i in range(26):
            B = A[i]
            import bisect
            p = bisect.bisect_left(B,a)
            q = bisect.bisect_right(B,b)
            if q - p > 0:
                ans += 1
        print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [ord(a) - 97 for a in input()]
NN = 5
BIT=[[0]*(2**NN+1) for _ in range(26)]
 
def addbit(t, i, x):
    while i <= 2**NN:
        BIT[t][i] += x
        i += i & (-i)

def getsum(t, i):
    ret = 0
    while i != 0:
        ret += BIT[t][i]
        i -= i&(-i)
    return ret


for i, a in enumerate(A):
    BIT[a][i+1] += 1

for t in range(26):
    B = [0] * (2**NN+1)
    for i in range(1<<NN):
        B[i+1] = B[i] + BIT[t][i]
    for i in range(1<<NN):
        BIT[t][i] = B[i] - B[i - (i & (-i))]

Q = int(input())
for _ in range(Q):
    a, b, c = input().split()
    if a == "1":
        b = int(b)
        c = ord(c) - 97
        t = A[b-1]
        addbit(t, b, -1)
        addbit(c, b, 1)
    else:
        b, c = int(b), int(c)
        s = 0
        for t in range(26):
            s += 1 if getsum(t, c+1) - getsum(t, b) else 0
        print(s)

Submission status: Runtime Error

Solution code:
import bisect
N = int(input())
S = input()

dic = {}
for i in range(N):
    if S[i] in dic:
        dic[S[i]].append(i+1)
    else:
        dic[S[i]] = [i+1]

Q = int(input())
for _ in range(Q):
    a,b,c = map(str, input().split())
    if a == "1":
        b = int(b)
        dic[S[b-1]].remove(b)
        if c in dic:
            idx = bisect.bisect_left(dic[c], b)
            dic[c].insert(idx, b)
        else:
            dic[c] = [b]
    else:
        l = int(b)
        r = int(c)
        cnt = 0
        for key in dic:
            if len(dic[key]) == 0:
                continue
            elif len(dic[key]) == 1:
                if l <= dic[key][0] <= r:
                    cnt += 1
                continue
            elif bisect.bisect_left(dic[key], r)-bisect.bisect_left(dic[key], l) > 0:
                cnt += 1
        print(cnt)

Submission status: Runtime Error

Solution code:
try:
	n = int(input())
	s = input()
	q = int(input())
	for i in range(q):
		c=list(input().split())
		if c[0]=="1":
			s=s[:int(c[1])-1]+c[2]+s[int(c[1]):]
		else:
			print(len(set(s[int(c[1])-1:int(c[2])])))
except:
    import traceback
    traceback.print_exc()
input()

Submission status: Runtime Error

Solution code:
from collections import Counter
N = int(input())
S = list(str(input()))
Q = int(input())
#print(Counter("abdf"))
for i in range(Q):
  x = str(input())
  if int(x[0]) == 1:
    S[int(x[2]) - 1] = x[4]
  else:#x == 2
    l, r = int(x[2]), int(x[4])
    S1 = S[l - 1 : r]
    #print(S1, "yes")
    c = Counter(S1)
    print(len(c))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.buffer.readline
import bisect
N = int(input())
S = input()

dic = {}
for i in range(N):
    if S[i] in dic:
        dic[S[i]].append(i+1)
    else:
        dic[S[i]] = [i+1]

Q = int(input())
for _ in range(Q):
    a,b,c = map(str, input().split())
    if a == "1":
        b = int(b)
        dic[S[b-1]].remove(b)
        if c in dic:
            idx = bisect.bisect_left(dic[c], b)
            dic[c].insert(idx, b)
        else:
            dic[c] = [b]
    else:
        l = int(b)
        r = int(c)
        cnt = 0
        for key in dic:
            if len(dic[key]) == 0:
                continue
            elif len(dic[key]) == 1:
                if l <= dic[key][0] <= r:
                    cnt += 1
                continue
            elif bisect.bisect_left(dic[key], r)-bisect.bisect_left(dic[key], l) > 0:
                cnt += 1
        print(cnt)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = [ord(a) - 97 for a in input()]
NN = 19
BIT=[[0]*(2**NN+1) for _ in range(26)]
 
def addbit(t, i, x):
    while i <= 2**NN:
        BIT[t][i] += x
        i += i & (-i)

def getsum(t, i):
    ret = 0
    while i != 0:
        ret += BIT[t][i]
        i -= i&(-i)
    return ret


for i, a in enumerate(A):
    BIT[a][i+1] += 1

for t in range(26):
    B = [0] * (2**NN+1)
    for i in range(1<<NN):
        B[i+1] = B[i] + BIT[t][i]
    for i in range(1<<NN):
        BIT[t][i] = B[i] - B[i - (i & (-i))]

Q = int(input())
for _ in range(Q):
    a, b, c = input().split()
    if a == "1":
        b = int(b)
        c = ord(c) - 97
        t = A[b-1]
        addbit(t, b, -1)
        addbit(c, b, 1)
    else:
        b, c = int(b), int(c)
        s = 0
        for t in range(26):
            s += 1 if getsum(t, c+1) - getsum(t, b) else 0
        print(s)

Submission status: Runtime Error

Solution code:
7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(500000)
def input():
    return sys.stdin.readline()[:-1]

class BIT:
            
    def __init__(self, L):
        self.N = len(L)
        self.bit = [0]*self.N
        #for i,l in enumerate(L):
        #    self.add(i,l)
        #self.N0 = 1
        #while self.N0*2 <= self.N:
        #    self.N0 *= 2

    def add(self, a, w):
        x = a + 1
        for i in range(1000):
            self.bit[x-1] += w
            x += x & -x
            if x > self.N:
                break

    def sum(self, a):
        x = a+1
        ret = 0
        for i in range(1000):
            ret += self.bit[x-1]
            x -= x & -x
            if x <= 0:
                break        
        return ret

    #you can use this function when the BIT has only non-negative values.
    def lower_bound(self, w):
        if w<=0:
            return 0
        x = 0
        k = self.N0
        while k>0:
            if x+k<=self.N:
                if self.bit[x+k-1]<w:
                    w-=self.bit[x+k-1]
                    x+=k
            k//=2
        return x+1



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

    B = [BIT([0]*N) for i in range(26)]

    S = list(input())

    for i in range(N):
        B[ord(S[i]) - ord("a")].add(i, 1)

    Q = int(input())

    for i in range(Q):
        a,b,c = input().split()
        if a=="1":
            index = int(b)-1
            c = c
            if not S[i]==c:
                old = ord(S[i]) - ord("a")
                num = ord(c) - ord("a")
                B[num].add(index, 1)
                B[old].add(index, -1)
                S[i] = c
        else:
            l = int(b)-1
            r = int(c)-1
            ans = 0
            for j in range(26):
                if l==0:
                    if B[j].sum(r)>0:
                        ans+=1
                else:
                    if B[j].sum(r)-B[j].sum(l-1)>0:
                        ans+=1

            print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
s = []
A = [[] for i in range(26)]
for i in range(N):
    x = S[i]
    s.append(x)
    n = ord(x) - 97
    A[n].append(i)
Q = int(input())
q,a,b = map(str,input().split())
q = str(q)
a = str(a)
a -= 1
if q == 1:
    x = S[a]
    S[a] = b
    A[ord(x)].remove(a)
if q == 2:
    ans = 0
    b = str(b)
    b -= 1
    for i in range(26):
        B = A[i]
        import bisect
        p = bisect.bisect_left(B,a)
        q = bisect.bisect_right(B,b)
        if q - b > 0:
            ans += 1
    print(ans)

Submission status: Runtime Error

Solution code:
import math
from typing import (
    Callable,
    cast,
    Generic,
    Iterable,
    Iterator,
    List,
    MutableSequence,
    NamedTuple,
    Optional,
    overload,
    TypeVar,
    Union,
)


def _hasParents(i: int) -> bool:
    return i > 1


def _isLeftChild(i: int) -> bool:
    return i % 2 == 0


def _isRightChild(i: int) -> bool:
    return i % 2 == 1


def _parentNode(i: int) -> int:
    return i // 2


def _leftChild(i: int) -> int:
    return 2 * i


def _rightChild(i: int) -> int:
    return 2 * i + 1


def _leftSibling(i: int) -> int:
    return i - 1


def _rightSibling(i: int) -> int:
    return i + 1


def _nextPowerOfTwo(n: int) -> int:
    ret = 1
    while ret < n:
        ret *= 2
    return ret


def ancestors(i: int) -> Iterator[int]:
    while _hasParents(i):
        i = _parentNode(i)
        yield i


T = TypeVar("T")
A = TypeVar("A")


class Config(NamedTuple, Generic[T, A]):
    mconvert: Callable[[T], A]
    mappend: Callable[[A, A], A]
    mempty: Callable[[], A]


class SegmentTree(Generic[T, A], MutableSequence[T]):
    """A complete binary tree of n leaves stored in a contiguous array of length 2n.

    The children of index i are found at 2*i and 2*i+1, with root starting at index 1
    Example:
      |---------------|
      | 1             |
      |---------------|
      | 2     | 3     |
      |---------------|
      | 4 | 5 | 6 | 7 |
      |---------------|
    """

    MAX_SIZE: int
    values: List[Optional[T]]
    accumulations: List[A]
    numValues: int
    config: Config

    def __init__(
        self,
        arr: List[T],
        mconvert: Callable[[T], A],
        mappend: Callable[[A, A], A],
        mempty: Callable[[], A],
        minCapacity: int = 1,
    ):
        def wrappedMconvert(x: T) -> A:
            if x is not None:
                return mconvert(x)
            else:
                return mempty()

        def wrappedMappend(x: A, y: A) -> A:
            xEmpty = x == mempty()
            if xEmpty:
                return y
            yEmpty = y == mempty()
            if yEmpty:
                return x
            if xEmpty and yEmpty:
                return mempty()
            return mappend(x, y)

        self.config = Config(
            mconvert=wrappedMconvert, mappend=wrappedMappend, mempty=mempty
        )

        # Init empty tree
        self.MAX_SIZE = _nextPowerOfTwo(max(len(arr), minCapacity))
        self.values = [None] * self.MAX_SIZE
        self.accumulations = [self.config.mempty()] * (2 * self.MAX_SIZE)
        self.numValues = 0

        # Init values
        self.values[: len(arr)] = arr
        self.numValues = len(arr)
        # Init leaf accumulations
        self.accumulations[self.MAX_SIZE : self.MAX_SIZE + len(arr)] = map(
            self.config.mconvert, arr
        )
        # Init parent accumulations
        for parent in range(self.MAX_SIZE - 1, -1, -1):
            self.accumulations[parent] = self.config.mappend(
                self.accumulations[_leftChild(parent)],
                self.accumulations[_rightChild(parent)],
            )

    def query(self, i: int, j: int) -> A:
        # Inclusive i, exclusive j
        if not (0 <= i <= j <= self.numValues):
            raise IndexError
        if i == j:
            return self.config.mempty()
        leftTotal = self.config.mempty()
        rightTotal = self.config.mempty()
        l = self.MAX_SIZE + i
        r = self.MAX_SIZE + j - 1  # make inclusive both endpoints for calculations
        while l <= r:
            if _isRightChild(l):
                leftTotal = self.config.mappend(leftTotal, self.accumulations[l])
                l = _rightSibling(l)
            l = _parentNode(l)

            if _isLeftChild(r):
                rightTotal = self.config.mappend(self.accumulations[r], rightTotal)
                r = _leftSibling(r)
            r = _parentNode(r)
        return self.config.mappend(leftTotal, rightTotal)

    def append(self, x: T) -> None:
        assert self.numValues + 1 <= self.MAX_SIZE
        self.numValues += 1
        self.__setitem__(self.numValues - 1, x)

    def pop(self, i: Optional[int] = None) -> T:
        if i is not None:
            raise NotImplementedError
        if not self.numValues:
            raise IndexError
        ret = self.__getitem__(self.numValues - 1)
        self.__delitem__(self.numValues - 1)
        return ret

    def insert(self, i: int, x: T) -> None:
        raise NotImplementedError

    @overload
    def __getitem__(self, index: int) -> T:
        pass

    @overload
    def __getitem__(self, index: slice) -> MutableSequence[T]:
        pass

    def __getitem__(self, index: Union[int, slice]) -> Union[T, MutableSequence[T]]:
        if isinstance(index, slice):
            raise NotImplementedError
        if not 0 <= index < self.numValues:
            raise IndexError
        ret = self.values[index]
        assert ret is not None
        return ret

    @overload
    def __setitem__(self, i: int, x: T) -> None:
        pass

    @overload
    def __setitem__(self, i: slice, x: Iterable[T]) -> None:
        pass

    def __setitem__(self, i: Union[int, slice], x: Union[T, Iterable[T]]) -> None:
        if isinstance(i, slice):
            raise NotImplementedError
        # int index always correspond with T x
        # Can't just check `isinstance(x, Iterable)` because x can be stuff like str
        x = cast(T, x)
        if not 0 <= i < self.numValues:
            raise IndexError
        index = self.MAX_SIZE + i
        self.values[i] = x
        self.accumulations[index] = self.config.mconvert(x)
        for parent in ancestors(index):
            self.accumulations[parent] = self.config.mappend(
                self.accumulations[_leftChild(parent)],
                self.accumulations[_rightChild(parent)],
            )
            parent = _parentNode(parent)

    @overload
    def __delitem__(self, i: int) -> None:
        pass

    @overload
    def __delitem__(self, i: slice) -> None:
        pass

    def __delitem__(self, i: Union[int, slice]) -> None:
        if isinstance(i, slice):
            raise NotImplementedError
        if not 0 <= i < self.numValues:
            raise IndexError
        index = self.MAX_SIZE + i
        self.values[i] = None
        self.accumulations[index] = self.config.mempty()
        for parent in ancestors(index):
            self.accumulations[parent] = self.config.mappend(
                self.accumulations[_leftChild(parent)],
                self.accumulations[_rightChild(parent)],
            )
        self.numValues -= 1

    def __len__(self) -> int:
        return self.numValues


def charToBitmask(c):
    return 1 << (ord(c) - ord("a"))


def popcount(n):
    # Python doesn't have __builtin_popcount
    return bin(n).count("1")


N, = list(map(int, input().split()))
S = [charToBitmask(c) for c in input().rstrip()]
Q, = list(map(int, input().split()))
queries = []
for i in range(Q):
    queries.append(input().split())


segTree = SegmentTree(S, lambda x: x, lambda mask1, mask2: mask1 | mask2, lambda: 0)
out = []
for query in queries:
    if query[0] == "1":
        _, i, c = query
        i = int(i) - 1
        segTree[i] = charToBitmask(c)
    elif query[0] == "2":
        _, l, r = query
        l = int(l) - 1
        r = int(r) - 1
        mask = segTree.query(l, r + 1)

        out.append(str(popcount(mask)))
print("\n".join(out))

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
check = [0 for _ in range(30)]
for i in range(int(input())):
    c = input()
    if c[0] == '1':
        s1 = s[:int(c[2])-1]
        s2 = s[int(c[2]):]
        s = s1 + c[4] + s2
    else:
        for i in range(30):
            check[i] = 0
        start = int(c[2])
        end = int(c[4])
        temp = s[start-1:end]
        for i in temp:
            check[ord(i) - ord('a')] = 1
        print(sum(check))

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
Q = int(input())

for i in range(Q):
	typ , a, b = input().split()
	if(typ == '1'):
		i = int(a)
		c = b
		if(i == 0):
			S = c + S[1:]
		else:
			S = S[:i-1] + c + S[i:]

	
	if(typ == '2'):
		l = int(a)
		r = int(b)
		S_part = S[l-1:r]

		print(len(set(S_part)))

	print(S)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
for i in range(int(input())):
    c = input()
    if c[0] == '1':
        s1 = s[:int(c[2])-1]
        s2 = s[int(c[2]):]
        s = s1 + c[4] + s2
    else:
        start = int(c[2])
        end = int(c[4])
        temp = s[start-1:end]
        check = [0 for _ in range(30)]
        for i in temp:
            check[ord(i) - ord('a')] = 1
        print(sum(check))

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
Q = int(input())

for i in range(Q):
	typ , a, b = input().split()
	if(typ == '1'):
		i = int(a)
		c = b
		print(i, c)
		if(i == 0):
			S = c + S[1:]
		else:
			S = S[:i-1] + c + S[i+1:]
		print(S)
	
	if(typ == '2'):
		l = int(a)
		r = int(b)
		S_part = S[l-1:r]
		print(S_part)

		print(len(set(S_part)))

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
for i in range(int(input())):
    c = input()
    if c[0] == '1':
        s1 = s[:int(c[2])-1]
        s2 = s[int(c[2]):]
        s = s1 + c[4] + s2
    else:
        start = int(c[2])
        end = int(c[4])
        temp = s[start-1:end]
        check = [0 for _ in range(30)]
        for i in temp:
            check[ord(i) - ord('a')] += 1
        ans = 0
        for i in check:
            if i > 0:
                ans += 1
        print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = list(input())
q = int(input())

for _ in range(q):
    a1, a2, a3 = input().split()

    # a1 = 1の時
    if a1 == "1" and s[int(a2) - 1] != a3:
        s[int(a2) - 1] = a3

    # a1 = 2の時
    else:
        print(len(set(s[int(a2) - 1:int(a3)])))

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
Q = int(input())

for i in range(Q):
	typ , a, b = input().split()
	if(typ == '1'):
		i = int(a)
		c = b

		S[i-1] = b
	
	if(typ == '2'):
		l = int(a)
		r = int(b)
		S_part = S[l+1:r+2]

		print(len(set(S_part)))

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
for i in range(int(input())):
    c = input()
    if c[0] == '1':
        s1 = s[:int(c[2])-1]
        s2 = s[int(c[2]):]
        s = s1 + c[4] + s2
    else:
        if len(c) < 5:
            print(1)
        else:
            start = int(c[2])
            end = int(c[4])
            temp = s[start-1:end]
            check = [0 for _ in range(30)]
            for i in temp:
                check[ord(i) - ord('a')] += 1
            ans = 0
            for i in check:
                if i > 0:
                    ans += 1
            print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
for i in range(int(input())):
    c = input()
    if c[0] == '1':
        s1 = s[:int(c[2])]
        s2 = s[int(c[2])+1:]
        s = s1 + c[4] + s2
    else:
        if len(c) < 5:
            print(1)
        else:
            start = int(c[2])
            end = int(c[4])
            temp = s[start-1:end]
            check = [0 for _ in range(30)]
            for i in temp:
                check[ord(i) - ord('a')] += 1
            ans = 0
            for i in check:
                if i > 0:
                    ans += 1
            print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
aas = "abcdefghijklmnopqrstuvwxyz"
N = int(input().strip())
S = list(input().strip())
Q = int(input().strip())
cbt = dict()
MX = 10**5
for c in aas:
    cbt[c] = Bit(MX)
    for i, a in enumerate(S):
        if a == c:
            cbt[c].add(i+1, 1)

for _ in range(Q):
    t, i, c = input().split()
    t = int(t)
    if t == 1:
        i = int(i)
        if S[i-1] != c:
            cbt[S[i-1]].add(i, -1)
            cbt[c].add(i, 1)
            S[i-1] = c
    else:
        l, r = int(i), int(c)
        s = 0
        for a in aas:
            bt = cbt[a]
            s += (bt.sum(r+1)-bt.sum(l-1)>0)
        print(s)