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:
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


def index(a, x):
    'Locate the leftmost value exactly equal to x'
    i = bisect_left(a, x)

    if i != len(a) and a[i] == x:
        return i
    raise ValueError

# ポイント
# 文字列update
# 文字種の高速取得

# 文字種の高速取得について考えてみる
# 文字のidxを取得しておいて二分探索することで高速に取得することが可能


N = read_a_int()
S = read()[:-1]

from collections import defaultdict
from bisect import bisect_left, bisect_right, insort_left
char_idxs = defaultdict(lambda: [])
for i, s in enumerate(S):
    char_idxs[s].append(i)


def get_syurui(char_idxs, l, r):
    ret = 0
    for v in char_idxs.values():
        l_idx = bisect_left(v, l)
        r_idx = bisect_right(v, r)
        # print(v,l_idx,r_idx)
        if r_idx - l_idx > 0:
            ret += 1
    return ret


Q = read_a_int()
for q in range(Q):
    com, a, b = read().split()
    if int(com) == 2:
        a, b = int(a) - 1, int(b) - 1
        print(get_syurui(char_idxs, a, b))
    else:
        i = int(a) - 1
        if S[i] == b:
            continue
        # i文字目を消す
        tmp = char_idxs[S[i]]  # S[i]を更新しなきゃ
        del char_idxs[S[i]][index(tmp, i)]
        # cのidxに挿入し直す
        insort_left(char_idxs[b], i)
        # S[i]を更新しなきゃ
        S[i] = b

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

Solution code:
class Binary_Indexed_Tree:
    def __init__(self, n):
        self.size = n
        self.tree = [0 for _ in range(n+1)]

    # a[i]にxを加算(1 <= i <= n) 1-indexed
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & (-i)

    # a[1] から a[t] までの合計を得る (1 <= t <= n)
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & (-i)

        return s




class RangeUpdate:
    def __init__(self, n):
        self.p = Binary_Indexed_Tree(n + 1)
        self.q = Binary_Indexed_Tree(n + 1)

    # a[s] から a[t] まで一律xを加算 (1 <= s <= t <= n)
    def add(self, s, t, x):
        t += 1
        self.p.add(s, -x * s)
        self.p.add(t, x * t)
        self.q.add(s, x)
        self.q.add(t, -x)

    # a[s] から a[t] までの合計を得る (1 <= s <= t <= n)
    def sum(self, s, t):
        t += 1
        return self.p.sum(t) + self.q.sum(t) * t - self.p.sum(s) - self.q.sum(s) * s

alpha_small_dict = {chr(i):i-97 for i in range(97, 97+26)} #辞書ver
n = int(input())
S = input()
data = [[0 for i in range(n)] for j in range(26)]

bit = [RangeUpdate(n) for i in range(26)]
for i,x in enumerate(S):
    bit[alpha_small_dict[x]].add(i+1,i+1,1)
    data[i][alpha_small_dict[x]] = 1
q = int(input())
for i in range(q):
    x,y,z = input().split()
    x,y = int(x),int(y)
    #print(x,y,z)
    if x == 1:
        num = alpha_small_dict[z]
        for j in range(26):
            if data[j][y-1] == 1:
                data[j][y-1] = 0
                bit[j].add(y,y,-1)
                break
        data[num][y-1] = 1
        bit[num].add(y,y,1)
    else:
        z = int(z)
        ans = 0
        for j in range(26):
            if bit[j].sum(y,z) >= 1:
                ans += 1
        print(ans)

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())
import bisect
for i in range(Q):
    q,a,b = map(str,input().split())
    a = int(a)
    a -= 1
    if q == '1':
        x = s[a]
        s[a] = b
        r = bisect.bisect_left(A[ord(x)-97],a)
        del A[ord(x)-97][r]
        A[ord(b)-97].append(a)
    if q == '2':
        ans = 0
        b = int(b)
        b -= 1
        for i in range(26):
            B = A[i]
            B.sort()
            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:
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():
    #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[index]==c:
                old = ord(S[index]) - 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 math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)
