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.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;

namespace AtCoder
{
	public class Program
	{
		static void Main()
		{
			var cin = new Scanner();
			int n = cin.Int();
			string s = cin.Next();
			int q = cin.Int();

			int[] cc = s.Select(x => x - 'a').ToArray();

			var sets = new OrderedSet<int>[26];
			for (int i = 0; i < 26; i++) {
				sets[i] = new OrderedSet<int>(inf: int.MaxValue);
			}

			for (int i = 0; i < n; i++) {
				int c = s[i] - 'a';
				sets[c].Add(i);
			}

			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';
					sets[cc[ii]].Remove(ii);
					sets[c].Add(ii);
					cc[ii] = c;
				} else {
					int l = cin.Int() - 1;
					int r = cin.Int() - 1;
					int cnt = 0;
					for (int j = 0; j < 26; j++) {
						int temp = sets[j].LowerBoundValue(l);
						if (temp >= l && temp <= r) {
							++cnt;
						}
					}

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

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

	public class OrderedSet<T> : IEnumerable<T>, IReadOnlyCollection<T>
		where T : IComparable
	{
		private readonly T inf_;
		private readonly bool isMulti_;
		private readonly RandomizedBinarySearchTree<T> bst_
			= new RandomizedBinarySearchTree<T>();
		private RandomizedBinarySearchTree<T>.Node root_;

		public T this[int index] { get { return ElementAt(index); } }

		public OrderedSet(bool isMulti = false, T inf = default(T))
		{
			inf_ = inf;
			isMulti_ = isMulti;
			bst_ = new RandomizedBinarySearchTree<T>(inf);
		}

		public int Count { get { return bst_.Count(root_); } }
		public int IndexOf(T v) => bst_.UpperBound(root_, v) - bst_.LowerBound(root_, v);
		public bool Contains(T value) => bst_.Contains(root_, value);
		public T Min { get { return Count == 0 ? inf_ : ElementAt(0); } }
		public T Max { get { return Count == 0 ? inf_ : ElementAt(Count - 1); } }

		public void Add(T value)
		{
			if (root_ == null) {
				root_ = new RandomizedBinarySearchTree<T>.Node(value);
			} else {
				if (bst_.Find(root_, value) != null && isMulti_ == false) {
					return;
				}

				root_ = bst_.Insert(root_, value);
			}
		}

		public void Remove(T value)
		{
			root_ = bst_.Remove(root_, value);
		}

		public void RemoveAt(int index)
		{
			root_ = bst_.RemoveAt(root_, index);
		}

		public void Clear()
		{
			root_ = null;
		}

		public T ElementAt(int index)
		{
			var node = bst_.FindByIndex(root_, index);
			if (node == null) {
				throw new IndexOutOfRangeException();
			}

			return node.Value;
		}

		public int LowerBound(T v) => bst_.LowerBound(root_, v);
		public int UpperBound(T v) => bst_.UpperBound(root_, v);
		public T LowerBoundValue(T v) => bst_.LowerBoundValue(root_, v);
		public T UpperBoundValue(T v) => bst_.UpperBoundValue(root_, v);

		public Tuple<int, int> EqualRange(T v)
		{
			if (Contains(v) == false) {
				return new Tuple<int, int>(-1, -1);
			}

			return new Tuple<int, int>(
				bst_.LowerBound(root_, v),
				bst_.UpperBound(root_, v) - 1);
		}

		public List<T> ToList() => new List<T>(bst_.Enumerate(root_));

		public IEnumerator<T> GetEnumerator()
		{
			foreach (var v in bst_.Enumerate(root_)) {
				yield return v;
			};
		}
		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
			=> GetEnumerator();
	}

	public class RandomizedBinarySearchTree<T>
		where T : IComparable
	{
		private static readonly Random random_ = new Random();
		private readonly T inf_;

		public RandomizedBinarySearchTree(T inf = default(T))
		{
			inf_ = inf;
		}

		public int Count(Node node) => node == null ? 0 : node.SubTreeSize;
		public bool Contains(Node node, T value) => Find(node, value) != null;

		public Node Insert(Node node, T value)
		{
			var index = LowerBound(node, value);
			return InsertAt(node, index, value);
		}

		private Node InsertAt(Node node, int k, T value)
		{
			var s = Split(node, k);
			return Merge(Merge(s.Left, new Node(value)), s.Right);
		}

		public Node Remove(Node node, T value)
		{
			if (Find(node, value) == null) {
				return node;
			}

			return RemoveAt(node, LowerBound(node, value));
		}

		public Node RemoveAt(Node node, int k)
		{
			var s1 = Split(node, k);
			var s2 = Split(s1.Right, 1);
			return Merge(s1.Left, s2.Right);
		}

		public Node Find(Node node, T target)
		{
			while (node != null) {
				var cmp = Compare(node.Value, target);
				if (cmp > 0) {
					node = node.LeftChild;
				} else if (cmp < 0) {
					node = node.RightChild;
				} else {
					break;
				}
			}
			return node;
		}

		public Node FindByIndex(Node node, int index)
		{
			if (node == null) {
				return null;
			}

			var currentIndex = Count(node) - Count(node.RightChild) - 1;
			while (node != null) {
				if (currentIndex == index) {
					return node;
				}

				if (currentIndex > index) {
					node = node.LeftChild;
					currentIndex -= (Count(node == null ? null : node.RightChild) + 1);
				} else {
					node = node.RightChild;
					currentIndex += (Count(node == null ? null : node.LeftChild) + 1);
				}
			}

			return null;
		}

		public Node Merge(Node left, Node right)
		{
			if (left == null || right == null) {
				return left == null ? right : left;
			}

			if ((double)Count(left) / (double)(Count(left) + Count(right)) > random_.NextDouble()) {
				left.RightChild = Merge(left.RightChild, right);
				return Update(left);
			} else {
				right.LeftChild = Merge(left, right.LeftChild);
				return Update(right);
			}
		}

		public Splited Split(Node node, int k)
		{
			if (node == null) {
				return new Splited(null, null);
			}

			//[0, k), [k, n)
			if (k <= Count(node.LeftChild)) {
				var s = Split(node.LeftChild, k);
				node.LeftChild = s.Right;
				return new Splited(s.Left, Update(node));
			} else {
				var s = Split(node.RightChild, k - Count(node.LeftChild) - 1);
				node.RightChild = s.Left;
				return new Splited(Update(node), s.Right);
			}
		}

		public int UpperBound(Node node, T value)
		{
			if (node == null) {
				return -1;
			}

			var ret = 0;
			while (node != null) {
				if (Compare(node.Value, value) > 0) {
					node = node?.LeftChild;
				} else {
					ret += Count(node?.LeftChild) + 1;
					node = node?.RightChild;
				}
			}

			return ret;
		}

		public T UpperBoundValue(Node node, T value)
		{
			T ret = inf_;
			while (node != null) {
				if (Compare(node.Value, value) > 0) {
					ret = node.Value;
					node = node?.LeftChild;
				} else {
					node = node?.RightChild;
				}
			}

			return ret;
		}

		public int LowerBound(Node node, T value)
		{
			if (node == null) {
				return -1;
			}

			var ret = 0;
			while (node != null) {
				if (Compare(node.Value, value) >= 0) {
					node = node?.LeftChild;
				} else {
					ret += Count(node?.LeftChild) + 1;
					node = node?.RightChild;
				}
			}

			return ret;
		}

		public T LowerBoundValue(Node node, T value)
		{
			T ret = inf_;
			while (node != null) {
				if (Compare(node.Value, value) >= 0) {
					ret = node.Value;
					node = node?.LeftChild;
				} else {
					node = node?.RightChild;
				}
			}

			return ret;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		private int Compare(T x, T y)
			=> x.CompareTo(y);

		private Node Update(Node node)
		{
			node.SubTreeSize = Count(node.LeftChild) + Count(node.RightChild) + 1;
			return node;
		}

		public IEnumerable<T> Enumerate(Node node)
		{
			var ret = new List<T>();
			Enumerate(node, ret);
			return ret;
		}

		private void Enumerate(Node node, List<T> ret)
		{
			if (node == null) {
				return;
			}

			Enumerate(node.LeftChild, ret);
			ret.Add(node.Value);
			Enumerate(node.RightChild, ret);
		}

		public class Node
		{
			public T Value { get; set; }
			public Node LeftChild { get; set; }
			public Node RightChild { get; set; }
			public int SubTreeSize { get; set; }
			public Node(T value)
			{
				Value = value;
				SubTreeSize = 1;
			}
		}

		public class Splited
		{
			public Node Left { get; set; }
			public Node Right { get; set; }
			public Splited(Node left, Node right)
			{
				Left = left;
				Right = right;
			}
		}
	}

	public class HashMap<TKey, TValue> : Dictionary<TKey, TValue>
	{
		private readonly Func<TKey, TValue> initialzier_;
		public HashMap(Func<TKey, TValue> initialzier)
			: base()
		{
			initialzier_ = initialzier;
		}

		public HashMap(Func<TKey, TValue> initialzier, int capacity)
			: base(capacity)
		{
			initialzier_ = initialzier;
		}

		new public TValue this[TKey key]
		{
			get
			{
				if (ContainsKey(key) == false) {
					base[key] = initialzier_(key);
				}

				return base[key];
			}

			set { base[key] = value; }
		}
	}

	public static class Helper
	{
		public static void UpdateMin<T>(ref T target, T value) where T : IComparable<T>
			=> target = target.CompareTo(value) > 0 ? value : target;

		public static void UpdateMin<T>(ref T target, T value, Action<T> onUpdated) where T : IComparable<T>
		{
			if (target.CompareTo(value) > 0) {
				target = value;
				onUpdated(value);
			}
		}

		public static void UpdateMax<T>(ref T target, T value) where T : IComparable<T>
			=> target = target.CompareTo(value) < 0 ? value : target;
		public static void UpdateMax<T>(ref T target, T value, Action<T> onUpdated) where T : IComparable<T>
		{
			if (target.CompareTo(value) < 0) {
				target = value;
				onUpdated(value);
			}
		}

		public static T[] Array<T>(int n, Func<int, T> init)
			=> Enumerable.Range(0, n).Select(x => init(x)).ToArray();
		public static List<T> List<T>(int n, Func<int, T> init)
			=> Enumerable.Range(0, n).Select(x => init(x)).ToList();
		public static T[,] Array2<T>(int n, int m, T init)
		{
			var array = new T[n, m];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					array[i, j] = init;
				}
			}

			return array;
		}

		public static T[,] Array2<T>(int n, int m, Func<int, int, T> initializer)
		{
			var array = new T[n, m];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					array[i, j] = initializer(i, j);
				}
			}

			return array;
		}

		public static T[,,] Array3<T>(int n1, int n2, int n3, T init)
		{
			var array = new T[n1, n2, n3];
			for (int i1 = 0; i1 < n1; i1++) {
				for (int i2 = 0; i2 < n2; i2++) {
					for (int i3 = 0; i3 < n3; i3++) {
						array[i1, i2, i3] = init;
					}
				}
			}

			return array;
		}

		private static readonly int[] delta4_ = { 1, 0, -1, 0, 1 };
		public static void DoAt4(int i, int j, int imax, int jmax, Action<int, int> action)
		{
			for (int n = 0; n < 4; n++) {
				int ii = i + delta4_[n];
				int jj = j + delta4_[n + 1];
				if ((uint)ii < (uint)imax && (uint)jj < (uint)jmax) {
					action(ii, jj);
				}
			}
		}
	}

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

		public Scanner(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);
		}
	}
}