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.Text.RegularExpressions;
using System.Collections.Generic;
using static program.Input;
using static program.MyMath;
using static System.Console;
using static program.Const;

namespace program
{
    static class Const
    {
        public static string alp = "abcdefghijklmnopqrstuvwxyz";
        public static string Alp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        public const long MOD = 1000000007;
        public static int INF = int.MaxValue;
        public static long INFL = long.MaxValue;
    }
    class Program
    {
        static void Main()
        {
            var n = ScanInt();
            var s = ScanStr();
            var q = ScanInt();

            var seg = new SegmentTree(s.ToCharArray());
            for (int i = 0; i < q; i++)
            {
                var qu = ScanStrArray();
                if (qu[0] == "1")
                {
                    seg.Update(int.Parse(qu[1])-1, qu[2][0]);
                }
                else
                {
                    WriteLine(seg.Get(int.Parse(qu[1]) - 1, int.Parse(qu[2])).CountBit());
                }
            }

        }

        class SegmentTree
        {
            int n;
            long[] node;
            private Dictionary<char, int> ext = Enumerable.Range(0, alp.Length).ToDictionary(_ => alp[_], _ => _);
            public SegmentTree(char[] arr)
            {
                int size = arr.Length;
                n = size.Ceiling2();
                node = new long[2 * n - 1];

                for (int i = 0; i < size; i++)
                    node[i + n - 1] = (1 << ext[arr[i]]);

                for (int i = n - 2; i >= 0; i--)
                    node[i] = node[2 * i + 1] | node[2 * i + 2];
            }
            public void Update(int x, char val)
            {
                x += (n - 1);

                node[x] = 1 << ext[val];
                while (x > 0)
                {
                    x = (x - 1) / 2;
                    node[x] = node[2 * x + 1] | node[2 * x + 2];
                }
            }
            public long Get(int a, int b, int k = 0, int l = 0, int r = -1)
            {
                if (r < 0) r = n;

                if (r <= a || b <= l) return 0;

                if (a <= l && r <= b) return node[k];

                var vl = Get(a, b, 2 * k + 1, l, (l + r) / 2);
                var vr = Get(a, b, 2 * k + 2, (l + r) / 2, r);
                return vl | vr;
            }
        }
    }

    static class Input
    {
        static public int ScanInt() { return int.Parse(Console.ReadLine()); }
        static public uint ScanUint() { return uint.Parse(Console.ReadLine()); }
        static public double ScanDouble() { return double.Parse(Console.ReadLine()); }
        static public long ScanLong() { return long.Parse(Console.ReadLine()); }
        static public ulong ScanUlong() { return ulong.Parse(Console.ReadLine()); }
        static public int[] ScanIntArray() { return Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray(); }
        static public uint[] ScanUintArray() { return Console.ReadLine().Split().Select(x => uint.Parse(x)).ToArray(); }
        static public double[] ScanDoubleArray() { return Console.ReadLine().Split().Select(x => double.Parse(x)).ToArray(); }
        static public long[] ScanLongArray() { return Console.ReadLine().Split().Select(x => long.Parse(x)).ToArray(); }
        static public ulong[] ScanUlongArray() { return Console.ReadLine().Split().Select(x => ulong.Parse(x)).ToArray(); }

        static public string ScanStr() { return Console.ReadLine(); }
        static public string[] ScanStrArray() { return ScanStr().Split(); }
        static public string RegaxReplace(this string arg1, string pattern, string replacement) { return Regex.Replace(arg1, pattern, replacement); }
        static public string Join<T>(this IEnumerable<T> source, string separator) { return string.Join(separator, source); }
        static public void Sort(this string[] array) { Array.Sort(array, StringComparer.OrdinalIgnoreCase); }
        static public void StringSort(this List<string> list) { list.Sort(StringComparer.OrdinalIgnoreCase); }
    }

    static class MyMath
    {
        static public int CountBit(this long a)
        {
            if (a == 0) return 0;
            int c = 1;
            for (; a>1; a/=2) if (a % 2 == 1) c++;
            return c;
        }
        static public int Floor2(this int a) { int N = 1; while (N < a) N *= 2; return N / 2; }
        static public long Floor2(this long a) { long N = 1; while (N < a) N *= 2; return N / 2; }
        static public int Ceiling2(this int a) { int N = 1; while (N < a) N *= 2; return N; }
        static public long Ceiling2(this long a) { long N = 1; while (N < a) N *= 2; return N; }
        static public long[] CumSum(this long[] a) { var res = new long[a.Length]; long s = 0; for (int i = 0; i < a.Length; i++) res[i] = (s += a[i]); return res; }
        static public IEnumerable<int> PrimeFactors(this int n)
        {
            int tmp = n;
            for (int i = 2; i * i <= n; i++)
            {
                while (tmp % i == 0)
                {
                    tmp /= i;
                    yield return i;
                }
            }
            if (tmp != 1) yield return tmp;
        }
        static public IEnumerable<long> PrimeFactors(this long n)
        {
            var tmp = n;
            for (long i = 2; i * i <= n; i++)
            {
                while (tmp % i == 0)
                {
                    tmp /= i;
                    yield return i;
                }
            }
            if (tmp != 1) yield return tmp;
        }
        static public int Digit(this int n, int d) { if (d < 0) return 0; return (n % (int)Math.Pow(10, d + 1)) / (int)Math.Pow(10, d); }
        static public int Length(this int n) { if (n < 0) return 0; return (int)Math.Log10(n) + 1; }
        static public int Top(this int n) { return n.Digit(n.Length() - 1); }
        public static bool isPrime(this int p)
        {
            if (p <= 1) return false;
            for (int i = 2; i * i <= p; i++)
                if (p % i == 0) return false;
            return true;
        }
        public static bool isPrime(this long p)
        {
            if (p <= 1) return false;
            for (long i = 2; i * i <= p; i++)
                if (p % i == 0) return false;
            return true;
        }
        public static int GCD(int a, int b)
        {
            Func<int, int, int> gcd = null;
            gcd = (x, y) => y == 0 ? x : gcd(y, x % y);
            return a > b ? gcd(a, b) : gcd(b, a);
        }
        public static long GCD(long a, long b)
        {
            Func<long, long, long> gcd = null;
            gcd = (x, y) => y == 0 ? x : gcd(y, x % y);
            return a > b ? gcd(a, b) : gcd(b, a);
        }
        public static ulong extGCD(ulong a, ulong b, ref ulong x, ref ulong y)
        {
            if (b == 0)
            {
                x = 1;
                y = 0;
                return a;
            }
            var d = extGCD(b, a % b, ref y, ref x);
            y -= a / b * x;
            return d;
        }
        public static int LCM(int a, int b) { return a * b / GCD(a, b); }
        public static long LCM(long a, long b) { return a * b / GCD(a, b); }
        public static long Inv(this long a, long m = MOD)
        {
            long b = m, u = 1, v = 0;
            while (b != 0)
            {
                long t = a / b;
                a -= t * b;
                swap(ref a, ref b);
                u -= t * v;
                swap(ref u, ref v);
            }
            u %= m;
            if (u < 0) u += m;
            return u;
        }
        private static void swap(ref long a, ref long b)
        {
            var t = a;
            a = b;
            b = t;
        }
        public static long ModPow(long a, long n, long m = MOD)
        {
            long res = 1;
            while (n > 0)
            {
                if (n % 2 == 1) res = (res * a) % m;
                a = (a * a) % m;
                n >>= 1;
            }
            return res;
        }

    }


}