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: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    S = input()
    Q = int(input())
    S_list = list(S)
    Query = [input().split() for i in range(Q)]
    for q in Query:
        if q[0] == "1":
            a = int(q[1]) - 1
            S_list[a] = q[2]
        elif q[0] == "2":
            a = int(q[1]) -1
            b = int(q[2])
            print(len(set(S_list[a:b])))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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

for q in range(Q):
        t, i, c = input().split()

        if t == '1':
                S[int(i)-1] = c
        elif t == '2':
                cnt=set()
                for j in range(int(i), int(c)+1):
                        cnt.add(S[int(j)-1])
                print(len(cnt))

Submission status: Time Limit Exceeded

Solution code:
def case1(string, x, c):
    li = [a for a in string]
    li[x-1] = c
    s = ''.join(li)
    return s


def case2(string, start, end):
    s = set(string[start-1:end])
    print(len(s))


i = int(input())  # skip this
char_list = input()
Q = int(input())

for i in range(Q):
    tokens = [int(x) if x.isdigit() else x for x in input().split()]
    if tokens[0] == 1:
        char_list = case1(char_list, tokens[1], tokens[2])
    elif tokens[0] == 2:
        case2(char_list, tokens[1], tokens[2])
    else:
        pass

Submission status: Time Limit Exceeded

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

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

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[k*2+1],seg[k*2+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&1 == 0:
            res = segfunc(res,seg[p])
        if q&1 == 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

#####単位元######
ide_ele = set()

#num:n以上の最小の2のべき乗
num =2**(n-1).bit_length()
seg=[ide_ele]*2*num

init(s)
#print(seg)

for i in range(q):
    t, x, y = map(str, input().split())
    if t == '2':
        l = int(x)-1
        r = int(y)-1
        print(len(query(l, r+1)))
    else:
        i = int(x)-1
        c = {y}
        update(i, c)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
q=int(input())
No=2
while n>No:
    No*=2
tree=[set()]*(2*No)
def add(k,x):
    k+=No-1
    tree[k]=set([x])
    while k>0:
        k=(k-1)//2
        tree[k]=tree[2*k+1]|tree[2*k+2]
def query(l,r):
    L=l+No-1
    R=r+No-1
    s=set()
    while L<=R:
        if R&1:
            s|=tree[R]
            R-=2
        else:
            R-=1
        if L&1:
            L-=1
        else:
            s|=tree[L]
        L>>=1;R>>=1
    return len(s)
for i,j in enumerate(s):
    add(i,j)
for i in range(q):
    a,b,c=input().split()
    a,b=int(a),int(b)
    if a==1:
        add(b-1,c)
    if a==2:
        c=int(c)
        print(query(b-1,c-1))

Submission status: Time Limit Exceeded

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


seg = [set([]) for _ in range(2*N-1)]
def update(k, x):
    k += N-1
    seg[k] = {x:1}
    while k:
        k = (k-1)//2
        seg[k] = set(seg[2*k+1]) | set(seg[2*k+2])
        
def query(p, q):
    p += N-1
    q += N-2
    res = set([])
    while q-p > 1:
        if p&1 == 0:
            if p >= N-1:
                seg[p] = set(seg[p])
            res = res | seg[p]
        if q&1 == 1:
            if q >= N-1:
                seg[q] = set(seg[q])
            res = res | seg[q]
            q -= 1
        p //= 2
        q = (q-1)//2
    if p == q:
        if q >= N-1:
            seg[q] = set(seg[q])
        res |= seg[q]
    else:
        if p >= N-1:
            seg[p] = set(seg[p])
        if q >= N-1:
            seg[q] = set(seg[q])
        res |= seg[p] | seg[q]
    return len(res)

for i in range(N):
    update(i, S[i])
for _ in range(Q):
    a,b,c = map(str, input().split())
    if a == "1":
        update(int(b)-1, c)
    else:
        print(query(int(b)-1, int(c)))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
q=int(input())
for i in range(q):
    query=list(input().split())
    if query[0]=='1':
        s[int(query[1])-1]=query[2]
    else:
        cut=s[int(query[1])-1:int(query[2])]
        ans=set(cut)
        print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
qN = int(input())
#Q=[list(map(int, input().split())) for i in range(qN)]
#Q=[list(input().split()) for i in range(qN)]

#print(S)
#print(Q)

for i in range(qN):
    Q=list(input().split())
    if int(Q[0])==1:
        S[int(Q[1])-1]=Q[2]
    else :
        if int(Q[2])!=N:
            #print(S[int(Q[i][1])-1:int(Q[i][2])])
            print(len(set(S[int(Q[1])-1:int(Q[2])])))
        else: 
            print(len(set(S[int(Q[1])-1:])))

Submission status: Time Limit Exceeded

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

for i in range(q):
    type,iq,q=map(str,input().split())
    if type == "1":
        s = s[:int(iq)-1] + q + s[int(iq):]
    else:
        print(len(set(s[int(iq)-1:int(q)])))

Submission status: Time Limit Exceeded

Solution code:
import string
import numpy
L = len(string.ascii_lowercase)
class BIT:

    def __init__(self, N):
        self.bit = numpy.zeros((N + 1, L))
        self.N = N

    def sum_all(self, i):
        s = numpy.zeros(L)
        while i > 0:
            s += self.bit[i]
            i -= i & -i
        return s

    def add(self, i, x, c):
        c = ord(c) - ord('a')
        while i <= self.N:
            self.bit[i][c] += x
            i += i & -i

def solve(N, S, Q, query):
    bits = BIT(N)

    for i, c in enumerate(S):
        bits.add(i + 1, 1, c)

    ans = []
    for q, i, cr in query:
        if q == "1":
            i = int(i)
            if S[i-1] != cr:
                bits.add(i, -1, S[i-1])
                bits.add(i, 1, cr)
                S[i-1] = cr
        else:
            l = int(i)
            r = int(cr)
            diff = bits.sum_all(r) - bits.sum_all(l)
            k = ord(S[l-1]) - ord('a')
            diff[k] += 1
            ans.append(str(numpy.count_nonzero(diff)))
    return "\n".join(ans)

if __name__ == "__main__":
    N = int(input())
    S = list(input())
    Q = int(input())

    query = []
    for _ in range(Q):
        query.append(tuple(input().split(" ")))
    print(solve(N, S, Q, query))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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

queries = []

for i in range(Q):
    queries.append(list(input().split()))

for query in queries:
    if query[0] == '1':
        S = S[:int(query[1])-1] + query[2] + S[int(query[1]):]
    else:
        print(len(set(S[int(query[1])-1: int(query[2])])))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
q=int(input())
for i in range(q):
    t,x1,x2=input().split()
    t=int(t)
    if t==1:
        x1=int(x1)
        s[x1-1] = x2
    else:
        l=int(x1)
        r=int(x2)
        judge=set(s[l-1:r])
        print(len(judge))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
Q = int(input())
query = []
moji = []
for i in S:
    moji.append(i)
for _ in range(Q):
    mode, tmp2, tmp3 = input().split()
    if mode == '1':
        i = int(tmp2)
        c = tmp3

        moji[i-1] = c

    else:
        l = int(tmp2)
        r = int(tmp3)

        print(len(set(moji[l-1:r])))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

class Bit:
    def __init__(self, n,ls):
        self.size=n
        self.tree=[Counter() for _ in range(n+1)]
        self.ls=['']*(n+1)

    def sum(self, i, j):
        i-=1
        res1=Counter()
        res2=Counter()
        while i>0:
            res1 += self.tree[i]
            i -= i&-i
        while j>0:
            res2 += self.tree[j]
            j -= j&-j
        return len(res2-res1)
    
    def add(self, i, x):
        self.ls[i] = x
        while i <= self.size:
            self.tree[i][x]+=1
            i += i & -i

    def change(self, i, x):
        original = self.ls[i]
        self.ls[i]=x
        while i <= self.size:
            self.tree[i][original] -=1
            self.tree[i][x] +=1
            i += i & -i
        

n = int(input())
s = list(input())
bit=Bit(n,s)

for i in range(n):
    bit.add(i+1, s[i])

q = int(input())
for _ in range(q):
    query, a, b = input().split()
    if query=='1':
        bit.change(int(a),b)
    else:
        ans=bit.sum(int(a), int(b))
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
leng = int(input())

strin = input()

num = int(input())

a = [list(input().split()) for i in range(num)]

for kk in range(num):
    
    if int(a[kk][0]) == 1:
        #print(strin)
        strin = strin[:int((int(a[kk][1])-1))] + a[kk][2]+ strin[int((a[kk][1])):]
        #print(" "+str(strin))
    else:
        pp = strin[int((int(a[kk][1])-1)):int((a[kk][2]))]
        print(len(set(pp)))

Submission status: Time Limit Exceeded

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

for i in range(num_q):
    q = input().split()
    if q[0] == "1":
        s[int(q[1])-1] = q[2]
    else:
        s_sub = s[int(q[1])-1:int(q[2])]
        print(len(set(s_sub)))

Submission status: Time Limit Exceeded

Solution code:
input()
S=input()
for i in range(int(input())):
    q,i,c=input().split()
    q=int(q)-1
    i=int(i)-1
    if q:print(len(set(S[i:int(c)])))
    else:S=S[:i]+c+S[i+1:]

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
q=int(input())
for i in range(q):
    t,x1,x2=input().split()
    t=int(t)
    if t==1:
        x1=int(x1)
        s[x1-1] = x2
    else:
        l=int(x1)
        r=int(x2)
        cnt=0
        judge=set()
        for j in range(n)[l-1:r]:
            if s[j] not in judge:
                judge.add(s[j])
                cnt+=1
        print(cnt)

Submission status: Time Limit Exceeded

Solution code:
input()
S=input()
for i in range(int(input())):
    q,i,c=input().split()
    q=int(q)-1
    i=int(i)-1
    if q:print(len(set(list(S[i:int(c)]))))
    else:S=S[:i]+c+S[i+1:]

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
s=list(input())
q=int(input())
for i in range(q):
  v,c,x=input().split()
  v=int(v)
  c=int(c)
  if v==1:
    s[c-1]=x
  else:
    x=int(x)
    print(len(set(s[c-1:x])))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8
from fractions import gcd 
from functools import reduce
import sys
sys.setrecursionlimit(200000000)
from inspect import currentframe


# my functions here!
#標準エラー出力
def printargs2err(*args):
    names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' : '+repr(arg) for arg in args),file=sys.stderr)






