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 C++ (GCC 9.2.1) (judge verdict: Accepted)


// Problem : E - Simple String Queries
// Contest : AtCoder - AtCoder Beginner Contest 157
// URL : https://atcoder.jp/contests/abc157/tasks/abc157_e
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define db double
#define mod 1000000007
#define pb push_back
#define pp pair<int,int>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repe(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(int i=b;i>=a;i--)
#define maxn 500005
#define ss second
#define ff first
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
int inv(int a, int b){
 return 1<a ? b - inv(b%a,a)*b/a : 1;
}
int st[maxn][26];
int n,q;
string str;
int ind;
void update(int j,int val,int ind)
{
	int i=j;
	while(i<=n)
	{
		if(ind >=0)
		st[i][ind]--;
		st[i][val]++;
		i+=(i&-i);
	}
}
int ans[26];
void query(int ind,int flag)
{
	int i=ind;
	while(i>0)
	{
		for(int j=0;j<26;j++)
		{
			if(flag)
				ans[j]+=st[i][j];
			else ans[j]-=st[i][j];
		}
		i-=(i&-i);
	}
}
void solve()
{
	cin >> n;
	cin >> str;
	cin >> q;
//	build(1,0,n-1);
	for(int i=0;i<n;i++)
	update(i+1,str[i]-'a',-1);
	while(q--)
	{
		int t;
		cin >> t;
		if(t==1)
		{
			int i;
			char x;
			cin >> i >> x;
			update(i,x-'a',str[i-1]-'a');
			str[i-1]=x;
		}
		else{
			int i,j;
			cin >> i >> j;
			//cout << i <<" "<<j<<"\n";
			query(j,1);
			query(i-1,0);
			int cnt=0;
			rep(k,0,26){
				if(ans[k]>0)
				{
					//cout << (char)(k+'a') <<" ";
					cnt++;
					}
				ans[k]=0;
			}
			cout << cnt <<"\n";
		}
	}
}
signed main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t=1;
    //cin >> t;
    while(t--)
    solve();
    return 0;
}