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.Linq;
using System.Collections.Generic;

namespace Contest
{
    class Program
    {
        static void Main(string[] args)
        {
            ABC157E();
        }

        static void ABC157E()
        {
            var N = ReadInt();
            var S = ReadStr();
            var Q = ReadInt();

            SortedList<int, int>[] indices = Enumerable.Range(0, 26).Select(i => new SortedList<int, int>()).ToArray();
            int[] array = new int[N];

            for (int i = 0; i < N; ++i)
            {
                var c = S[i] - 'a';
                array[i] = c;
                indices[c].Add(i, i);
            }

            for (int q = 0; q < Q; ++q)
            {
                var query = ReadStrN();
                if (query[0] == "1")
                {
                    var i = Convert.ToInt32(query[1]) - 1;
                    var c = Convert.ToChar(query[2]) - 'a';
                    indices[array[i]].Remove(i);
                    indices[c].Add(i, i);
                    array[i] = c;
                }
                else
                {
                    var l = Convert.ToInt32(query[1]) - 1;
                    var r = Convert.ToInt32(query[2]) - 1;
                    int count = 0;
                    for (int i = 0; i < 26; ++i)
                    {
                        var lb = Util.Lowerbound(indices[i], l);
                        var ub = Util.Upperbound(indices[i], r);
                        if (ub > lb) { count += 1; }
                    }
                    Console.WriteLine(count);
                }
            }
        }

        class Util
        {
            public static readonly long P = 1000000007;

            public static long Sum(long[] array)
            {
                return Sum(array, array.Length);
            }
            public static long Sum(long[] array, int n)
            {
                long total = 0;
                for(int i = 0; i < n; ++i)
                {
                    total += array[i];
                }
                return total;
            }
            public static long Count(long[] array, long x)
            {
                return Count(array, x, array.Length);
            }
            public static long Count(long[] array, long x, int n)
            {
                long total = 0;
                for (int i = 0; i < n; ++i)
                {
                    total += (array[i] == x ? 1 : 0);
                }
                return total;
            }
            public static long[] ASum(long[] array)
            {
                var res = new long[array.Length + 1];
                for (int i = 0; i < array.Length; ++i)
                {
                    res[i + 1] = res[i] + array[i];
                }
                return res;
            }

            public static long GCD(long a, long b)
            {
                while (b > 0)
                {
                    var c = a % b;
                    a = b;
                    b = c;
                }
                return a;
            }
            public static bool IsPrime(long x)
            {
                var fact = Factorize(x);
                return fact.Count == 1 && fact.Values.ElementAt(0) == 1;
            }
            public static Dictionary<long,int> Factorize(long x)
            {
                var dict = new Dictionary<long, int>();
                long minp = 2;
                while (x > 1)
                {
                    bool ok = false;
                    for(long p = minp; p * p <= x; ++p)
                    {
                        if (x % p == 0)
                        {
                            if (!dict.ContainsKey(p))
                            {
                                dict.Add(p, 0);
                            }
                            dict[p]++;
                            minp = p;
                            x /= p;
                            ok = true;
                            break;
                        }
                    }
                    if (!ok)
                    {
                        if (!dict.ContainsKey(x)) { dict.Add(x, 0); }
                        dict[x]++;
                        break;
                    }
                }
                return dict;
            }
            public static long Inverse(long a, long p)
            {
                long b = p, c = 1, d = 0, e = 0, f = 0;
                while (b != 0)
                {
                    e = a / b;
                    f = b; b = a - e * b; a = f;
                    f = d; d = c - e * d; c = f;
                }
                c %= p;
                return c + (c < 0 ? p : 0);
            }
            public static long[] Facts(int n, long p)
            {
                var facts = new long[n + 1];
                facts[0] = 1;
                for (int i = 1; i <= n; ++i)
                {
                    facts[i] = (i * facts[i - 1]) % p;
                }
                return facts;
            }
            public static Func<int,int,long> Combin(int n, long P)
            {
                var fact = Facts(n, P);
                var inv = fact.Select(x => Inverse(x, P)).ToArray();
                return (N, M) =>
                {
                    if (N < 0 || N < M || M < 0) { return 0; }
                    return (((fact[N] * inv[M]) % P) * inv[N - M]) % P;
                };
            }