def printglobals():
    for (symbol, value) in globals().items():
        print('symbol="%s"、value=%s' % (symbol, value),file=sys.stderr)
def printlocals():
    for (symbol, value) in locals().items():
        print('symbol="%s"、value=%s' % (symbol, value),file=sys.stderr)
#入力（後でいじる）
def pin(type=int):
    return map(type,input().split())
     
#input
from collections import Counter 
def resolve():
    N,=pin()
    S=list(input())
    Q,=pin()
    for q in range(Q):
        a,b,c=input().split()
        if a=="1":
            S[int(b)-1]=c
        elif a=="2":
            
            #print(S[int(b)-1:int(c)])
            x = Counter(S[int(b)-1:int(c)])
            
            print(len(x))
            
    #print([["NA","YYMM"],["MMYY","AMBIGUOUS"]][cMMYY][cYYMM])
#if __name__=="__main__":resolve()

"""
#printデバッグ消した？
#前の問題の結果見てないのに次の問題に行くの？
"""
"""
お前カッコ閉じるの忘れてるだろ
"""
import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7"""
        output = """3
1
5"""
        self.assertIO(input, output)

if __name__=="__main__":resolve()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
Q = int(input())
Query = []
for i in range(Q):
    l = input().split()
    if l[0] == '1':
        l[0] = int(l[0])
        l[1] = int(l[1])
    else:
        l = list(map(int, l))
    Query.append(l)

for i in range(Q):
    if Query[i][0] == 1:
        S[Query[i][1] - 1] = Query[i][2]
    else:
        string_list = list(S[(Query[i][1] - 1):(Query[i][2])])
        ans = len(set(string_list))
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, Counter, namedtuple, deque
from bisect import bisect_left
from functools import reduce
import math

NN = 2020202
MOD = 10**9+7
INF = float("inf")


class BIT():
    def __init__(self, n):
        self.n = n
        self.bit = []
        for i in range(self.n+1):
            self.bit.append(defaultdict(int))
        self.s = ["*"]*self.n

    def _sum(self, i):
        s = defaultdict(int)
        while i > 0:
            for k, v in self.bit[i].items():
                s[k] += v
            i -= i & -i
        return s

    def add(self, i, val):
        old = self.s[i-1]
        self.s[i - 1] = val
        # print(old, val)
        if old == val:
            return

        while i <= self.n:
            # print(i)
            self.bit[i][val] += 1
            self.bit[i][old] -= 1
            if self.bit[i][old] <= 0:
                del self.bit[i][old]

            i += i & -i

    def get_sum(self, l, r):
        A = self._sum(r)
        B = self._sum(l-1)
        # print(A, B)
        for k,v in B.items():
            A[k] -= v
            if A[k] <= 0:
                del A[k]

        return len(A)
        # return self._sum(r) - self._sum(l)


N = int(input())
S = input()
Q = int(input())
query = [list(input().split()) for i in range(Q)]

tree = BIT(N)
for i in range(N):
    tree.add(i+1, S[i])
# print(tree.bit)

for i in range(Q):
    if query[i][0] == "1":
        tree.add(int(query[i][1]), query[i][2])
        # print(tree.bit)
    if query[i][0] == "2":
        print(tree.get_sum(int(query[i][1]), int(query[i][2])))

Submission status: Time Limit Exceeded

Solution code:
import string
import numpy
L = len(string.ascii_lowercase)
class BIT:

    def __init__(self, N):
        self.bit = numpy.array([[0 for _ in range(L)] for _ in range(N + 1)])
        self.N = N

    def sum(self, i, c):
        s = 0
        c = ord(c) - ord('a')
        while i > 0:
            s += self.bit[i][c]
            i -= i & -i
        return s

    def sum_all(self, i):
        s = numpy.array([0 for _ in range(L)])
        while i > 0:
            s += self.bit[i]
            i -= i & -i
        return s

    def add(self, i, x, c):
        c = ord(c) - ord('a')
        while i <= self.N:
            self.bit[i][c] += x
            i += i & -i

def solve(N, S, Q, query):
    bits = BIT(N)

    for i, c in enumerate(S):
        bits.add(i + 1, 1, c)

    ans = []
    for q, i, cr in query:
        if q == "1":
            i = int(i)
            if S[i-1] != cr:
                bits.add(i, -1, S[i-1])
                bits.add(i, 1, cr)
                S[i-1] = cr
        else:
            l = int(i)
            r = int(cr)
            diff = bits.sum_all(r) - bits.sum_all(l)
            k = ord(S[l-1]) - ord('a')
            diff[k] += 1
            ans.append(str(numpy.count_nonzero(diff)))
    return "\n".join(ans)

if __name__ == "__main__":
    N = int(input())
    S = list(input())
    Q = int(input())

    query = []
    for _ in range(Q):
        query.append(tuple(input().split(" ")))
    print(solve(N, S, Q, query))

Submission status: Time Limit Exceeded

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):
    query = querys[i]
    if query[0] == "1":
        qi = int(query[1])
        S = S[:qi-1] + query[2] + S[qi:]
    else:
        c = collections.Counter(S[int(query[1]) - 1:int(query[2])])
        print(len(c))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


N = [int(i) for i in input().split()][0]
array = np.zeros([N, 26])
string = list(input())
strs = []
a0 = ord("a")
for n, s in enumerate(string):
  i = ord(s) - a0
  array[n, i] = 1
  strs.append(i)
qumsum = np.cumsum(array, axis=0)

Q = [int(i) for i in input().split()][0]
queries =[[i for i in input().split()] for _ in range(Q)]

changed = []
for que in queries:
  type_, pos, st = que
  if type_ == "1":
    p = int(pos) - 1
    s1 = strs[p]
    array[p, s1] = 0
    s2 = ord(st) - a0
    strs[p] = s2
    array[p, s2] = 1
    changed.append((p, s1, s2))
  else:
    lq = int(pos) - 1
    rq = int(st)
    sums = qumsum[rq-1, :] - qumsum[lq, :] + array[lq, :]
    for change in changed:
      p, s1, s2 = change
      if lq <= p < rq:
        sums[s1] -= 1
        sums[s2] += 1
    print(np.sum(sums>0))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(input())
q = int(input())
for _ in range(q):
  a,b,c = input().split()
  b = int(b)-1
  if a=='1':
    l[b] = c
  else:
    s = set()
    for t in l[b:int(c)]:
      s |= {t}
    print(len(s))

Submission status: Time Limit Exceeded

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

"""考え方
対応が必要そうなところだけSを修正する。
そうでないと、Sを修正するところの計算量が大きくなりすぎてしまいそう
"""

_dict = {}
for q in range(Q):
  _, i, c = input().split()
  if _ == '1':
    _dict[int(i)] = c
  else:
    for _ in range(int(i),int(c)+1):
      
      if _ in _dict: 
        S = S[:int(_)-1] + _dict[_] + S[int(_):]
        del _dict[_]
    ans = len(set(S[int(i)-1:int(c)]))
    print(ans)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    q_type, a, b = input().split()
    
    if (int(q_type) == 1):
        if (S[int(a) - 1] != b):
            S[int(a) - 1] = b
            
        continue
    
    q_str = S[int(a)-1:int(b)]
    num = len(set(q_str))
    print(num)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
num_q = int(input())

for i in range(num_q):
    q = input().split()
    if q[0] == "1":
        s = list(s)
        s[int(q[1])-1] = q[2]
        s = str(s)
    else:
        s_sub = s[int(q[1])-1:int(q[2])]
        print(len(set(s_sub)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
qN = int(input())
#Q=[list(map(int, input().split())) for i in range(qN)]
Q=[list(input().split()) for i in range(qN)]

#print(S)
#print(Q)

for i in range(qN):
    if int(Q[i][0])==1:
        S[int(Q[i][1])-1]=Q[i][2]
    else :
        if int(Q[i][2])!=N:
            #print(S[int(Q[i][1])-1:int(Q[i][2])])
            print(len(set(S[int(Q[i][1])-1:int(Q[i][2])])))
        else: 
            print(len(set(S[int(Q[i][1])-1:])))          
            
        #count=len(set(S[Q[i][1]-1:end]))  
        #print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=[x for x in input()]
q=int(input())
for i in range(q):
  t,a,b = input().split()
  if t == '1':
    a = int(a)
    s[a-1] = b
  elif t == '2':
    a = int(a)
    b = int(b)
    print(len(set(s[a-1:b])))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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

queries = []

for i in range(Q):
    queries.append(list(input().split()))

for query in queries:
    if query[0] == '1':
        S = S[:int(query[1])-1] + query[2] + S[int(query[1]):]
    else:
        print(len(set(S[int(query[1])-1: int(query[2])])))

Submission status: Time Limit Exceeded

Solution code:
def solve(S,a,b):
    #print("Solve",S[a:b],set(S[a:b]))
    return len(set(S[a:b]))
 
# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
def main():
    N=int(input())
    S=list(input())
    Q=int(input())
    for i in range(Q):
        t,a,b=input().split()
        if t=="2": print(solve(S,int(a)-1,int(b)))
        if t=="1": S[int(a)-1]=b
        #print("#",N,S,i)
 
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
l = []
for i in range(n):
    l.append(s[i])
q = int(input())
query = [input().split() for _ in range(q)]
for i in query:
    if i[0] == '1':
        l[int(i[1])-1] = i[2]
    else: 
        r = l[int(i[1])-1:int(i[2])]
        print(len(set(r)))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
s = input()
q = int(input())
for i in range(q):
    x,y,z = input().split()
    y = int(y)
    if x == '1':
        s = ''.join([s[:y-1],z,s[y:]])
    else:
        z = int(z)
        print(len(Counter(s[y-1:z])))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def countChar(sList):
  return len(set(sList))

n = int(input())
sList = list(input())
q = int(input())

for i in range(q):
  t, l, r = map(str, input().split())
  if t == '2':
    print(countChar(sList[int(l)-1:int(r)]))
  elif t == '1':
    sList[int(l)-1] = r

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    q_type, a, b = input().split()
    
    if (int(q_type) == 1):
        if (S[int(a) - 1] != b):
            str_list = list(S)
            str_list[int(a) - 1] = b
            S = "".join(str_list)
            
        continue
    
    q_str = S[int(a)-1:int(b)]
    num = len(set(list(q_str)))
    print(num)

Submission status: Time Limit Exceeded

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

def main():
    """"ここに今までのコード"""
    n=int(input())
    s=list(input())
    k=int(input())

    result=[]

    for _ in range(k):
        a,b,c=map(str,input().split())
        if a=="1":
            s[int(b)-1]=c
            #print(s)
        else:
            temp=set(s[int(b)-1:int(c)])
            result.append(len(temp))

    for item in result:
        print(item)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N=int(input())
