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:
import sys
import bisect
n = int(sys.stdin.readline().strip())
s = list(sys.stdin.readline().strip())
alpha = "abcdefghijklmnopqrstuvwxyz"
dict = {i:[] for i in alpha}
for i, _s in enumerate(s):
    dict[_s].append(i)
q = int(sys.stdin.readline().strip())
for i in range(q):
    type, first, last = [i for i in sys.stdin.readline().split()]
    if type == "2":
        first = int(first)
        last = int(last)
        first -= 1
        last -= 1
        cnt = 0
        for ls in dict.values():
            if bisect.bisect_left(ls, first) != bisect.bisect_right(ls, last):
                cnt += 1
        print(cnt)
    else:
        first = int(first)
        first -= 1
        _s = s[first]
        dict[_s].pop(bisect.bisect_left(dict[_s], first))
        s[first] = last
        bisect.insort_left(dict[last], first)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
Q = int(input())
query = [tuple(input().split()) for i in range(Q)]

class segtree:
  def __init__(self, initArr, segfunc, idele):
    n = len(initArr)
    stn = 2**((n - 1).bit_length() + 1) - 1
    arr =  [idele for i in range(stn)]
    for i in range(n):
      arr[2**((n - 1).bit_length())+i-1] = initArr[i]
    for i in range(stn-1, 1, -2):
      arr[(i-1)//2] = segfunc(arr[i-1], arr[i])
  
    self.arr = arr
    self.segfunc = segfunc
    self.idele = idele
  
  def update(self, idx, val):
    idx += len(self.arr)//2
    self.arr[idx] = val
    while idx > 0:
      idx = (idx-1)//2
      self.arr[idx] = self.segfunc(self.arr[idx*2+1], self.arr[idx*2+2])
    return None

  def query(self, x, y, i=0, l=0, r=-1):
    if r < 0: r = len(self.arr)//2 + 1
    if r <= x or y <= l: return self.idele
    if x <= l and r <= y: return self.arr[i]
    return self.segfunc(
      self.query(x, y, 2*i+1, l, (l+r)//2),
      self.query(x, y, 2*i+2, (l+r)//2, r)
    )

st = segtree(list(map(set, S)), lambda a, b: a | b, set())

for q, *p in query:
  if q == "1":
    i, r = p
    st.update(int(i)-1, set(r))
  else:
    l, r = map(int, p)
    print(len(st.query(l-1, r)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(str(input()))
Q = int(input())
for i in range(Q):
  a, b, c = map(str, input().split())
  if a == "1":
    S[int(b)-1] = c
  else:
    print(len(set(S[int(b)-1:int(c)])))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = input()
Q = int(input())

dd = []
for i in range(Q):
  d = list(input().split())
  dd.append(d)

for i in range(Q):
  if(dd[i][0] == '1'):
     Stmp = S[:int(dd[i][1])-1] + dd[i][2] + S[int(dd[i][1]):]
     S = Stmp
  else:
     st = S[int(dd[i][1])-1:int(dd[i][2])]
     ss = set()
     for s in st:
       ss.add(s)
     print(len(ss))

Submission status: Time Limit Exceeded

Solution code:
import sys

def read():
    return sys.stdin.readline().rstrip()

def main():
    read()
    s = list(read())
    q = int(read())
    for _ in range(q):
        t, a, b = read().split()
        if t == "1":
            s[int(a) - 1] = b
        if t == "2":
            print(len(set(s[int(a) - 1:int(b)])))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
s = input()
q = int(input())
Q = [list(map(str,input().split())) for _ in range(q)]

for x,y,z in Q:
    if int(x) == 1:
        s = s[:int(y)-1]+z+s[int(y):]
    else:
        c = Counter(s[int(y)-1:int(z)])
        print(len(c.keys()))

Submission status: Time Limit Exceeded

Solution code:

class segtree:

    def __init__(self, n,operator,identity):
        nb = bin(n)[2:]
        bc = sum([int(digit) for digit in nb])
        if bc == 1:
            self.num_end_leaves = 2**(len(nb)-1)
        else:
            self.num_end_leaves = 2**(len(nb))

        self.array = [identity for i in range(self.num_end_leaves * 2)]
        self.identity = identity
        self.operator =operator

    def update(self,x,val):
        actual_x = x+self.num_end_leaves
        self.array[actual_x] = val
        while actual_x > 0 :
            actual_x = actual_x//2
            self.array[actual_x] = self.operator(self.array[actual_x*2],self.array[actual_x*2+1])

    def get(self,q_left,q_right,arr_ind=1,leaf_left=0,depth=0):
        width_of_floor = self.num_end_leaves//(2**depth)
        leaf_right = leaf_left+width_of_floor-1

        if leaf_left > q_right or leaf_right < q_left:
            return  self.identity

        elif leaf_left >= q_left and leaf_right <= q_right:
            return self.array[arr_ind]

        else:
            val_l = self.get(q_left,q_right,2*arr_ind,leaf_left,depth+1)
            val_r = self.get(q_left,q_right,2*arr_ind+1,leaf_left+width_of_floor//2,depth+1)
            return self.operator(val_l,val_r)


import sys
input = sys.stdin.readline
N=int(input())
S=[i for i in input()]
Q=int(input())

s_tree = segtree(N,(lambda x,y:list(set(x+y))),[]) 
for i in range(N):
    s_tree.update(i, [S[i]])

for _ in range(Q):
    f, a, b =(i for i in input().split())
    if int(f)==1:
        s_tree.update(int(a)-1,[b])
    else:
        print(len(s_tree.get(int(a)-1, int(b)-1)))

Submission status: Time Limit Exceeded

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


def solve():
    N = int(input())
    S = list(input())
    Q = int(input())
    res_l = []
    for _ in range(Q):
        q, l, r = input().split()
        if q == '1':
            i,c = int(l), r
            S[i-1] = r
        if q == '2':
            l, r = int(l), int(r)
            res_l.append(len(set(S[l-1:r])))


    res = '\n'.join(map(str,res_l))
    return res

if __name__ == '__main__':
    print(solve())

Submission status: Time Limit Exceeded

Solution code:
import bisect
n=int(input())
S=list(input())
q=int(input())
abc=[[] for _ in range(26)]
for i in range(n):
    s=S[i]
    o=ord(s)-97
    abc[o].append(i)
for _ in range(q):
    t,x,y=input().split()
    t=int(t)
    if t==1:
        x=int(x)
        x-=1
        z=S[x]
        if z!=y:
            o_y=ord(y)-97
            o_z=ord(z)-97
            i_z=bisect.bisect_left(abc[o_z],x)
            abc[o_z]=abc[o_z][:i_z]+abc[o_z][i_z+1:]
            bisect.insort_left(abc[o_y],x)
            S[x]=y
    else:
        x=int(x)
        x-=1
        y=int(y)
        y-=1
        ans=0
        for i in range(26):
            if bisect.bisect_left(abc[i],x)!=bisect.bisect_right(abc[i],y):
                ans+=1
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
q = int(input())
for _ in range(q):
    t, i, c = map(str, input().split())
    if t == '1':
        s = s[:int(i)-1] + c + s[int(i):]
    else:
        print(len(set(s[int(i)-1:int(c)])))

Submission status: Time Limit Exceeded

Solution code:
class SegmentTree:
    def __init__(self, lst, op, e):
        self.n = len(lst)
        self.N = 1 << ((self.n - 1).bit_length())
        self.op = op # operation
        self.e = e # identity element
        self.v = self._build(lst)
        
    def _build(self, lst):
        # construction of a tree
        # total 2 * self.N - 1 elements
        tree = [self.e] * (self.N - 1) + lst + [self.e] * (self.N - self.n)
        for i in range(self.N - 2, -1, -1):
            tree[i] = self.op(tree[2*i + 1], tree[2*i + 2])
        return tree
    
    def __getitem__(self, i):
        return self.v[i + self.N - 1]
    
    # update a_i to be x 
    def update(self, i, x):
        i += self.N - 1
        self.v[i] = x
        while i > 0:
            i -= 1; i >>= 1
            self.v[i] = self.op(self.v[2*i + 1], self.v[2*i + 2])
    
    # returns answer for the query interval [l, r)
    def query(self, l, r):
        left = l + self.N - 1
        right = r + self.N - 1
        L = self.e; R = self.e
        while left < right:
            if left & 1 == 0: # self.v[left] is the right child
                L = self.op(L, self.v[left])
                left += 1
            if right & 1 == 0: # self.v[right-1] is the left child
                right -= 1
                R = self.op(self.v[right], R)
            left -= 1; left >>= 1
            right >>= 1
        return self.op(L, R)

N = int(input())
S = input()
sg = SegmentTree([set(s) for s in S], lambda x, y: x | y, set([]))
Q = int(input())
for _ in range(Q):
    t, a, b = input().split()
    t = int(t); a = int(a)
    if t == 1:
        sg.update(a-1, {b})
    else:
        b = int(b)
        print(len(sg.query(a-1, b)))

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**9)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

import collections

n = ni()
s = ns()
s = list(s)
q = ni()

for i in range(q):
	a,b,c = nn()
	if int(a) == 1:
		s[int(b)-1] = c
	else:
		ss = collections.Counter(s[int(b)-1:int(c)])
		print(len(ss.keys()))

Submission status: Time Limit Exceeded

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

def type1(S,i,w):
    S[i-1] = w
    return S
    
def type2(S,l,r):
    print(len(set(S[l-1:r])))
    

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

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

def main():
    N = int(input())
    S = input()
    Q = int(input())
    query = [list(input().split()) for _ in range(Q)]
    # print(query)
    S_list = list(S)
    for q in query:
        # print(q)
        if q[0] == '1':
            S_list[int(q[1])-1] = q[2]
        else:
            # print(q)
            sub_list = S_list[int(q[1])-1:int(q[2])]
            # print(sub_list)
            print(len(set(sub_list)))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

N = int(input())
S = input()
Q = int(input())
Queries = [input().split() for _ in range(Q)]

chars = 'abcdefghijklmnopqrstuvwxyz'
flags = {c: 0 for c in chars}
#print(flags)
for i, s in enumerate(S):
  flags[s] |= 1 << i
#print(flags)
SS = [s for s in S]
#print(SS)

def change_char(i, c):
  bc = SS[i]
  SS[i] = c
  flags[bc] ^= 1 << i
  flags[c] |= 1 << i
  #print(SS)

def count_chars(l, r):
  f = 2**(r-l+1)-1
  #print(f, flags)
  #print([c for c,v in flags.items() if (v >> l) & f])
  return len([flag for flag in flags.values() if (flag >> l) & f])

for t, x, y in Queries:
  if t == '1':
    change_char(int(x)-1, y)
  else:
    print(count_chars(int(x)-1, int(y)-1))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
q=int(input())
que=[]
for i in range(q):
    a=input().split()
    que.append((int(a[0]),int(a[1]),a[2]))

def cul(le):
    cu=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    for k in le:
        if k=="a":
            cu[0]+=1
        elif k=="b":
            cu[1]+=1
        elif k=="c":
            cu[2]+=1
        elif k=="d":
            cu[3]+=1
        elif k=="e":
            cu[4]+=1
        elif k=="f":
            cu[5]+=1
        elif k=="g":
            cu[6]+=1
        elif k=="h":
            cu[7]+=1
        elif k=="i":
            cu[8]+=1
        elif k=="j":
            cu[9]+=1
        elif k=="k":
            cu[10]+=1
        elif k=="l":
            cu[11]+=1
        elif k=="m":
            cu[12]+=1
        elif k=="n":
            cu[13]+=1
        elif k=="o":
            cu[14]+=1
        elif k=="p":
            cu[15]+=1
        elif k=="q":
            cu[16]+=1
        elif k=="r":
            cu[17]+=1
        elif k=="s":
            cu[18]+=1
        elif k=="t":
            cu[19]+=1
        elif k=="u":
            cu[20]+=1
        elif k=="v":
            cu[21]+=1
        elif k=="w":
            cu[22]+=1
        elif k=="x":
            cu[23]+=1
        elif k=="y":
            cu[24]+=1
        elif k=="z":
            cu[25]+=1
    tt=0
    for jj in cu:
        if jj==0:
            tt+=1
    return 26-tt
for j in que:
    if j[0]==1:
        s[j[1]-1]=j[2]
    if j[0]==2:
        b=int(j[1])
        c=int(j[2])
        l=[]
        for m in range(b-1,c):
            l.append(s[m])
        ans=cul(l)
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
S = str(input())
Q = int(input())
alp = 'abcdefghijklmnopqrstuvwxyz'
for i in range(Q):
  t,m,p = map(str,input().split())
  t = int(t)
  m = int(m)
  C = [0] * 26
  ans = 0
  if t == 2:
    p = int(p)
    St = S[m-1:p]
    for i in St:
      if C[alp.index(i)] == 0:
        ans += 1
        C[alp.index(i)] = 1
    print(ans)
  else:
    St = list(S)
    St[m-1] = p
    S = St

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


class BIT:

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

    def sum_all(self, i):
        s = numpy.zeros(L, dtype=numpy.int32)
        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:
n = int(input())
S = list(input())
q = int(input())

for i in range(q):
    a, b, c = input().split()
    b = int(b) - 1
    if a == '1':
        S[b] = c
    else:
        c = int(c)
        ans = 0
        for _ in 'abcdefghijklmnopqrstuvwxyz':
            if _ in S[b:c]:
                ans += 1
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import bisect

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

dictList=[defaultdict(int) for _ in range(n+1)]

for i in range(n):
    dictList[i+1]=dictList[i].copy()
    dictList[i+1][s[i]]+=1

ans_list=[]
change={}

#print(dictList)

for g in range(q):
    a,b,c=map(str,input().split())
    a=int(a)
    b=int(b)
    if a==1:
        change[b]=[s[b-1],c] #[before,after]
    else:
        ans=0
        c=int(c)
        Judge = defaultdict(int)
        lDict = dictList[b-1].copy()
        rDict = dictList[c].copy()
        #print(lDict,rDict)
        for i,m in lDict.items():
            Judge[i]-=m
        for i,m in rDict.items():
            Judge[i]+=m
        for i,m in change.items():
            if b<=i and i<=c:
                Judge[m[0]]-=1
                Judge[m[1]]+=1
        #print(Judge)
        for i,m in Judge.items():
            if m>0:
                ans+=1
        ans_list.append(ans)

#print(change)
for i in range(len(ans_list)):
    print(ans_list[i])

Submission status: Time Limit Exceeded

Solution code:
import collections

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

answer = []


for i in range(Q):
    
    q, i, x = input().split()
    
    if (q == '1'):
        tmp = int(i) - 1
        S[tmp] = x
        
    
    else:
        l = int(i)
        r = int(x)
        string = S[l - 1 : r]
    
        CTR = collections.Counter(string).most_common()
        ans = len(CTR)

        answer.append(ans)


for i in answer:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input().strip()
C = {chr(i):[0 for _ in range(N)] for i in range(97,123)}
for j in range(N):
    a = S[j]
    C[a][j] = 1
Q = int(input())
for _ in range(Q):
    x,y,z = input().split()
    if int(x)==1:
        y = int(y)
        for i in range(97,123):
            if C[chr(i)][y-1]==1:
                C[chr(i)][y-1] = 0
        C[z][y-1] = 1
    elif int(x)==2:
        y = int(y)
        z = int(z)
        cnt = 0
        for i in range(97,123):
            if 1 in C[chr(i)][y-1:z]:
                cnt += 1
        print(cnt)

Submission status: Time Limit Exceeded

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

# int = int(input())
# s = input().split()
# i = list(map(int, input().split()))
# s = [input() for i in range(3)]

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

for i in range(q):
    x, y, z = input().split()
    if x == "1":
        s[int(y)-1] = z
    else:
        c = collections.Counter(s[int(y)-1:int(z)])
        print(len(c))

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict


class SegTree:
    def __init__(self, n, s):
        """
        :param n: 要素数
        :param INF: 初期値（入りうる要素より十分に大きな数）
        """
        n2 = 1 << (n - 1).bit_length()
        self.offset = n2
        self.tree = [None] * n2 + [{} for _ in range(n2)]

        for i, c in enumerate(s):
            x = ord(c) - 97
            i += self.offset
            self.tree[i][x] = 1
        for i in range(self.offset - 1, 0, -1):
            sti = self.tree[i] = defaultdict(lambda: 0, self.tree[i << 1])
            stj = self.tree[(i << 1) + 1]
            for x in stj:
                sti[x] += stj[x]
        # print(*(dict(t) for t in self.tree), sep='\n')

    def update(self, i, x):
        """
        i番目の値をxに更新
        :param i: index(0-indexed)
        :param x: update value
        """
        i += self.offset
        if x in self.tree[i]:
            return
        y, _ = self.tree[i].popitem()
        self.tree[i][x] = 1
        i >>= 1
        while i > 0:
            sti = self.tree[i]
            sti[x] += 1
            if sti[y] == 1:
                del sti[y]
            else:
                sti[y] -= 1
            i >>= 1

    def get_types(self, a, b):
        """
        [a, b)の最小値を得る
        :param a: index(0-indexed)
        :param b: index(0-indexed)
        """
        result = set()

        l = a + self.offset
        r = b + self.offset
        while l < r:
            if r & 1:
                result.update(self.tree[r - 1].keys())
            if l & 1:
                result.update(self.tree[l].keys())
                l += 1
            l >>= 1
            r >>= 1

        return len(result)


n = int(input())
s = input()
st = SegTree(n, s)
q = int(input())
ans = []
for line in sys.stdin:
    i, j, k = line.rstrip().split()
    if i == '1':
        j = int(j) - 1
        k = ord(k) - 97
        st.update(j, k)
    else:
        l = int(j) - 1
        r = int(k)
        ans.append(st.get_types(l, r))
print('\n'.join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
Q = int(input())
for _ in range(Q):
  t, i, c = map(str, input().split())
  t = int(t)
  i = int(i)
  if t==1 and S[i-1]!=c:
    S = S[:i-1] + c + S[i:]
  elif t==2:
    c = int(c)
    print(len(set(S[i-1:c])))

Submission status: Time Limit Exceeded

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())
  a, b, c= x.split(" ")
  #print(a, b, c)
  if int(a) == 1:
    if S[int(b) - 1] != c:
      S[int(b) - 1] = c
      #print(S)
  if int(a) == 2:
    l, r = int(b), int(c)
    S1 = S[l - 1 : r]
    #print(S1, "yes")
    c = Counter(S1)
    #print(c,S1)
    print(len(c))
    
#print(len(S))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = list(input())
Q = int(input())
S = np.array(S)
for _ in range(Q):
    a, b, c = input().split()
    if a == '1':
        S[int(b)-1] = c
    elif a == '2':
        print(len(set(S[int(b)-1:int(c)])))

Submission status: Time Limit Exceeded

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

Query = list()
for i in range(Q):
    Query_tmp = list(map(str, input().split()))
    Query.append(Query_tmp)

S_list = list()
for i in range(N):
    S_list.append(S[i])


for i in range(Q):
    if Query[i][0] == "1":
        S_list[int(Query[i][1]) - 1] = Query[i][2]

    if Query[i][0] == "2":
        left = int(Query[i][1])
        right = int(Query[i][2])
        tmp = S_list[left - 1:right]
        hind = [0]*26

        for j in range(right - left + 1):
            hind[ord(tmp[j]) - 97] += 1
        
        ans = 0
        for j in range(26):
            if hind[j] != 0:
                ans += 1

        
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
def case1(li, x, c):
    if li[x-1] != c:
        li[x-1] = c
    return li


def case2(li, start, end):
    s = set(li[start-1:end])
    sys.stdout.write(str(len(s)) + '\n')


i = int(input())  # skip this
char_list = [x for x in input()]
Q = int(input())

for i in range(Q):
    tokens = [int(x) if x.isdigit() else x for x in sys.stdin.readline().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])

Submission status: Time Limit Exceeded

Solution code:

def main():
    n = int(input())
    s = [0] + list(input())
    q = int(input())
    for i in range(q):
        #print(i,"回目",s)
        q,a,b = input().split()
        if q == "1":
            query1(int(a),b,s)
            #print(i,"回目",s)
        elif q == "2":
            query2(int(a),int(b),s)

def query1(i,c,s):
    s[i] = c

def query2(l,r,s):
    print(len(set(s[l:r+1])))



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

class segtree:

    def __init__(self, n,operator,identity):
        nb = bin(n)[2:]
        bc = sum([int(digit) for digit in nb])
        if bc == 1:
            self.num_end_leaves = 2**(len(nb)-1)
        else:
            self.num_end_leaves = 2**(len(nb))

        self.array = [identity for i in range(self.num_end_leaves * 2)]
        self.identity = identity
        self.operator =operator

    def update(self,x,val):
        actual_x = x+self.num_end_leaves
        self.array[actual_x] = val
        while actual_x > 0 :
            actual_x = actual_x//2
            self.array[actual_x] = self.operator(self.array[actual_x*2],self.array[actual_x*2+1])

    def get(self,q_left,q_right,arr_ind=1,leaf_left=0,depth=0):
        width_of_floor = self.num_end_leaves//(2**depth)
        leaf_right = leaf_left+width_of_floor-1

        if leaf_left > q_right or leaf_right < q_left:
            return  self.identity

        elif leaf_left >= q_left and leaf_right <= q_right:
            return self.array[arr_ind]

        else:
            val_l = self.get(q_left,q_right,2*arr_ind,leaf_left,depth+1)
            val_r = self.get(q_left,q_right,2*arr_ind+1,leaf_left+width_of_floor//2,depth+1)
            return self.operator(val_l,val_r)

identity = np.array([False for i in range(26)])
def num_kinds(a,b):
    return np.logical_or(a,b)

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

tree = segtree(n,num_kinds,identity)

for i in range(n):
    temp = np.array([False for i in range(26)])
    temp[ord(s[i]) - 97] = True
    tree.update(i, temp)

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

    if ty == "1":
        i = int(i)
        temp = np.array([False for i in range(26)])
        temp[ord(c)-97]=True
        tree.update(i-1,temp)
    else:
        i = int(i)
        c = int(c)
        i-=1
        c-=1
        print(sum(tree.get(i,c)))

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter
input = sys.stdin.readline

def main():
    n=int(input())
    s=list(input())
    q=int(input())
    for i in range(q):
        a,b,c=map(str,input().split())
        if a=='1':
            s[int(b)-1]=c
        else:
            print(len(Counter(s[int(b)-1:int(c)])))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
S = input()
Q = int(input())
query = []
for i in range(Q):
    query.append(input().split())

for i in range(Q):
    a, b, c = query[i]
    if a == '1':
        b = int(b)-1
        if not S[b] == c: S = S[:b]+c+S[b+1:]
    else: print(len(Counter(S[int(b)-1:int(c)]).keys()))

Submission status: Time Limit Exceeded

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

for q in Query:
    if q[0] == '1':
        idx = int(q[1]) - 1
        S[idx] = q[2]
    elif q[0] == '2':
        left = int(q[1]) - 1
        right = int(q[2])
        s_slice = S[left:right]
        print(len(list(set(s_slice))))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import collections

ket = int(input())
moj = input()
tas = int(input())
x = 0
y = 0
i = 0
j = 0

for i in range(tas):
    que = list(input().split())
    if que[0] == "1":
        x = int(que[1])
        moj = moj[:x-1] + str(que[2]) + moj[x:]
    elif que[0] == "2":
        x = int(que[1])
        y = int(que[2])
        l = list(moj[(x-1):y])
        c = collections.Counter(l)
        i  =c.most_common()
        print(len(i))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def main():
    N = int(input())
    S = list(input())
    Q = int(input())

    for q in range(Q):
        query = input().split()
        for idx in range(3):
            try:
                query[idx]=int(query[idx])
            except:
                pass

        if query[0] == 1:
            S[query[1]-1] = query[2]
        else:
            l = S[query[1]-1: query[2]]
            print(len(Counter(l)))
            """
            l.sort()
            cnt = 1
            for i in range(1,len(l)):
                if l[i-1]!=l[i]: cnt += 1
            """


    return

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect
import sys

input = sys.stdin.readline
num2alpha = lambda c: chr(c + 97)
alpha2num = lambda c: ord(c) - ord("a")


class SegTreeMax:
    def __init__(self, l):
        self.N = 1
        while self.N < len(l):
            self.N *= 2
        self.L = [False for _ in range(self.N * 2 - 1)]

    def update(self, k, v):
        k += self.N - 1
        self.L[k] = v
        while k > 0:
            k = (k - 1) // 2
            self.L[k] = self.L[k * 2 + 1] or self.L[k * 2 + 2]

    def query(self, a, b):
        return self.inquiry(0, self.N, 0, a, b)

    def inquiry(self, l, r, k, a, b):
        if r <= a or l >= b:
            return 0
        if a <= l and b >= r:
            return self.L[k]
        if self.inquiry(l, (l + r) // 2, k * 2 + 1, a, b):
            return True
        if self.inquiry((l + r) // 2, r, k * 2 + 2, a, b):
            return True
        return False


def main():
    N = int(input())
    Stri = input()
    L = []
    for c in Stri:
        L.append(c)
    Q = int(input())
    l0 = [0 for _ in range(N)]
    S = [SegTreeMax(l0) for _ in range(26)]
    for i in range(N):
        S[alpha2num(L[i])].update(i, True)

    for _ in range(Q):
        a, b, c = input().split()
        if a == "1":
            i = int(b) - 1
            S[alpha2num(L[i])].update(i, False)
            L[i] = c
            S[alpha2num(c)].update(i, True)

        else:
            l = int(b) - 1
            r = int(c)
            res = 0
            for i in range(26):
                if S[i].query(l, r):
                    res += 1
            print(res)


main()

Submission status: Time Limit Exceeded

Solution code:
import bisect

N=int(input())
S=input()
Q=int(input())
query=[list(map(str,input().split())) for _ in range(Q)]
#print(query)
for i in range(Q):
    if query[i][0]=="1":
        s1=S[:int(query[i][1])-1]
        s2=S[int(query[i][1]):]
        S="".join((s1,query[i][2],s2))
        #print(S)
    if query[i][0]=="2":
        #print(S)
        print(len(set(S[int(query[i][1])-1:int(query[i][2])])))