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: Wrong Answer)

using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using Debug = System.Diagnostics.Trace;
using SB = System.Text.StringBuilder;
using static System.Math;
using Number = System.Int32;

namespace Program {
	public class Solver {
		Random rnd = new Random();
		public void Solve() {
			var n = ri;
			var s = rs.ToCharArray();
			var q = ri;
			var set = Enumerate(26, x => new Set<int>());
			for (int i = 0; i < n; i++)
				set[s[i] - 'a'].Add(i);
			for (int i = 0; i < q; i++) {
				if (ri == 1) {
					var id = ri - 1;
					var c = rs[0];
					set[s[id] - 'a'].Remove(id);
					set[c - 'a'].Add(id);
				}
				else {
					var l = ri - 1;
					var r = ri - 1;
					var cnt = 0;
					for (int c = 0; c < 26; c++)
						if (set[c].UpperBound(r) - set[c].LowerBound(l) != 0) cnt++;
					Console.WriteLine(cnt);
				}
			}

		}

		const long INF = 1L << 60;
		static int[] dx = { -1, 0, 1, 0 };
		static int[] dy = { 0, 1, 0, -1 };
		int ri { get { return sc.Integer(); } }
		long rl { get { return sc.Long(); } }
		double rd { get { return sc.Double(); } }
		string rs { get { return sc.Scan(); } }
		public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput());

		static T[] Enumerate<T>(int n, Func<int, T> f) {
			var a = new T[n];
			for (int i = 0; i < a.Length; ++i) a[i] = f(i);
			return a;
		}
		static public void Swap<T>(ref T a, ref T b) { var tmp = a; a = b; b = tmp; }
	}
}

#region main
static class Ex {
	static public string AsString(this IEnumerable<char> ie) { return new string(ie.ToArray()); }
	static public string AsJoinedString<T>(this IEnumerable<T> ie, string st = " ") {
		return string.Join(st, ie);
	}
	static public void Main() {
		Console.SetOut(new Program.IO.Printer(Console.OpenStandardOutput()) { AutoFlush = true });
		var solver = new Program.Solver();
		//* 
		var t = new System.Threading.Thread(solver.Solve, 50000000);
		t.Start();
		t.Join();
		//*/
		// solver.Solve();
		Console.Out.Flush();
	}
}
#endregion
#region Ex
namespace Program.IO {
	using System.IO;
	using System.Text;
	using System.Globalization;

	public class Printer : StreamWriter {
		public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
		public Printer(Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
	}

	public class StreamScanner {
		public StreamScanner(Stream stream) { str = stream; }

		public readonly Stream str;
		private readonly byte[] buf = new byte[1024];
		private int len, ptr;
		public bool isEof = false;
		public bool IsEndOfStream { get { return isEof; } }

		private byte read() {
			if (isEof) return 0;
			if (ptr >= len) {
				ptr = 0;
				if ((len = str.Read(buf, 0, 1024)) <= 0) {
					isEof = true;
					return 0;
				}
			}
			return buf[ptr++];
		}

		public char Char() {
			byte b = 0;
			do b = read(); while ((b < 33 || 126 < b) && !isEof);
			return (char)b;
		}
		public string Scan() {
			var sb = new StringBuilder();
			for (var b = Char(); b >= 33 && b <= 126; b = (char)read()) sb.Append(b);
			return sb.ToString();
		}
		public string ScanLine() {
			var sb = new StringBuilder();
			for (var b = Char(); b != '\n' && b != 0; b = (char)read()) if (b != '\r') sb.Append(b);
			return sb.ToString();
		}
		public long Long() { return isEof ? long.MinValue : long.Parse(Scan()); }
		public int Integer() { return isEof ? int.MinValue : int.Parse(Scan()); }
		public double Double() { return isEof ? double.NaN : double.Parse(Scan(), CultureInfo.InvariantCulture); }
	}
}

#endregion

#region Set
/// <summary>
/// 要素の追加、削除、検索、取得が可能な集合を表します．
/// </summary>
/// <typeparam name="T">優先度付きキュー内の要素の型を指定します．</typeparam>
/// <remarks>内部的にはAVL木によって実装されています．</remarks>
public class Set<T> {
	Node root;
	readonly IComparer<T> comparer;
	readonly Node nil;
	/// <summary>
	/// 多重集合かどうかを表します．
	/// </summary>
	public bool IsMultiSet { get; set; }
	public Set(IComparer<T> comparer) {
		nil = new Node(default(T));
		root = nil;
		this.comparer = comparer;
	}
	public Set(Comparison<T> comaprison) : this(Comparer<T>.Create(comaprison)) { }
	public Set() : this(Comparer<T>.Default) { }
	/// <summary>
	/// 要素をコレクションに追加します．
	/// </summary>
	/// <remarks>この操作は計算量 O(log N) で実行されます．</remarks>
	public bool Add(T v) {
		return insert(ref root, v);
	}
	/// <summary>
	/// v が存在するならコレクションから削除します．
	/// </summary>
	/// <remarks>この操作は計算量 O(log N) で実行されます．</remarks>
	public bool Remove(T v) {
		return remove(ref root, v);
	}
	/// <summary>
	/// 0-indexed で index 番目の要素をコレクションから取得します．．
	/// </summary>
	/// <remarks>この操作は計算量 O(log N) で実行されます．</remarks>
	public T this[int index] { get { return find(root, index); } }
	public int Count { get { return root.Count; } }
	public void RemoveAt(int k) {
		if (k < 0 || k >= root.Count) throw new ArgumentOutOfRangeException();
		removeAt(ref root, k);
	}
	/// <summary>
	/// このコレクションに含まれる要素を昇順に並べて返します．
	/// </summary>
	/// <remarks>この操作は計算量 O(N) で実行されます．</remarks>
	public T[] Items {
		get {
			var ret = new T[root.Count];
			var k = 0;
			walk(root, ret, ref k);
			return ret;
		}
	}
	void walk(Node t, T[] a, ref int k) {
		if (t.Count == 0) return;
		walk(t.lst, a, ref k);
		a[k++] = t.Key;
		walk(t.rst, a, ref k);
	}

