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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace AtCorder
{
	public class Program
	{
		static void Main(string[] args)
		{
			var cin = new Scanner2();
			int n = cin.Int();
			string s = cin.Next();
			int q = cin.Int();

			var segs = new SegmentTree<int>(n, 0, (x, y) => x | y);
			for (int i = 0; i < n; i++) {
				var c = s[i] - 'a';
				segs.Update(i, 1 << c);
			}

			var sb = new StringBuilder();
			for (int i = 0; i < q; i++) {
				int qq = cin.Int();
				if (qq == 1) {
					int ii = cin.Int() - 1;
					int c = cin.Next()[0] - 'a';
					segs.Update(ii, 1 << c);
				} else {
					int l = cin.Int() - 1;
					int r = cin.Int();
					int cnt = 0;
					int cs = segs.Query(l, r);
					for (int j = 0; j < 26; j++) {
						if ((cs & (1 << j)) != 0) {
							cnt++;
						}
					}

					sb.AppendLine(cnt.ToString());
				}
			}

			Console.WriteLine(sb.ToString());
		}
	}

	public class SegmentTree<T>
	{
		private readonly int n_;
		private readonly T unit_;
		private readonly T[] tree_;
		private readonly Func<T, T, T> merge_;

		public int Count { get; }
		public T Top => tree_[1];

		public SegmentTree(int count, T unit, Func<T, T, T> merge)
		{
			merge_ = merge;
			unit_ = unit;

			Count = count;
			n_ = 1;
			while (n_ < count) {
				n_ <<= 1;
			}

			tree_ = new T[n_ << 1];
			for (int i = 0; i < tree_.Length; i++) {
				tree_[i] = unit;
			}
		}

		public SegmentTree(T[] src, T max, Func<T, T, T> merge)
			: this(src.Length, max, merge)
		{
			for (int i = 0; i < n_; ++i) {
				tree_[i + n_] = src[i];
			}

			for (int i = n_ - 1; i > 0; --i) {
				tree_[i] = merge_(tree_[i << 1], tree_[(i << 1) + 1]);
			}
		}

		public T this[int index]
		{
			get { return tree_[index + n_]; }
			set { Update(index, value); }
		}

		public void Update(int index, T val)
		{
			if (index >= Count) {
				return;
			}

			index += n_;
			tree_[index] = val;
			index >>= 1;
			while (index != 0) {
				tree_[index] = merge_(tree_[index << 1], tree_[(index << 1) + 1]);
				index >>= 1;
			}
		}

		public T Query(int left, int right)
		{
			if (left > right || right < 0 || left >= Count) {
				return unit_;
			}

			int l = left + n_;
			int r = right + n_;
			T valL = unit_;
			T valR = unit_;
			while (l < r) {
				if ((l & 1) != 0) {
					valL = merge_(valL, tree_[l]);
					++l;
				}
				if ((r & 1) != 0) {
					--r;
					valR = merge_(tree_[r], valR);
				}

				l >>= 1;
				r >>= 1;
			}

			return merge_(valL, valR);
		}
	}

	public class Scanner2
	{
		private readonly char[] delimiter_ = new char[] { ' ' };
		private readonly string filePath_;
		private readonly Func<string> reader_;
		private string[] buf_;
		private int index_;

		public Scanner2(string file = "")
		{
			if (string.IsNullOrWhiteSpace(file)) {
				reader_ = Console.ReadLine;
			} else {
				filePath_ = file;
				var fs = new StreamReader(file);
				reader_ = fs.ReadLine;
			}
			buf_ = new string[0];
			index_ = 0;
		}

		public string Next()
		{
			if (index_ < buf_.Length) {
				return buf_[index_++];
			}

			string st = reader_();
			while (st == "") {
				st = reader_();
			}

			buf_ = st.Split(delimiter_, StringSplitOptions.RemoveEmptyEntries);
			if (buf_.Length == 0) {
				return Next();
			}

			index_ = 0;
			return buf_[index_++];
		}

		public int Int() => int.Parse(Next());
		public long Long() => long.Parse(Next());
		public double Double() => double.Parse(Next());

		public int[] ArrayInt(int N, int add = 0)
		{
			int[] Array = new int[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Int() + add;
			}
			return Array;
		}

		public long[] ArrayLong(int N, long add = 0)
		{
			long[] Array = new long[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Long() + add;
			}
			return Array;
		}

		public double[] ArrayDouble(int N, double add = 0)
		{
			double[] Array = new double[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Double() + add;
			}
			return Array;
		}

		public void Save(string text)
		{
			if (string.IsNullOrWhiteSpace(filePath_)) {
				return;
			}

			File.WriteAllText(filePath_ + "_output.txt", text);
		}
	}
}