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

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = ConsoleExtension.ReadInt();
            var S = Console.ReadLine();
            var sArray = S.ToCharArray();
            var Q = ConsoleExtension.ReadInt();

            var qArray = new char[Q, 3];
            for (int i = 0; i < Q; i++)
            {
                var temp = ConsoleExtension.ReadStringArray();
                qArray[i, 0] = temp[0][0];
                qArray[i, 1] = temp[1][0];
                qArray[i, 2] = temp[2][0];
            }

            for (int i = 0; i < Q; i++)
            {
                var a = qArray[i, 0];
                var b = qArray[i, 1] - 48;

                if (a == '1')
                {
                    var c = qArray[i, 2];
                    sArray[b - 1] = c;
                }
                else
                {
                    var c = qArray[i, 2]-48;
                    var set = new HashSet<char>();
                    for (int j = b - 1; j < c; j++)
                    {
                        set.Add(sArray[j]);
                    }

                    Console.WriteLine(set.Count);
                }
            }
        }
    }

}

/*
namespace AtCoder.Algorithms
{
    public static partial class Algorithm
    {
    }
}
*/

namespace AtCoder.Collections
{
    public class Deque<T>
    {
        int dx;
        T[] buf;
        int mask;

        public int Count { get; private set; }
        public Deque() : this(8) { }
        public Deque(int capacity)
        {
            mask = capacity - 1;
            buf = new T[capacity];
        }
        public T this[int index]
        {
            get
            {
                return buf[(dx + index) & mask];
            }
            set
            {
                buf[(dx + index) & mask] = value;
            }
        }
        public void PushFront(T item)
        {
            if (Count == buf.Length) extend();
            dx = (dx + buf.Length - 1) & mask;
            buf[dx] = item;
            Count++;
        }
        public T PopFront()
        {
            var ret = buf[dx = (dx + 1) & mask];
            Count--;
            return ret;
        }
        public void PushBack(T item)
        {
            if (Count == buf.Length) extend();
            buf[(dx + Count++) & mask] = item;
        }
        public T PopBack()
        {
            var ret = buf[(dx + --Count) & mask];
            return ret;
        }
        public T Last()
        {
            return buf[(dx + Count - 1) & mask];
        }
        public T First()
        {
            return buf[dx];
        }
        void extend()
        {
            var nbuf = new T[buf.Length * 2];
            for (int i = 0; i < buf.Length; i++)
                nbuf[i] = buf[(dx + i) & mask];
            mask = mask * 2 + 1;
            dx = 0;
            buf = nbuf;
        }
        public void Clear() { Count = 0; }
        public bool Any() { return Count != 0; }
        public void Insert(int index, T item)
        {
            PushFront(item);
            for (int i = 0; i < index; i++)
                this[i] = this[i + 1];
            this[index] = item;
        }
        public T RemoveAt(int index)
        {
            var ret = this[index];
            for (int i = index; i > 0; i--)
                this[i] = this[i - 1];
            PopFront();
            return ret;
        }
        public T[] Items
        {
            get
            {

                var ret = new T[Count];
                for (int i = 0; i < Count; i++)
                    ret[i] = this[i];
                return ret;
            }
        }
    }
    public class PriorityQueue<T> : IEnumerable<T>
    {
        private readonly List<T> _data = new List<T>();
        private readonly IComparer<T> _comparer;
        private readonly bool _isDescending;

        public PriorityQueue(IComparer<T> comparer, bool isDescending = true)
        {
            _comparer = comparer;
            _isDescending = isDescending;
        }

        public PriorityQueue(Comparison<T> comparison, bool isDescending = true)
            : this(Comparer<T>.Create(comparison), isDescending)
        {
        }

        public PriorityQueue(bool isDescending = true)
            : this(Comparer<T>.Default, isDescending)
        {
        }

        public void Enqueue(T item)
        {
            _data.Add(item);
            var childIndex = _data.Count - 1;
            while (childIndex > 0)
            {
                var parentIndex = (childIndex - 1) / 2;
                if (Compare(_data[childIndex], _data[parentIndex]) >= 0)
                    break;
                Swap(childIndex, parentIndex);
                childIndex = parentIndex;
            }
        }