import bisect

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

n = int(input())
s = [ c for c in input()]
q = int(input())
qqq = list(map(lambda s: s.strip().split(), sys.stdin.readlines()))

chars = [set() for _ in range(26)]
for i,c in enumerate(s):
    o = ord(c) - ord("a")
    chars[o].add(i)
# log(chars)

for que in qqq:
    if que[0] == "1":
        target = int(que[1])-1
        now = ord(s[target]) - ord("a")
        c = ord(que[2]) - ord("a")
        if now != c:
            # chars[now]からtargetを消す
            chars[now].remove(target)
            # chars[c]にtargetを加える
            chars[c].add(target)

    else:
        # 半開区間に変換
        left = int(que[1])-1
        right = int(que[2])

        count = 0
        for c in range(26):
            if any(map(lambda i: left<=i<right, chars[c])):
                # chars[c]にleft以上right未満の値があれば
                count += 1
        print(count)

Submission status: Runtime Error

Solution code:
# INT_MAX = 2**31-1
MAX_N = 10**5

def init(n_):
    n = 1
    while n < n_:
        n *= 2
    # dat = [INT_MAX] * (2*n-1)
    dat = [set()] * (2*n-1)
    return n, dat

def update(k, a):
    k += N-1
    dat[k] = set(a)
    while k > 0:
        k = (k-1) // 2
        # dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2])
        # dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2])
        dat[k] = dat[k * 2 + 1] | dat[k * 2 + 2]


def query(a, b, k, l, r):
    if r <= a or b <= l:
        return set()
    if a <= l and r <= b:
        return dat[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 min(vl, vr)
        return vl | vr

if __name__ == "__main__":
    # n, q = map(int, input().split())
    N = int(input())
    S = input()
    N, dat = init(N)
    for i, si in enumerate(S, 1):
        update(i, si)
    Q = int(input())
    for i in range(Q):
        com, x, y = input().split()
        if com == '1':
            if y == S[int(x)-1]:
                continue
            update(int(x), y)
        elif com == '2':
            # print(query(int(x), int(y)+1, 0, 0, N))
            print(len(query(int(x), int(y)+1, 0, 0, N)))
    # print(dat)

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


def index(a, x):
    'Locate the leftmost value exactly equal to x'
    i = bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    raise ValueError

# ポイント
# 文字列update
# 文字種の高速取得

# 文字種の高速取得について考えてみる
# 文字のidxを取得しておいて二分探索することで高速に取得することが可能


N = read_a_int()
S = read()[:-1]

from collections import defaultdict
from bisect import bisect_left, bisect_right, insort_left
char_idxs = defaultdict(lambda: [])
for i, s in enumerate(S):
    char_idxs[s].append(i)


def get_syurui(char_idxs, l, r):
    ret = 0
    for v in char_idxs.values():
        l_idx = bisect_left(v, l)
        r_idx = bisect_right(v, r)
        # print(v,l_idx,r_idx)
        if r_idx - l_idx > 0:
            ret += 1
    return ret


Q = read_a_int()
for q in range(Q):
    com, a, b = read().split()
    if int(com) == 2:
        a, b = int(a) - 1, int(b) - 1
        print(get_syurui(char_idxs, a, b))
    else:
        i = int(a) - 1
        if S[i] == b:
            continue
        # i文字目を消す
        tmp = char_idxs[S[i]]
        del char_idxs[S[i]][index(tmp, i)]
        # cのidxに挿入し直す
        insort_left(char_idxs[b], i)

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())
import bisect
for i in range(Q):
    q,a,b = map(str,input().split())
    a = int(a)
    a -= 1
    if q == '1':
        x = s[a]
        s[a] = b
        r = bisect.bisect_left(A[ord(x)-97])
        del A[ord(x)-97][r]
        A[ord(b)-97].append(a)
    if q == '2':
        ans = 0
        b = int(b)
        b -= 1
        for i in range(26):
            B = A[i]
            B.sort()
            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:
from bisect import bisect_left, bisect_right, insort_left

n = int(input())
s = input()
q = int(input())
qs = [list(input().split()) for _ in range(q)]

abc = [[-1, n] for _ in range(26)]
for i, e in enumerate(s):
    j = ord(e) - ord("a")
    abc[j].append(i)

for i in range(26):
    abc[i].sort()

for t, e1, e2 in qs:
    if t == "1":
        i = int(e1) - 1
        c = e2

        let = s[i]

        idx = ord(let) - ord("a")
        popi = bisect_left(abc[idx], i)
        abc[idx].pop(popi)

        idx = ord(c) - ord("a")
        insort_left(abc[idx], i)

    else:
        l = int(e1) - 1
        r = int(e2) - 1

        cnt = 0
        for i in range(26):
            li = bisect_left(abc[i], l)
            ri = bisect_right(abc[i], r)
            if li != ri:
                cnt += 1

        print(cnt)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

n = int(input())
s = list(input())
q = int(input())
ql = [input().split() for _ in range(q)]

w = 800
sl = []
dl = []
for i in range((n+w-1)//w):
    ss = s[i*w:min((i+1)*w,n)]
    dl.append(set(ss))
    sl.append(ss)
if len(dl)%2==1:
    dl.append(set())
dl2 = []
for i in range(len(dl)//2):
    dl2.append(dl[2*i] | dl[2*i+1])
for i in range(q):
    if ql[i][0]=="1":
        idx = int(ql[i][1])-1
        c = ql[i][2]

        ss = sl[idx//w]
        ss[idx%w] = c
        s[idx] = c
        sl[idx//w] = ss
        dl[idx//w] = set(ss)
        if (idx//w)%2==0:
            dl2[idx//w//2] = dl[idx//w] | dl[idx//w+1]
        else:
            dl2[idx//w//2] = dl[idx//w-1] | dl[idx//w]
    else:
        l = int(ql[i][1])-1
        r = int(ql[i][2])-1

        li = l//w
        ri = r//w
        if abs(li-ri)<2:
            print(len(set(s[l:r+1])))
        else:
            u = set(sl[li][l%w:])
            u = u | set(sl[ri][:r%w+1])
            if (li+1)%2==1:
                u = u | dl[li+1]
                li += 1
            if (ri-1)%2==0:
                u = u | dl[ri-1]
                ri -= 1
            for j in range(li+1,ri,2):
                u = u | dl2[j]
            print(len(u))

Submission status: Runtime Error

Solution code:
import sys

N,M, K = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(M)]
CD = [list(map(int,input().split())) for i in range(K)]


#xの木の根を求める
def root(x,li): 
    if li[x] == x:
        return x
    else:
        li[x] = root(li[x],li)
        return li[x]

#xとyが同じ集合に属するかの判定
def find(x,y,li):
    return root(x,li) == root(y,li)

#xとｙを併合
def union(x,y,li):
    x = root(x,li)
    y = root(y,li)
    if x==y:
        return
    else:
        li[x] = y

li1 = [i for i in range(N)]
li2 = [i for i in range(N)]

for i in range(M):
    a = AB[i][0]
    b = AB[i][1]
    union(a-1,b-1,li1)
# for i in range(K):
#     c = CD[i][0]
#     d = CD[i][1]
#     union(c-1,d-1,li2)
sys.exit()
mm = [0 for i in range(max(li1))]
for i in range(N):
    m = root(i,li1)
    mm[m-1] += 1

# print(li1)
# print(mm)
ans = [mm[li1[i]-1]-1 for i in range(N)]
# print(ans)

for i in range(M):
    a = AB[i][0]
    b = AB[i][1]
    ans[a-1] -= 1
    ans[b-1] -= 1


for i in range(K):
    c = CD[i][0]
    d = CD[i][1]
    if find(c-1,d-1,li1):
        ans[c-1] -= 1
        ans[d-1] -= 1
print(*ans)

Submission status: Runtime Error

Solution code:
def main():
    n = int(input())
    S = input()
    q = int(input())
    for _ in range(q):
        t, i, c = input().split()
        if t == '1':
            S[int(i)-1] = c
        else:
            print(len(set(S[int(i)-1:int(c)])))
        
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def q1(s, i, c):
  ss = list(s)
  ss[i-1] = c
  return ''.join(ss)
  
def q2(s, l, r):
  if (r-l):
    substr = s[(l-1):r]
  else:
    substr = s[l]
  return len(set(list(substr)))

n = int(input())
s = input()
nq = int(input())

q = []
t = []
for _ in range(nq):
  l = list(input().split())
  if l[0]=='1':
    s = q1(s, int(l[1]), l[2])
  else:
    print(q2(s, int(l[1]), int(l[2])))

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


def index(a, x):
    'Locate the leftmost value exactly equal to x'
    i = bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    raise ValueError

# ポイント
# 文字列update
# 文字種の高速取得

# 文字種の高速取得について考えてみる
# 文字のidxを取得しておいて二分探索することで高速に取得することが可能


N = read_a_int()
S = read()[:-1]

from collections import defaultdict
from bisect import bisect_left, bisect_right, insort_left
char_idxs = defaultdict(lambda: [])
for i, s in enumerate(S):
    char_idxs[s].append(i)


def get_syurui(char_idxs, l, r):
    ret = 0
    for v in char_idxs.values():
        l_idx = bisect_left(v, l)
        r_idx = bisect_right(v, r)
        # print(v,l_idx,r_idx)
        if r_idx - l_idx > 0:
            ret += 1
    return ret


Q = read_a_int()
for q in range(Q):
    com, a, b = read().split()
    if int(com) == 2:
        a, b = int(a) - 1, int(b) - 1
        print(get_syurui(char_idxs, a, b))
    else:
        i = int(a) - 1
        if S[i] == b:
            continue
        # i文字目を消す
        tmp = char_idxs[S[i]]
        del tmp[index(tmp, i)]
        char_idxs[S[i]] = tmp
        # cのidxに挿入し直す
        insort_left(char_idxs[b], i)

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
n = int(input())
s = input()
quenum = int(input())
q = []
for i in range(quenum):
    q.append(input().split())

N0 = 2**(n-1).bit_length()
INF = 2**31-1
data = [set()]*(2*N0)
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data[k] = data[2*k+1]  | data[2*k+2]
def query(l, r):
    L = l + N0; R = r + N0
    s = set()
    while L < R:
        if R & 1:
            R -= 1
            s = s | data[R-1]

        if L & 1:
            s = s |  data[L-1]
            L += 1
        L >>= 1; R >>= 1
    return s

for i in range(len(s)):
    update(i,set(s[i]))

for que in q:
    if que[0] == "1":
        update(int(que[1])-1,set(que[2]))
    else:
        print(len(query(int(que[1])-1,int(que[2]))))

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
n = int(input())
s = input()
n = int(input())
q = []
for i in range(n):
    q.append(input().split())

N0 = 2**(n-1).bit_length()
INF = 2**31-1
data = [set()]*(2*N0)
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data[k] = data[2*k+1]  | data[2*k+2]
def query(l, r):
    L = l + N0; R = r + N0
    s = set()
    while L < R:
        if R & 1:
            R -= 1
            s = s | data[R-1]

        if L & 1:
            s = s |  data[L-1]
            L += 1
        L >>= 1; R >>= 1
    return s

for i in range(len(s)):
    update(i,set(s[i]))

for que in q:
    if que[0] == "1":
        update(int(que[1])-1,set(que[2]))
    else:
        print(len(query(int(que[1])-1,int(que[2]))))

Submission status: Runtime Error

Solution code:
n = int(input())
import sys
input = sys.stdin.readline
s = tuple(input())[:-1]
q = int(input())

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

Submission status: Runtime Error

Solution code:
import collections
N = int(input())
S = input()
Q = int(input())
querys = [list(map(str, input().split())) for i in range(Q)]
for i in range(Q):
    if querys[i][0] == "1":
        S = S[:int(querys[0][1])-1] + query[2] + S[int(querys[0][1]):]
    else:
        c = collections.Counter(S[int(query[1]) - 1:int(query[2])])
        print(len(c))

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():
    #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 java.util.*;
 
 public class Main {
     public static void main(String args[] ) throws Exception {
		 Scanner sc = new Scanner(System.in);
		 String S = sc.nextLine() ;

		 int N = Integer.valueOf( S ) ;
		 S = sc.nextLine() ;

		 Map<Character,TreeSet<Integer>> map = new HashMap<>() ;
		 char[] arr = new char[N] ;
		 for ( int j=0 ; j<S.length() ; j++ )  {
			 char c = S.charAt(j) ;
			 arr[j] = c  ;
			if ( ! map.containsKey( c ) )
				map.put( c , new TreeSet<>() );
			map.get(c).add ( j ) ;
		 }

		 S = sc.nextLine() ;
		 int Q = Integer.valueOf( S ) ;
		 for ( int j=0 ; j<Q ; j++ ) {
			 String[] query = sc.nextLine().split(" ") ;
			 int cmd = Integer.valueOf( query[0] ) ;
			 if ( cmd == 1 ) {
				 int  p1  = Integer.valueOf( query[1] )  -1 ;
				 char c1  = query[2].charAt(0) ;
				 
				 map.get( arr[p1] ).remove( p1 ) ; 
				 arr[p1] = c1 ;
				if ( ! map.containsKey( c1 ) )
					map.put( c1 , new TreeSet<>() );
				 map.get( c1 ).add ( p1 ) ; 

			 } else {
				 int p1 = Integer.valueOf( query[1] ) -1 ;
				 int p2 = Integer.valueOf( query[2] ) -1 ;
				 int cc = 0 ;
				 for ( Character x : map.keySet() ) {
					 if ( map.get(x).first() > p2 ) continue ;
					 if ( map.get(x).last()  < p1 ) continue ;
					SortedSet<Integer> s1 = map.get(x).tailSet( p1 ) ;
					if ( s1.size() <=0 ) continue ;
					s1 = map.get(x).headSet( p2+1 ) ;
					if ( s1.size() >= 1 )
						cc ++ ;
				 }
				System.out.println( cc ) ;
			 }


		 }


	 }
}

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())
import bisect
for i in range(Q):
    q,a,b = map(str,input().split())
    a = int(a)
    a -= 1
    if q == '1':
        x = s[a]
        s[a] = b
        B = A[ord(x)-97]
        r = bisect.bisect_left(B,a)
        A[ord(x)-97].pop(r)
        A[ord(b)-97].append(a)
    else:
        ans = 0
        b = int(b)
        b -= 1
        for i in range(26):
            B = A[i]
            B.sort()
            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:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

tmp = 'abcdefghijklmnopqrstuvwxyz'
n = I()
s = list(input())
cnt = [0]*n
alpha = defaultdict(list)
for i in range(n):
    alpha[s[i]].append(i)

q = I()
f = defaultdict(list)
for _ in range(q):
    num, i, c = input().split()
    if int(num) == 1:
        alpha[s[int(i)-1]].remove(int(i)-1)
        alpha[c].append(int(i)-1)
        alpha[c].sort()
    else:
        cnt = 0
        for j in range(26):
            l = bl(alpha[tmp[j]],int(i)-1)
            r = br(alpha[tmp[j]],int(c)-1)
            if r-l > 0:
                cnt += 1
            #print(tmp[j],alpha[tmp[j]],int(i),int(c),l,r)
        print(cnt)

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:
    if S[int(x[2]) - 1] != x[4]:
      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:
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.N0:
                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.N0:
                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()