	bool insert(ref Node t, T key) {
		if (t.Count == 0) { t = new Node(key); t.lst = t.rst = nil; t.Update(); return true; }
		var cmp = comparer.Compare(t.Key, key);
		bool res;
		if (cmp > 0)
			res = insert(ref t.lst, key);
		else if (cmp == 0) {
			if (IsMultiSet) res = insert(ref t.lst, key);
			else return false;
		}
		else res = insert(ref t.rst, key);
		balance(ref t);
		return res;
	}
	bool remove(ref Node t, T key) {
		if (t.Count == 0) return false;
		var cmp = comparer.Compare(key, t.Key);
		bool ret;
		if (cmp < 0) ret = remove(ref t.lst, key);
		else if (cmp > 0) ret = remove(ref t.rst, key);
		else {
			ret = true;
			var k = t.lst.Count;
			if (k == 0) { t = t.rst; return true; }
			if (t.rst.Count == 0) { t = t.lst; return true; }


			t.Key = find(t.lst, k - 1);
			removeAt(ref t.lst, k - 1);
		}
		balance(ref t);
		return ret;
	}
	void removeAt(ref Node t, int k) {
		var cnt = t.lst.Count;
		if (cnt < k) removeAt(ref t.rst, k - cnt - 1);
		else if (cnt > k) removeAt(ref t.lst, k);
		else {
			if (cnt == 0) { t = t.rst; return; }
			if (t.rst.Count == 0) { t = t.lst; return; }

			t.Key = find(t.lst, k - 1);
			removeAt(ref t.lst, k - 1);
		}
		balance(ref t);
	}
	void balance(ref Node t) {
		var balance = t.lst.Height - t.rst.Height;
		if (balance == -2) {
			if (t.rst.lst.Height - t.rst.rst.Height > 0) { rotR(ref t.rst); }
			rotL(ref t);
		}
		else if (balance == 2) {
			if (t.lst.lst.Height - t.lst.rst.Height < 0) rotL(ref t.lst);
			rotR(ref t);
		}
		else t.Update();
	}

	T find(Node t, int k) {
		if (k < 0 || k > root.Count) throw new ArgumentOutOfRangeException();
		for (; ; )
		{
			if (k == t.lst.Count) return t.Key;
			else if (k < t.lst.Count) t = t.lst;
			else { k -= t.lst.Count + 1; t = t.rst; }
		}
	}
	/// <summary>
	/// コレクションに含まれる要素であって、 v 以上の最小の要素の番号を返します。
	/// </summary>
	/// <remarks>この操作は計算量 O(log N) で実行されます．</remarks>
	public int LowerBound(T v) {
		var k = 0;
		var t = root;
		for (; ; )
		{
			if (t.Count == 0) return k;
			if (comparer.Compare(v, t.Key) <= 0) t = t.lst;
			else { k += t.lst.Count + 1; t = t.rst; }
		}
	}
	/// <summary>
	/// コレクションに含まれる要素であって、 v より真に大きい、最小の要素の番号を返します。
	/// </summary>
	/// <remarks>この操作は計算量 O(log N) で実行されます．</remarks>
	public int UpperBound(T v) {
		var k = 0;
		var t = root;
		for (; ; )
		{
			if (t.Count == 0) return k;
			if (comparer.Compare(t.Key, v) <= 0) { k += t.lst.Count + 1; t = t.rst; }
			else t = t.lst;
		}
	}

	void rotR(ref Node t) {
		var l = t.lst;
		t.lst = l.rst;
		l.rst = t;
		t.Update();
		l.Update();
		t = l;
	}
	void rotL(ref Node t) {
		var r = t.rst;
		t.rst = r.lst;
		r.lst = t;
		t.Update();
		r.Update();
		t = r;
	}


	class Node {
		public Node(T key) {
			Key = key;
		}
		public int Count { get; private set; }
		public sbyte Height { get; private set; }
		public T Key { get; set; }
		public Node lst, rst;
		public void Update() {
			Count = 1 + lst.Count + rst.Count;
			Height = (sbyte)(1 + Math.Max(lst.Height, rst.Height));
		}
		public override string ToString() {
			return string.Format("Count = {0}, Key = {1}", Count, Key);
		}
	}
}
#endregion