        public T Dequeue()
        {
            var lastIndex = _data.Count - 1;
            var firstItem = _data[0];
            _data[0] = _data[lastIndex];
            _data.RemoveAt(lastIndex--);
            var parentIndex = 0;
            while (true)
            {
                var childIndex = parentIndex * 2 + 1;
                if (childIndex > lastIndex)
                    break;
                var rightChild = childIndex + 1;
                if (rightChild <= lastIndex && Compare(_data[rightChild], _data[childIndex]) < 0)
                    childIndex = rightChild;
                if (Compare(_data[parentIndex], _data[childIndex]) <= 0)
                    break;
                Swap(parentIndex, childIndex);
                parentIndex = childIndex;
            }
            return firstItem;
        }

        public T Peek()
        {
            return _data[0];
        }

        private void Swap(int a, int b)
        {
            var tmp = _data[a];
            _data[a] = _data[b];
            _data[b] = tmp;
        }

        private int Compare(T a, T b)
        {
            return _isDescending ? _comparer.Compare(b, a) : _comparer.Compare(a, b);
        }

        public int Count => _data.Count;

        public IEnumerator<T> GetEnumerator()
        {
            return _data.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
    }
    public class PriorityQueue<TKey, TValue> : IEnumerable<TValue>
    {
        private readonly List<KeyValuePair<TKey, TValue>> _data = new List<KeyValuePair<TKey, TValue>>();
        private readonly bool _isDescending;
        private readonly Func<TValue, TKey> _keySelector;
        private readonly IComparer<TKey> _keyComparer;

        public PriorityQueue(Func<TValue, TKey> keySelector, bool isDescending = true)
            : this(keySelector, Comparer<TKey>.Default, isDescending)
        {
        }

        public PriorityQueue(Func<TValue, TKey> keySelector, IComparer<TKey> keyComparer, bool isDescending = true)
        {
            _keySelector = keySelector;
            _keyComparer = keyComparer;
            _isDescending = isDescending;
        }

        public void Enqueue(TValue item)
        {
            _data.Add(new KeyValuePair<TKey, TValue>(_keySelector(item), item));
            var childIndex = _data.Count - 1;
            while (childIndex > 0)
            {
                var parentIndex = (childIndex - 1) / 2;
                if (Compare(_data[childIndex].Key, _data[parentIndex].Key) >= 0)
                    break;
                Swap(childIndex, parentIndex);
                childIndex = parentIndex;
            }
        }

        public TValue Dequeue()
        {
            var lastIndex = _data.Count - 1;
            var firstItem = _data[0];
            _data[0] = _data[lastIndex];
            _data.RemoveAt(lastIndex--);
            var parentIndex = 0;
            while (true)
            {
                var childIndex = parentIndex * 2 + 1;
                if (childIndex > lastIndex)
                    break;
                var rightChild = childIndex + 1;
                if (rightChild <= lastIndex && Compare(_data[rightChild].Key, _data[childIndex].Key) < 0)
                    childIndex = rightChild;
                if (Compare(_data[parentIndex].Key, _data[childIndex].Key) <= 0)
                    break;
                Swap(parentIndex, childIndex);
                parentIndex = childIndex;
            }
            return firstItem.Value;
        }

        public TValue Peek()
        {
            return _data[0].Value;
        }

        private void Swap(int a, int b)
        {
            var tmp = _data[a];
            _data[a] = _data[b];
            _data[b] = tmp;
        }

        private int Compare(TKey a, TKey b)
        {
            return _isDescending ? _keyComparer.Compare(b, a) : _keyComparer.Compare(a, b);
        }

        public int Count => _data.Count;

        public IEnumerator<TValue> GetEnumerator()
        {
            return _data.Select(r => r.Value).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
    }
    class Treap<T> where T : IComparable
    {
        private class Node
        {
            private static Random g_rand = new Random();
            private readonly int m_rank = g_rand.Next();
            private readonly T m_value;
            private Node m_lch;
            private Node m_rch;
            private int m_count;

            public Node(T value)
            {
                m_value = value;
                m_count = 1;
            }

            private static int Count(Node node)
            {
                return (node != null) ? node.m_count : 0;
            }

            private Node Update()
            {
                m_count = Count(m_lch) + Count(m_rch) + 1;
                return this;
            }

            public static Node Merge(Node a, Node b)
            {
                if (a == null) { return b; }
                if (b == null) { return a; }

                if (a.m_rank < b.m_rank)
                {
                    a.m_rch = Merge(a.m_rch, b);
                    return a.Update();
                }
                else
                {
                    b.m_lch = Merge(a, b.m_lch);
                    return b.Update();
                }
            }

