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:
cmin = lambda a,b : a if a<b else b
cmax = lambda a,b : a if a>b else b
iin = lambda : int(input())
lin = lambda : list(map(int,input().split()))
rin = lambda n : [int(input()) for _ in range(n)]

n = iin()
s = input()
q = iin()
ans = []
for i in range(q):
    f,a,b = map(lambda x:int(x)0 if x.isdigit() else x,input().split())
    if f==1:
        if a==n:
            s = s[:a] + b
        else:
            s = s[:a] + b + s[a+2:]
    else:
        if b==n:
            ans += [len(set(s[a:]))]
        else:
            ans += [len(set(s[a:b+2]))]

for i in ans:
    print(i)

Submission status: Runtime Error

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


#セグメント木
#次の2つの処理をO(log N)で実施することができる
# s, tが与えられた時、区間[s, t)の最小値を求める
# i, xが与えられた時、a_iの値をxに変更する

# INIT = 10 ** 18 #初期値 答えに関係ないもの
MAX_N = 5 * 10 ** 5 + 10 #与えられる要素数の最大

data = [0] * (2 * MAX_N - 1) #MAX_Nで初期化

#ここに機能をもたせる
def func(vl, vr):
    return (vl | vr)

def init(n_):
    #簡単のため要素数を2のべき乗に
    n = 1
    while n < n_:
        n *= 2
    return n

n = init(N)

def update(k, a): #k番目の値をaに変更 0-index
    # B[a] = k
    k += n - 1
    data[k] = (1 << a)
    while k > 0: #登りながら更新
        k = (k - 1)//2
        data[k] = func(data[k * 2 + 1], data[k * 2 + 2])

