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.

Solution in Python (3.4.3) (judge verdict: Wrong Answer)

class SegmentTree:
    def __init__(self, a):      
        self.n = len(a)
        self.N = 2 ** (self.n-1).bit_length()
        l = []
        for _a in a:
            l.append({'dic':{_a:1}, 'set':set(_a)})
        self.data = [{'dic':{}, 'set':set()} for i in range(self.N-1)] + l + [{'dic':{}, 'set':set()} for i in range(self.N-self.n)]
        for i in range(2*self.N-2, 0, -2):
            self.data[(i-1)//2] = self.init_func(self.data[i], self.data[i-1])
    
    def init_func(self, data1, data2):
        s = data1['set'] | data2['set']
        d = data1['dic']
        for k in data2['dic']:
            if k in d:
                d[k] += data2['dic'][k]
            else:
                d[k] = data2['dic'][k]
        return {'dic':d, 'set':s}

    def __len__(self):
        return self.n
    
    def func(self, idx, x1, x2):
        self.data[idx]['dic'][x1] -= 1
        if self.data[idx]['dic'][x1] == 0:
            self.data[idx]['set'].discard(x1)
        if x2 in self.data[idx]['dic']:
            self.data[idx]['dic'][x2] += 1
            if self.data[idx]['dic'][x2] == 1:
                self.data[idx]['set'].add(x2)
        else:
            self.data[idx]['dic'][x2] = 1
            self.data[idx]['set'].add(x2)        

    def update(self, i, x):
        idx = self.N - 1 + i
        pre = list(self.data[idx]['dic'].keys())[0]
        self.data[idx] = {'dic':{x:1}, 'set':set(x)}
        while idx:
            idx = (idx-1) // 2
            self.func(idx, pre, x)
    
    def query(self, i, j):
        # [i, j)
        if i == j:
            return len(self.data[self.N - 1 + i]['set'])
        else:
            idx1 = self.N - 1 + i
            idx2 = self.N - 2 + j # 閉区間にする
            result = set()
            while idx1 + 1 < idx2:
                if idx1&1 == 0: # idx1が偶数
                    result = result | self.data[idx1]['set']
                if idx2&1 == 1: # idx2が奇数
                    result = result | self.data[idx2]['set']
                    idx2 -= 1
                
                idx1 //= 2
                idx2 = (idx2 - 1)//2
            
            if idx1 == idx2:
                result = result | self.data[idx1]['set']
            else: # idx1 + 1 == idx2
                result = result | self.data[idx1]['set'] | self.data[idx2]['set']
            
            return len(result)

N = int(input())
S = list(input())
st = SegmentTree(S)

Q = int(input())
for i in range(Q):
  t, a, b = input().split()
  if t == '1':
    a = int(a)
    if S[a-1] == b:
      continue
    st.update(a-1, b)
    S[a-1] = b
  else:
    a, b = int(a), int(b)
    print(st.query(a-1, b-1))