            public static Tuple<Node, Node> Split(Node t, int k)
            {
                if (t == null) { return new Tuple<Node, Node>(null, null); }

                if (k <= Count(t.m_lch))
                {
                    var pair = Split(t.m_lch, k);
                    t.m_lch = pair.Item2;
                    return new Tuple<Node, Node>(pair.Item1, t.Update());
                }
                else
                {
                    var pair = Split(t.m_rch, k - Count(t.m_lch) - 1);
                    t.m_rch = pair.Item1;
                    return new Tuple<Node, Node>(t.Update(), pair.Item2);
                }
            }

            public int FindIndex(T value)
            {
                int L = Count(m_lch);
                if (value.CompareTo(m_value) < 0)
                {
                    return (m_lch != null) ? m_lch.FindIndex(value) : 0;
                }
                else if (value.CompareTo(m_value) > 0)
                {
                    return (m_rch != null) ? m_rch.FindIndex(value) + L + 1 : L + 1;
                }
                else
                {
                    return L;
                }
            }

            public T this[int i]
            {
                get
                {
                    int L = Count(m_lch);
                    if (i < L)
                    {
                        return m_lch[i];
                    }
                    else if (i > L)
                    {
                        return m_rch[i - L - 1];
                    }
                    else
                    {
                        return m_value;
                    }
                }
            }
        }

        private Node node;

        public void Insert(T value)
        {
            if (node != null)
            {
                int k = node.FindIndex(value);
                var pair = Node.Split(node, k);
                node = Node.Merge(Node.Merge(pair.Item1, new Node(value)), pair.Item2);
            }
            else
            {
                node = new Node(value);
            }
        }

        public int FindIndex(T value)
        {
            return node.FindIndex(value);
        }