def query(a, b, k = 0, l = 0, r = n):
    if r <= a or b <= l:
        return 0
    if a <= l and r <= b:
        return data[k]
    else:
        vl = query(a, b, k * 2 + 1, l, (l + r)//2)
        vr = query(a, b, k * 2 + 2, (l + r)//2, r)
        return func(vl, vr)

def initialize(A): #リストAを与えて初期化する
    for i, a in enumerate(A):
        data[i + n - 1] = (1 << (ord(a) - 97))
        # B[a] = i
    for i in range((2 * n - 3)//2, -1, -1):
        data[i] = func(data[i * 2 + 1], data[i * 2 + 2])

# def index(a): #要素aがもとのどこに保存されているかを返す関数
#     return B[a]

# def value(index): #indexが与えられた時、index番目の値を得る
#     return data[index + n - 1]

#初期値の入力
initialize(S)

# print (data)

#クエリの数
# Q = int(input())
for _ in range(Q):
    a, b, c = map(str, input().split())
    if a == '1':
        update(int(b) - 1, (ord(c) - 97))
    else:
        tmp = query(int(b) - 1, int(c))
        count = 0
        for j in range(26):
            if (tmp >> j) & 1 == 1:
                count += 1
        # print (tmp)
        print (count)
# print (data)

Submission status: Runtime Error

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


#セグメント木
#次の2つの処理をO(log N)で実施することができる
# s, tが与えられた時、区間[s, t)の最小値を求める
# i, xが与えられた時、a_iの値をxに変更する

# INIT = 10 ** 18 #初期値 答えに関係ないもの
MAX_N = 5 * 10 ** 5 + 10 #与えられる要素数の最大
MAX_N = 20
data = [0] * (2 * MAX_N - 1) #MAX_Nで初期化

#ここに機能をもたせる
def func(vl, vr):
    return (vl | vr)

def init(n_):
    #簡単のため要素数を2のべき乗に
    n = 1
    while n < n_:
        n *= 2
    return n

n = init(N)

def update(k, a): #k番目の値をaに変更 0-index
    # B[a] = k
    k += n - 1
    data[k] = (1 << a)
    while k > 0: #登りながら更新
        k = (k - 1)//2
        data[k] = func(data[k * 2 + 1], data[k * 2 + 2])

def query(a, b, k = 0, l = 0, r = n):
    if r <= a or b <= l:
        return 0
    if a <= l and r <= b:
        return data[k]
    else:
        vl = query(a, b, k * 2 + 1, l, (l + r)//2)
        vr = query(a, b, k * 2 + 2, (l + r)//2, r)
        return func(vl, vr)

def initialize(A): #リストAを与えて初期化する
    for i, a in enumerate(A):
        data[i + n - 1] = (1 << (ord(a) - 97))
        # B[a] = i
    for i in range((2 * n - 3)//2, -1, -1):
        data[i] = func(data[i * 2 + 1], data[i * 2 + 2])

# def index(a): #要素aがもとのどこに保存されているかを返す関数
#     return B[a]

# def value(index): #indexが与えられた時、index番目の値を得る
#     return data[index + n - 1]

#初期値の入力
initialize(S)

# print (data)

#クエリの数
# Q = int(input())
for _ in range(Q):
    a, b, c = map(str, input().split())
    if a == '1':
        update(int(b) - 1, (ord(c) - 97))
    else:
        tmp = query(int(b) - 1, int(c))
        count = 0
        for j in range(26):
            if (tmp >> j) & 1 == 1:
                count += 1
        # print (tmp)
        print (count)
# print (data)

Submission status: Runtime Error

Solution code:
mport sys
input = sys.stdin.readline
 
n = int(input())
s = list(str(input()))
s = [{ord(c)-ord('a')} for c in s]
 
def segfunc(x, y):
    return x | y
 
def init(init_val):
    # set_val
    for i in range(n):
        seg[i+num-1] = init_val[i]
    # built
    for i in range(num-2, -1, -1):
        seg[i] = segfunc(seg[2*i+1], seg[2*i+2])
 
def update(k, x):
    k += num - 1
    seg[k] = x
    while k:
        k = (k-1)//2
        seg[k] = segfunc(seg[2*k+1], seg[2*k+2])
 
def query(p, q):
    if q <= p:
        return ide_ele
    p += num - 1
    q += num - 2
    res = ide_ele
    while q-p>1:
        if p%2 == 0:
            res = segfunc(res, seg[p])
        if q%2 == 1:
            res = segfunc(res, seg[q])
            q -= 1
        p = p//2
        q = (q-1)//2
    if p == q:
        res = segfunc(res, seg[p])
    else:
        res = segfunc(segfunc(res, seg[p]), seg[q])
    return res
 
# identity element
ide_ele = set()
 
# num: n以上の最小の2のべき乗
num = 2**(n-1).bit_length()
seg = [ide_ele]*2*num
 
init(s)
 
q = int(input())
for _ in range(q):
    t, x, y = map(str, input().split())
    if t == '1':
        i = int(x)-1
        c = {ord(y)-ord('a')}
        update(i, c)
    else:
        l = int(x)-1
        r = int(y)-1
        print(len(query(l, r+1)))

Submission status: Runtime Error

Solution code:
#13:19
n = int(input())
s = input()
t = []
for i in range(n):
  t.append(ord(s[i])-97)
q = int(input())
scale = 18
seg = [[0 for _ in range(2**(scale+1))] for _ in range(26)]
for i in range(n):
  tmp = int(i) + 2 ** scale
  x = t[i]
  while tmp != 0:
    seg[x][tmp] += 1
    tmp //= 2
#print(seg)
raw = []
for _ in range(q):
  d,a,b = input().split()
  d = int(d)
  if d == 1:
    a = int(a) - 1
    b = ord(b) - 97
  else:
    a = int(a) - 2
    b = int(b) - 1
  raw.append([d,a,b])
for p in range(q):
  d,a,b = raw[p]
  if d == 1:
    tmp = int(a)
    c = t[tmp]
    tmp += 2**scale
    while tmp != 0:
      seg[c][tmp] -= 1
      tmp //= 2
    tmp = int(a)
    t[tmp] = b
    tmp += 2**scale
    while tmp != 0:
      seg[b][tmp] += 1
      tmp //= 2
  else:
    ans = 0
    for d in range(26):
      l = 0
      r = 0
      if a == -1:
        l = 0
      else:
        tmp = a + 2**scale
        l += seg[d][tmp]
        while tmp != 1:
          if tmp % 2 == 1:
            l += seg[d][tmp-1]
          tmp //= 2
      tmp = b + 2**scale
      r += seg[d][tmp]
      while tmp != 1:
        if tmp % 2 == 1:
          r += seg[d][tmp-1]
        tmp //= 2
      if r > l:
        ans += 1
      #print(d,r,l)
    print(ans)
#print(t)

Submission status: Runtime Error

Solution code:
#13:19
n = int(input())
s = input()
t = []
for i in range(n):
  t.append(ord(s[i])-ord('a'))
q = int(input())
scale = 3
seg = [[0 for _ in range(2**(scale+1))] for _ in range(26)]
for i in range(n):
  tmp = int(i) + 2 ** scale
  x = t[i]
  while tmp != 0:
    seg[x][tmp] += 1
    tmp //= 2
#print(seg)
#print(seg[2])
for _ in range(q):
  d,a,b = input().split()
  if d == '1':
    b = ord(b) - ord('a')
    tmp = int(a) - 1
    c = t[tmp]
    tmp += 2**scale
    while tmp != 0:
      seg[c][tmp] -= 1
      tmp //= 2
    tmp = int(a) - 1
    t[tmp] = b
    tmp += 2**scale
    while tmp != 0:
      seg[b][tmp] += 1
      tmp //= 2
  else:
    a = int(a) - 2
    b = int(b) - 1
    ans = 0
    for d in range(26):
      l = 0
      r = 0
      if a == -1:
        l = 0
      else:
        tmp = a + 2**scale
        l += seg[d][tmp]
        while tmp != 1:
          if tmp % 2 == 1:
            l += seg[d][tmp-1]
          tmp //= 2
      tmp = b + 2**scale
      r += seg[d][tmp]
      while tmp != 1:
        if tmp % 2 == 1:
          r += seg[d][tmp-1]
        tmp //= 2
      if r > l:
        ans += 1
      #print(d,r,l)
    print(ans)
#print(t)

Submission status: Runtime Error

Solution code:
# E - Simple String Queries
from operator import or_
from typing import Callable, List, Optional, Sequence, Union


class SegmentTree:
    __slots__ = ["_default_value", "_seg_func", "_modifying_func", "_size", "_tree"]

    def __init__(self, initial_values: Sequence, default_value: Union[int, str], seg_func: Callable,
                 modifying_func: Optional[Callable] = None) -> None:
        self._default_value = default_value
        self._seg_func = seg_func
        self._modifying_func = modifying_func
        self._size = 1 << (len(initial_values) - 1).bit_length()
        self._tree = self._build(initial_values)

    def _build(self, initial_values: Sequence) -> List:
        """Build a segment tree with initial values."""
        tree = [self._default_value] * (2 * self._size)

        if self._modifying_func:
            initial_values = map(self._modifying_func, initial_values)

        for idx, val in enumerate(initial_values):  # set a tree
            tree[idx + self._size - 1] = val

        for idx in range(self._size - 2, -1, -1):  # build
            tree[idx] = self._seg_func(tree[2 * idx + 1], tree[2 * idx + 2])

        return tree

    def update(self, index: int, value: int) -> None:
        """Update index to value."""
        index += self._size - 1
        if self._modifying_func:
            value = self._modifying_func(value)
        self._tree[index] = value
        while index:
            index = (index - 1) // 2
            self._tree[index] = self._seg_func(self._tree[2 * index + 1], self._tree[2 * index + 2])

    def query(self, left: int, right: int) -> Union[int, str]:
        """Respond to query in [left, right]."""
        left += self._size
        right += self._size
        result = self._default_value
        while left < right:
            if left & 1:
                result = self._seg_func(result, self._tree[left - 1])
                left += 1
            if right & 1:
                right -= 1
                result = self._seg_func(result, self._tree[right - 1])
            left >>= 1
            right >>= 1
        return result


def main():
    _, S, _, *queries = open(0).read().split()
    tree = SegmentTree(list(S), 0, or_, lambda c: 1 << (ord(c) - 97))
    ans = []
    popcount = lambda x: bin(x).count("1")
    for com, a, b in zip(*[iter(queries)] * 3):
        if com == "1":
            tree.update(int(a) - 1, b)
        else:
            ans.append(popcount(tree.query(int(a) - 1, int(b))))
    print("\n".join(map(str, ans)))


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import bisect
import collections
import sys

input = sys.stdin.readline


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

    s = list(input())

    q = int(input())

    li = list(s)

    d = {}

    for i in range(26):
        d[chr(i + 97)] = []

    for i, x in enumerate(s):
        d[x].append(i)


    for _ in range(q):
        a, *x = input().split()

        if a == "1":
            i = int(x[0]) - 1
            c = x[1]
            o = li[i]
            index = bisect.bisect_left(d[o], i)
            d[o].pop(index)
            d[c].insert(bisect.bisect_left(d[c], i), i)
            li[i] = c
        else:
            l = int(x[0]) - 1
            r = int(x[1]) - 1
            cnt = 0
            for v in d.values():
                if len(v) == 0:
                    continue
                index = bisect.bisect_left(v, l)
                if index == len(v):
                    continue
                if v[index] <= r:
                    cnt += 1
            print(cnt)

main()

Submission status: Runtime Error

Solution code:
import sys
from bisect import bisect_left,bisect_right,insort
input = sys.stdin.readline

def main():
    n=int(input())
    s=list(input())
    p=[[] for _ in range(26)]
    for i in range(n):
        p[ord(s[i])-ord('a')].append(i+1)
    q=int(input())
    for i in range(q):
        a,b,c=map(str,input().split())
        if a=='1':
            b=int(b)-1
            if s[b]==c:
                continue
            else:
                insl=bisect_left(p[ord(c)-ord('a')],b)
                p[ord(s[i])-ord('a')].pop(insl)
                insort(p[ord(c)-ord('a')],insl)
                s[b]=c
        else:
            b,c=int(b)-1,int(c)-1
            cnt=0
            for i in range(26):
                insl=bisect_left(p[i],b)
                if insl>=len(p[i]):
                    continue
                elif p[i][insl]<=c:
                    cnt+=1
            print(cnt)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import defaultdict
import bisect

alphabet="abcdefghijklmnopqrstuvwxyz"

bS = int(input())
S= input()
nQ = int(input())

query = []
for i in range(nQ):
  query.append(input().split())

locs =defaultdict(list)

for i,char in enumerate(S):
    locs[char].append(i)
    
def countChars(iter,ss,ee):
  nChar = 0
  for i in alphabet:
    if len(locs[i]) == 0:
      pass
    else:
      sptr = bisect.bisect_right(locs[i],ss)
      eptr = bisect.bisect_left(locs[i],ee)
      if sptr == eptr:
        if sptr < 1 or sptr > len(locs[i]):
          continue
        else:
          if locs[i][sptr-1] == ss:
            nChar+=1
      else:
        nChar += 1
  return nChar
    
  
res = []
for iter,iQ in enumerate(query):
  if int(iQ[0]) == 2:
    res.append(str( countChars(iter,int(iQ[1])-1,int(iQ[2])) ))
  if int(iQ[0]) == 1:
    location = int(iQ[1])-1
    curChar  = S[location]
    if curChar != iQ[2]:
      locs[curChar].remove(location) #remove from old
      bisect.insort(locs[iQ[2]],location)
      
print(" ".join(res))

Submission status: Runtime Error

Solution code:
import bisect
from collections import defaultdict

N = int(input())
S = input()
Q = int(input())

ref = defaultdict(list)
for i, c in enumerate(S):
    ref[c].append(i)

for _ in range(Q):
    t, q1, q2 = input().split(' ')
    if t == '1':
        q1 = int(q1) - 1
        ref[S[q1]].remove(q1)
        bisect.insort(ref[q2], q1)
    else:
        ans = 0
        q1 = int(q1) - 1
        q2 = int(q2) - 1
        for v in ref.values():
            idx = bisect.bisect_left(v, q1)
            if idx < len(v):
                if (v[idx] <= q2):
                    ans += 1
        print(ans)

Submission status: Runtime Error

Solution code:
import bisect
from collections import defaultdict

N = int(input())
S = input()
Q = int(input())

ref = defaultdict(list)
for i, c in enumerate(S):
    ref[c].append(i)

for _ in range(Q):
    t, q1, q2 = input().split(' ')
    if t == '1':
        q1 = int(q1) - 1
        ref[S[q1]].remove(q1)
        bisect.insort(ref[q2], q1)
    else:
        ans = 0
        q1 = int(q1) - 1
        q2 = int(q2) - 1
        for v in ref.values():
            idx = bisect.bisect_left(v, q1)
            if (idx < len(v)) and (v[idx] <= q2):
                ans += 1
        print(ans)

Submission status: Runtime Error

Solution code:
import bisect
from collections import defaultdict

N = int(input())
S = list(input())
Q = int(input())

ref = defaultdict(list)
for i, c in enumerate(S):
    ref[c].append(i)

ans = []
for _ in range(Q):
    t, q1, q2 = input().split(' ')
    if t == '1':
        q1 = int(q1) - 1
        c = S[q1]
        ref[c].remove(q1)
        ref[q2].append(q1)
    else:
        num = 0
        q1 = int(q1) - 1
        q2 = int(q2) - 1
        for k, v in ref.items():
            idx = bisect.bisect_right(v, q1-1)
            if idx < len(v):
                pos = v[idx]
                if pos <= q2:
                    num += 1
        ans.append(num)

print('\n'.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
import pprint
sys.setrecursionlimit(10 ** 9)


INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


class BIT:
    def __init__(self, size):
        self.bit = [0] * size
        self.size = size
        self.total = 0

    def add(self, i, w):
        x = i + 1
        self.total += w
        while x <= self.size:
            self.bit[x - 1] += w
            x += x & -x
        return

    def sum(self, i):
        if i < 0:
            return 0
        res = 0
        x = i + 1
        while x:
            res += self.bit[x - 1]
            x -= x & -x
        return res

    def interval_sum(self, i, j): # i <= x < j の区間
        return self.sum(j - 1) - self.sum(i - 1)


n = I()
s = list(S())
D = defaultdict(lambda:BIT(n))
for i in range(n):
    D[ord(s[i])].add(i, 1)

q = I()
for j in range(q):
    qi, i, c = LS()
    i = int(i) - 1
    if qi == '1':
        D[ord(s[i])].add(i, -1)
        D[ord(c)].add(i, 1)
        s[i] = c
    else:
        l, r = i, int(c)
        ret = 0
        for i in range(97, 123):
            if D[i].sum(r) > D[i].sum(l - 2):
                ret += 1
        print(ret)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.buffer.readline
def main():
    N = int(input())
    S = list(input().rstrip())

    c = lambda x: ord(x) - ord('a')
    tree = [SegmentTree(N, lambda x,y:x|y) for _ in range(26)]
    for i, l in enumerate(S):
        tree[c(l)].update(i, 1)

    Q = int(input())
    for _ in range(Q):
        t, a, b = input().rstrip().split()
        if t == '1':
            a = int(a) - 1
            tree[c(S[a])].update(a, 0)
            tree[c(b)].update(a, 1)
            S[a] = b
        else:
            a, b = int(a)-1, int(b)-1
            ans = 0
            for i in range(26):
                t = tree[i].query(a, b+1)
                ans += 1 if t else 0
            print(ans)

class SegmentTree:
    def __init__(self, n=None, f=max, zero_factory=int, converter=lambda x:x, initial_values=None):
        assert(n, initial_values)
        size = n if n else len(initial_values)
        self.__f, self.__z, self.__c = f, zero_factory, converter
        self.__n = 1 << ((size - 1).bit_length())
        self.__dat = [zero_factory() for _ in range(2 * self.__n)]
        if initial_values:
            zi = self.__get_leaf(0)
            for i, v in enumerate(initial_values): self.__dat[zi + i] = converter(v)
            for i in range(zi-1, -1, -1): self.__dat[i] = f(*self.__get_children(i))

    def update(self, index, value):
        i, v = self.__get_leaf(index), self.__c(value)
        if self.__dat[i] == v: return
        self.__dat[i] = v
        while i:
            i = self.__get_parent(i)
            self.__dat[i] = self.__f(*self.__get_children(i))

    def query(self, from_inclusive, to_exclusive):
        ans = self.__z()
        if to_exclusive <= from_inclusive: return ans
        l, r = self.__get_leaf(from_inclusive), self.__get_leaf(to_exclusive) - 1
        while r - l > 1:
            if self.__is_right(l): ans = self.__f(ans, self.__dat[l])
            if self.__is_left(r): ans, r = self.__f(ans, self.__dat[r]), r-1
            l, r = l//2, self.__get_parent(r)
        ans = self.__f(ans, self.__dat[l])
        if l != r: ans = self.__f(ans, self.__dat[r])
        return ans

    def __get_leaf(self, i): return self.__n + i - 1
    def __get_parent(self, i): return (i-1)//2
    def __get_children(self, i): return (self.__dat[2*i+1], self.__dat[2*i+2])
    def __is_left(self, i): return i&1 == 1
    def __is_right(self, i): return i&1 == 0

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
S = list(input())
Q = int(input())
Q_list = [[int(i[0]), int(i[1]), i[2] for i input().split()] for _ in range(Q)]
for q in Q_list:
  if(q[0]==1):
    S[q[1]-1] = q[2]
  else:
    q[2] = int(q[2])
    s, e = q[1], q[2]
    t = 0
    str_list = []
    for i in range(s-1,e):
      if(S[i] not in str_list):
        str_list.append(S[i])
        t += 1
    print(t)

Submission status: Runtime Error

Solution code:
import math

n = int(input())
s = input()
S = [ord(i) - 97 for i in s]
q = int(input())
HEIGHT = math.ceil(math.log(n) / math.log(2))
SIZE = 1 << HEIGHT

def build_tree_for_letter(letter):
    tree = [False for _ in range(2 << HEIGHT)]
    for i in range(1 << HEIGHT):
        tree[i + (1 << HEIGHT)] = True if i < n and s[i] == chr(97 + letter) else False

    for i in range(HEIGHT - 1, -1, -1):
        for x in range(1<<i, 2<<i):
            tree[x] = any((tree[x<<1], tree[(x<<1)+1]))

    return tree


def build():
    return [build_tree_for_letter(letter) for letter in range(26)]



def update(x, letter, positive=True):
    trees[letter][x] = (positive == True)
    while x > 1:
        x >>= 1
        trees[letter][x] = any((trees[letter][x << 1], trees[letter][(x << 1) + 1]))


def query(ll, rr):

    # print("query: ", l, r)
    total = 0
    for letter in range(26):
        l, r = ll, rr
        is_letter = False
        while l < r:
            if l%(2) != 0:
                is_letter = any((is_letter, trees[letter][l]))
                l += 1
            l >>= 1
            if r%(2) != 0:
                r -= 1
                is_letter = any((is_letter, trees[letter][r]))
            r >>= 1
        total += is_letter
    return total



trees = build()

for i in range(q):
    a, b, c = input().split()
    b = int(b) - 1 + SIZE
    if int(a) == 1:
        c = ord(c) - 97
        update(b, S[b], positive=False)
        S[b] = c
        update(b, c, positive=True)
    else:
        print(query(b, int(c)+SIZE))

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
q=int(input())
 
for i in range(q):
  a,b,c = input().split(" ")
  if int(a) == 1:
    b = int(b)
    s = "".join(s[:b-1],c,s[b:])
  else:
    b = int(b)
    c = int(c)
    print(len(set(list(s[b-1:c]))))

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
q=int(input())
#print(s)

for i in range(q):
  a,b,c = input().split(" ")
  if int(a) == 1:
    b = int(b)
    s = s[:b-1] + c + s[b:]
    #print(s)
    else:
      b = int(b)
      c = int(c)
      #print(list([j for j in s[b-1:c]]))
      print(len(set(list([j for j in s[b-1:c]]))))

Submission status: Runtime Error

Solution code:
n=int(inputs(code))
s=inputs(code)
q=int(inputs(code))
#print(s)

for i in range(q):
  a,b,c = inputs(code).split(" ")
  if int(a) == 1:
    b = int(b)
    s = s[:b-1] + c + s[b:]
    #print(s)
    else:
      b = int(b)
      c = int(c)
      #print(list([j for j in s[b-1:c]]))
      print(len(set(list([j for j in s[b-1:c]]))))