S=list(input())
Q=int(input())

for _ in range(Q):
  t,i,c=input().split()
  if t=="1":
    S[int(i)-1]=c
  else:
    print(len(Counter(S[int(i)-1:int(c)])))

Submission status: Time Limit Exceeded

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

for _ in range(q):
    a, b, c = map(str, input().split())
    if a == "1":
        s = s[:int(b)-1] + c + s[int(b):]
    else:
        x = s[int(b)-1:int(c)]
        print(len(set(x)))

Submission status: Time Limit Exceeded

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

s_dict = dict()
for i in range(len(s)):
    if s[i] in s_dict:
        s_dict[s[i]] += 1
    else:
        s_dict[s[i]] = 1

for i in range(q):
    a, b, c = map(str, input().split())
    if a == '1':
        b = int(b) - 1
        # 文字挿入
        temp = s[b]
        if s_dict[temp] > 0:
            s_dict[temp] -= 1
        s[b] = c
        if c in s_dict:
            s_dict[c] += 1
        else:
            s_dict[c] = 1
    elif a == '2':
        b = int(b) - 1
        c = int(c) - 1
        # 文字種類カウント
        temp = s[b:c + 1]
        count = 0
        for k, v in s_dict.items():
            if v > 0:
                if k in temp:
                    count += 1
        print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

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