            /// <summary>
            /// Calc Combin(n,m) where n is large and m<=mMax
            /// </summary>
            public static Func<int,int,long> CombinLarge(int mMax, long p)
            {
                var inv = Enumerable.Range(0, mMax + 1).Select(x => Util.Inverse(x, Util.P)).ToArray();
                return (n, m) =>
                {
                    long mult = 1;
                    for (int i = 1; i <= m; ++i)
                    {
                        mult = (((mult * (n + 1 - i)) % Util.P) * inv[i]) % Util.P;
                    }
                    return mult;
                };
            }

            /// <summary>
            /// Calc 2^n mod p
            /// </summary>
            public static long Power2(long n, long p)
            {
                return Power(2, n, p);
            }
            public static long Power(long a, long n, long p)
            {
                long res = 1;
                long mult = a;
                for (int i = 0; i < 40; ++i)
                {
                    if ((n & ((long)1 << i)) > 0)
                    {
                        res = (res * mult) % p;
                    }
                    mult = (mult * mult) % p;
                }
                return res;
            }

            public static int Lowerbound<T1,T2>(SortedList<T1,T2> list, T1 x) where T1 : IComparable<T1>
            {
                return Lowerbound(list.Keys, x);
            }
            public static int Upperbound<T1, T2>(SortedList<T1, T2> list, T1 x) where T1 : IComparable<T1>
            {
                return Upperbound(list.Keys, x);
            }
            public static int Lowerbound<T>(IList<T> list, T x) where T : IComparable<T>
            {
                int min = 0;
                int max = list.Count;
                while (min < max)
                {
                    var mid = (min + max) / 2;
                    if (list[mid].CompareTo(x) >= 0)
                    {
                        max = mid;
                    }
                    else
                    {
                        min = mid + 1;
                    }
                }
                return max;
            }
            public static int Upperbound<T>(IList<T> list, T x) where T : IComparable<T>
            {
                int min = 0;
                int max = list.Count;
                while (min < max)
                {
                    var mid = (min + max) / 2;
                    if (list[mid].CompareTo(x) > 0)
                    {
                        max = mid;
                    }
                    else
                    {
                        min = mid + 1;
                    }
                }
                return max;
            }
        }

        class Graph
        {
            public static HashSet<int> ConnectedComponent(Dictionary<int, HashSet<int>> adj, int idx)
            {
                HashSet<int> comp = new HashSet<int>();
                comp.Add(idx);
                List<int> target = new List<int>();
                target.Add(idx);
                while (target.Count > 0)
                {
                    List<int> next = new List<int>();
                    foreach (var p in target)
                    {
                        foreach (var f in adj[p])
                        {
                            if (!comp.Contains(f))
                            {
                                next.Add(f);
                                comp.Add(f);
                            }
                        }
                    }
                    target = next;
                }
                return comp;
            }
        }

        static string ReadStr()
        {
            return Console.ReadLine();
        }
        static string[] ReadStrN()
        {
            return Console.ReadLine().Split(' ');
        }
        static int ReadInt()
        {
            return Convert.ToInt32(Console.ReadLine());
        }
        static int[] ReadIntN()
        {
            return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray();
        }
        static long ReadLong()
        {
            return Convert.ToInt64(Console.ReadLine());
        }
        static long[] ReadLongN()
        {
            return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray();
        }
        static Tuple<string, string>[] ReadTupleStr(int n)
        {
            List<Tuple<string, string>> list = new List<Tuple<string, string>>();
            for (int i = 0; i < n; ++i)
            {
                var ab = ReadStrN();
                list.Add(new Tuple<string, string>(ab[0], ab[1]));
            }
            return list.ToArray();
        }
        static Tuple<long, long>[] ReadTupleLong(int n)
        {
            List<Tuple<long, long>> list = new List<Tuple<long, long>>();
            for (int i = 0; i < n; ++i)
            {
                var ab = ReadLongN();
                list.Add(new Tuple<long, long>(ab[0], ab[1]));
            }
            return list.ToArray();
        }
        static Tuple<double, double>[] ReadTupleDouble(int n)
        {
            List<Tuple<double, double>> list = new List<Tuple<double, double>>();
            for (int i = 0; i < n; ++i)
            {
                var ab = ReadLongN();
                list.Add(new Tuple<double, double>(ab[0], ab[1]));
            }
            return list.ToArray();
        }
        static Dictionary<int, HashSet<int>> ReadAdjacencyList(int N, int M)
        {
            Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
            for (int i = 0; i <= N; ++i)
            {
                dict.Add(i, new HashSet<int>());
            }
            for (int i = 0; i < M; ++i)
            {
                var ab = ReadIntN();
                dict[ab[0]].Add(ab[1]);
                dict[ab[1]].Add(ab[0]);
            }
            return dict;
        }
    }
}