        public T this[int i]
        {
            get
            {
                return node[i];
            }
        }
    }
}

namespace AtCoder.Mathematics
{
    public class MathEx
    {
        /// <summary>
        /// 最小公倍数
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        static long Lcm(long a, long b)
        {
            return a / Gcd(a, b) * b;
        }
        /// <summary>
        /// 最大公約数
        /// </summary>
        /// <param name="m"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        static long Gcd(long m, long n)
        {
            if (n == 0) return m;
            return Gcd(n, m % n);
        }
    }
    public struct ModInt
    {
        public const long Mod = (int)1e9 + 7;
        public long num;
        public ModInt(long n) { num = n; }
        public override string ToString() { return num.ToString(); }
        public static ModInt operator +(ModInt l, ModInt r) { l.num += r.num; if (l.num >= Mod) l.num -= Mod; return l; }
        public static ModInt operator -(ModInt l, ModInt r) { l.num -= r.num; if (l.num < 0) l.num += Mod; return l; }
        public static ModInt operator *(ModInt l, ModInt r) { return new ModInt(l.num * r.num % Mod); }
        public static ModInt operator /(ModInt l, ModInt r) { return l * Pow(r, Mod - 2); }
        public static implicit operator ModInt(long n) { n %= Mod; if (n < 0) n += Mod; return new ModInt(n); }
        public static ModInt Pow(ModInt v, long k) { return Pow(v.num, k); }
        public static ModInt Pow(long v, long k)
        {
            long ret = 1;
            for (k %= Mod - 1; k > 0; k >>= 1, v = v * v % Mod)
                if ((k & 1) == 1) ret = ret * v % Mod;
            return new ModInt(ret);
        }
        public static ModInt Combination(long n, long r)
        {
            ModInt ans = 1;
            for (int i = 0; i < r; i++)
            {
                ans *= n - i;
            }
            for (int i = 1; i <= r; i++)
            {
                ans /= i;
            }
            return ans;
        }
        public static ModInt Inverse(ModInt v) { return Pow(v, Mod - 2); }
    }
}

namespace AtCoder.Extensions
{
    public static class Extension
    {
        static int BinarySearch<T>(IList<T> source, T value, IComparer<T> comparer, bool isLowerBound)
        {
            return BinarySearch(source, 0, source.Count - 1, value, comparer, isLowerBound);
        }
        static int BinarySearch<T>(IList<T> source, int start, int end, T value, IComparer<T> comparer, bool isLowerBound)
        {
            while (start <= end)
            {
                var mid = (start + end) / 2;
                var result = comparer.Compare(source[mid], value);
                if (result < 0 || (result == 0 && !isLowerBound)) start = mid + 1;
                else end = mid - 1;
            }
            return start;
        }
        public static int LowerBound<T>(this IList<T> source, T value, Comparison<T> f) => BinarySearch(source, value, Comparer<T>.Create(f), true);
        public static int LowerBound<T>(this IList<T> source, int start, T value, Comparison<T> f) => BinarySearch(source, start, source.Count - 1, value, Comparer<T>.Create(f), true);
        public static int LowerBound<T>(this IList<T> source, int start, int end, T value, Comparison<T> f) => BinarySearch(source, start, end, value, Comparer<T>.Create(f), true);
        public static int LowerBound<T>(this IList<T> source, T value) => BinarySearch(source, value, Comparer<T>.Default, true);
        public static int LowerBound<T>(this IList<T> source, int start, T value) => BinarySearch(source, start, source.Count - 1, value, Comparer<T>.Default, true);
        public static int LowerBound<T>(this IList<T> source, int start, int end, T value) => BinarySearch(source, start, end, value, Comparer<T>.Default, true);
        public static int UpperBound<T>(this IList<T> source, T value, Comparison<T> cmp) => BinarySearch(source, value, Comparer<T>.Create(cmp), false);
        public static int UpperBound<T>(this IList<T> source, int start, T value, Comparison<T> cmp) => BinarySearch(source, start, source.Count - 1, value, Comparer<T>.Create(cmp), false);
        public static int UpperBound<T>(this IList<T> source, int start, int end, T value, Comparison<T> cmp) => BinarySearch(source, start, end, value, Comparer<T>.Create(cmp), false);
        public static int UpperBound<T>(this IList<T> source, T value) => BinarySearch(source, value, Comparer<T>.Default, false);
        public static int UpperBound<T>(this IList<T> source, int start, T value) => BinarySearch(source, start, source.Count - 1, value, Comparer<T>.Default, false);
        public static int UpperBound<T>(this IList<T> source, int start, int end, T value) => BinarySearch(source, start, end, value, Comparer<T>.Default, false);
        public static PriorityQueue<T> AsPriorityQueue<T>(this IEnumerable<T> source, bool isDescending = true)
        {
            var queue = new PriorityQueue<T>(isDescending);
            foreach (var item in source)
            {
                queue.Enqueue(item);
            }

            return queue;
        }
        public static PriorityQueue<TKey, TSource> AsPriorityQueue<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, bool isDescending = true)
        {
            var queue = new PriorityQueue<TKey, TSource>(keySelector, isDescending);
            foreach (var item in source)
            {
                queue.Enqueue(item);
            }

            return queue;
        }
        public static bool ChangeMin<T>(this IList<T> source, int index, T value)
            where T : IComparable
        {
            var a = source[index];
            if (a.CompareTo(value) > 0)
            {
                source[index] = value;
                return true;
            }
            return false;
        }
        public static bool ChangeMax<T>(this IList<T> source, int index, T value)
            where T : IComparable
        {
            var a = source[index];
            if (a.CompareTo(value) < 0)
            {
                source[index] = value;
                return true;
            }
            return false;
        }
    }
    public static class ConsoleExtension
    {
        public static int ReadInt() => int.Parse(Console.ReadLine());
        public static long ReadLong() => long.Parse(Console.ReadLine());
        public static double ReadDouble() => double.Parse(Console.ReadLine());
        public static string[] ReadStringArray() => Console.ReadLine().Split().ToArray();
        public static string[] ReadStringArray(int n) => Enumerable.Range(0, n).Select(_ => Console.ReadLine()).ToArray();
        public static int[] ReadIntArray() => Console.ReadLine().Split().Select(int.Parse).ToArray();
        public static int[] ReadIntArray(int n) => Enumerable.Range(0, n).Select(_ => ReadInt()).ToArray();
        public static long[] ReadLongArray() => Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
        public static long[] ReadLongArray(int n) => Enumerable.Range(0, n).Select(_ => ReadLong()).ToArray();
        public static double[] ReadDoubleArray() => Console.ReadLine().Split().Select(double.Parse).ToArray();
        public static double[] ReadDoubleArray(int n) => Enumerable.Range(0, n).Select(_ => ReadDouble()).ToArray();
    }
}