res = []
for i in range(Q):
    qtype, a, b = input().split()
    # print('QTYPE: {}'.format(qtype))
    # print('{} {} {}'.format(qtype, a, b))
    if qtype == '1':
        a = int(a)
        S = S[:a-1] + b + S[a:]
        # print(S)
    elif qtype == '2':
        a = int(a)
        b = int(b)
        c = S[a-1:b]
        res.append(len(set(c)))

for r in res:
    print(r)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    t, a, b = input().split()

    #type1
    if t == '1':
        a = int(a)
        S[a-1]= b

    #type2
    elif t == '2':
        a = int(a)
        b = int(b)

        print(len(set(S[(a-1):(b)])))

    #somtheing wrong
    else:
        exit(-1)

Submission status: Time Limit Exceeded

Solution code:
import math

class SegmentTree:
	def update(self, i, x):
		i += self.num_leaves-1
		self.nodes[i] = x
		while i > 0:
			i = (i-1) // 2
			self.nodes[i] = self.monoid(self.nodes[i*2+1],self.nodes[i*2+2])
			
	def query(self,a,b,k=0,l=0,r=None):
		if r is None:
			r = self.num_leaves
		if r <= a or b <= l:
			return None
		if a <= l and r <= b:
			return self.nodes[k]
		else:
			vl = self.query(a,b,k*2+1,l,(l+r)//2)
			vr = self.query(a,b,k*2+2,(l+r)//2,r)
			return self.monoid(vl,vr)
		
	def __init__(self, monoid, values):
		self.monoid = lambda x, y : monoid(x, y) if x is not None and y is not None else x or y
		self.num_leaves = 2**math.ceil(math.log2(len(values)))
		self.nodes = [None for i in range(self.num_leaves*2-1)]
		for i in range(len(values)):
			self.update(i,values[i])

def func(a,b):
    return a.union(b)

n = int(input())
s = input()
q = int(input())
Q = [input().split() for i in range(q)]

values = [set(s[i]) for i in range(n)]

sgt = SegmentTree(func, values)

for i in range(q):
    a,b,c = Q[i]
    b = int(b)
    if a == "1":
        sgt.update(b-1,set(c))
    else:
        c = int(c)
        val = sgt.query(b-1,c)
        print(len(val))

Submission status: Time Limit Exceeded

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

for i in range(Q):
    x, y, z = input().split()
    if x == "1":
        S[int(y)-1] = z
    else:
        print(len(set(S[int(y)-1:int(z)])))

Submission status: Time Limit Exceeded

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):
    query = querys[i]
    if query[0] == "1":
        qi = int(query[1])
        S = S[:qi-1] + query[2] + S[qi:]
    else:
        rq = int(query[2])
        lq = int(query[1])
        c = collections.Counter(S[lq - 1:rq])
        print(len(c))

Submission status: Time Limit Exceeded

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

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

def segfunc(x,y):
    return x | y

def init(init_val):
    #set_val
    for i in range(n):
        seg[i+num-1]=set(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[k*2+1],seg[k*2+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&1 == 0:
            res = segfunc(res,seg[p])
        if q&1 == 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

#####単位元######
ide_ele = set()

#num:n以上の最小の2のべき乗
num =2**(n-1).bit_length()
seg=[ide_ele]*2*num

init(s)
#print(seg)

for i in range(q):
    t, x, y = map(str, input().split())
    if t == '1':
        i = int(x)-1
        c = set(y)
        update(i, c)
    else:
        l = int(x)-1
        r = int(y)-1
        print(len(query(l, r+1)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
  args_0,args_1,args_2 = (str(i) for i in input().split())
  if args_0 == "1":
    S_list = list(S)
    S_list[int(args_1)-1] = args_2
    S = "".join(S_list)
  elif args_0 == "2":
    S_tmp = S[int(args_1)-1:int(args_2)]
    S_dict = {}
    for s in S_tmp:
      S_dict[s] = S_dict.get(s,0)+1
    print(len(S_dict))

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np
from collections import deque
def main():
    n = int(input())
    s = list(input())
    q = int(input())

    result = []
    for _ in range(q):
        query = input().split()
        ty,t1,t2 = query[0],query[1],query[2]
        if ty =='1':
            s[int(t1)-1] = t2
        else:
            t1,t2 = int(t1)-1, int(t2)
            result.append(len(set(s[t1:t2])))

    for r in result:
        print(r)
        
if __name__ == "__main__":
    main()