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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	const double eps=1e-11;
	static void Main(){
		Sc sc=new Sc();
		int n=sc.I;
		var t=" "+sc.S;
		var sd=new Sd(t);
		int q=sc.I;
		StringBuilder sb=new StringBuilder();
		for(int i = 0;i<q;i++) {
			var e=sc.Sa;
			if(e[0]=="1"){
				sd.Add(int.Parse(e[1]),1<<(e[2][0]-'a'));
			}
			else{
				sb.Append(Pc((int)sd.Get(int.Parse(e[1]),int.Parse(e[2])))+"\n");
			}
		}
		Console.Write(sb);
	}
	static int Pc(int n){
		n-=(n>>1)&0x55555555;
		n=(n&0x33333333)+((n>>2)&0x33333333);
		return ((n+(n>>4)&0xF0F0F0F)*0x1010101)>>24;
    }
}
public class Sd{
	public long[] h1,h2;
	public int n,m;
	public Sd(int n){
		this.n=n;
		m=(int)Sqrt(n)+1;
		h1=new long[n];
		h2=new long[m];
	}
	public Sd(string t){
		n=t.Length;
		m=(int)Sqrt(n)+1;
		h1=new long[n];
		h2=new long[m];
		for(int i = 1,j;i<n;i++) {
			h1[i]=1<<(t[i]-'a');
			h2[j=i/m]=Fu(h2[j],h1[i]);
		}
	}
	private long Fu(long a,long b){
		return a|b;
	}
	public void Add(int a,int k){
		int g=a/m,ml=g*m,mp=Min(ml+m,n);
		h2[g]=h1[a]=k;
		for(int i = g*m;i<mp;i++) {
			h2[g]=Fu(h2[g],h1[i]);
		}
	}
	public long Get(int l,int r){
		int ml=(l+m-1)/m,mr=(r-m+1)/m,mp=Min(ml*m,r+1);
		long p=h1[l];
		for(int i = l;i<mp;i++) {
			p=Fu(p,h1[i]);
		}
		for(int i = ml;i<=mr;i++) {
			p=Fu(p,h2[i]);
		}
		for(int i = Max(ml*m,mr*m+m);i<=r;i++) {
			p=Fu(p,h1[i]);
		}
		return p;
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);}
	public int[] Ia3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);}
	public long[] La3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}