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)

namespace AtCoderCS
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Linq;
    using static System.Console;
    using static System.Math;
    using static Macro;
    using static Library;

    class Program
    {
        void Solve()
        {
            var N = IN.Int;
            var S = IN.String.ToCharArray();
            var Q = IN.Int;
            var abc = Enumerable.Range(0, 26);
            var indexesSet = new Set<int>[26];
            REP(26, i => indexesSet[i] = new Set<int>());
            REP(N, i => { indexesSet[S[i] - 'a'].Insert(i); });
            REP(Q, i =>
            {
                var type = IN.Int;
                switch (type)
                {
                    case 1:
                        var iq = IN.Int - 1;
                        var cq = IN.Char;
                        indexesSet[S[iq] - 'a'].Remove(iq);
                        S[iq] = cq;
                        indexesSet[S[iq] - 'a'].Insert(iq);
                        break;
                    case 2:
                        var lq = IN.Int - 1;
                        var rq = IN.Int - 1;
                        var num = 0;
                        foreach (var x in abc)
                        {
                            if (indexesSet[x].Count() > 0)
                            {
                                var xx = indexesSet[x].LowerBound(lq);
                                if (xx < indexesSet[x].Count() && indexesSet[x][xx] <= rq) num++;
                            }
                        }
                        WriteLine(num);
                        break;
                    default:
                        break;
                }
            });
        }

        private SimpleInput IN = new SimpleInput();
        private SimpleOutput OUT = new SimpleOutput();

        void Stream()
        {
            Solve();
            OUT.Flush();
        }

        static void Main(string[] args)
            => new Program().Stream();
    }

    // http://yambe2002.hatenablog.com/entry/2017/02/07/122421
    /// <summary>
    /// Self-Balancing Binary Search Tree
    /// (using Randamized BST)
    /// </summary>
    public class SB_BinarySearchTree<T> where T : IComparable
    {
        public class Node
        {
            public T Value;
            public Node LChild;
            public Node RChild;
            public int Count; //size of the sub tree

            public Node(T v)
            {
                Value = v;
                Count = 1;
            }
        }

        static Random _rnd = new Random();

        public static int Count(Node t)
        {
            return t == null ? 0 : t.Count;
        }

        static Node Update(Node t)
        {
            t.Count = Count(t.LChild) + Count(t.RChild) + 1;
            return t;
        }

        public static Node Merge(Node l, Node r)
        {
            if (l == null || r == null) return l == null ? r : l;
            if ((double) Count(l) / (double) (Count(l) + Count(r)) > _rnd.NextDouble())
            {
                l.RChild = Merge(l.RChild, r);
                return Update(l);
            }
            else
            {
                r.LChild = Merge(l, r.LChild);
                return Update(r);
            }
        }

        /// <summary>
        /// split as [0, k), [k, n)
        /// </summary>
        public static Tuple<Node, Node> Split(Node t, int k)
        {
            if (t == null) return new Tuple<Node, Node>(null, null);
            if (k <= Count(t.LChild))
            {
                var s = Split(t.LChild, k);
                t.LChild = s.Item2;
                return new Tuple<Node, Node>(s.Item1, Update(t));
            }
            else
            {
                var s = Split(t.RChild, k - Count(t.LChild) - 1);
                t.RChild = s.Item1;
                return new Tuple<Node, Node>(Update(t), s.Item2);
            }
        }

        public static Node Remove(Node t, T v)
        {
            if (Find(t, v) == null) return t;
            return RemoveAt(t, LowerBound(t, v));
        }

        public static Node RemoveAt(Node t, int k)
        {
            var s = Split(t, k);
            var s2 = Split(s.Item2, 1);
            return Merge(s.Item1, s2.Item2);
        }

        public static bool Contains(Node t, T v)
        {
            return Find(t, v) != null;
        }

        public static Node Find(Node t, T v)
        {
            while (t != null)
            {
                var cmp = t.Value.CompareTo(v);
                if (cmp > 0) t = t.LChild;
                else if (cmp < 0) t = t.RChild;
                else break;
            }
            return t;
        }

        public static Node FindByIndex(Node t, int idx)
        {
            if (t == null) return null;
            var currentIdx = Count(t) - Count(t.RChild) - 1;
            while (t != null)
            {
                if (currentIdx == idx) return t;
                if (currentIdx > idx)
                {
                    t = t.LChild;
                    currentIdx -= (Count(t == null ? null : t.RChild) + 1);
                }
                else
                {
                    t = t.RChild;
                    currentIdx += (Count(t == null ? null : t.LChild) + 1);
                }
            }
            return null;
        }

        public static int UpperBound(Node t, T v)
        {
            var torg = t;
            if (t == null) return -1;
            var ret = Int32.MaxValue;
            var idx = Count(t) - Count(t.RChild) - 1;
            while (t != null)
            {
                var cmp = t.Value.CompareTo(v);
                if (cmp > 0)
                {
                    ret = Math.Min(ret, idx);
                    t = t.LChild;
                    idx -= (Count(t == null ? null : t.RChild) + 1);
                }
                else if (cmp <= 0)
                {
                    t = t.RChild;
                    idx += (Count(t == null ? null : t.LChild) + 1);
                }
            }
            return ret == Int32.MaxValue ? Count(torg) : ret;
        }

        public static int LowerBound(Node t, T v)
        {
            var torg = t;
            if (t == null) return -1;
            var idx = Count(t) - Count(t.RChild) - 1;
            var ret = Int32.MaxValue;
            while (t != null)
            {
                var cmp = t.Value.CompareTo(v);
                if (cmp >= 0)
                {
                    if (cmp == 0) ret = Math.Min(ret, idx);
                    t = t.LChild;
                    if (t == null) ret = Math.Min(ret, idx);
                    idx -= t == null ? 0 : (Count(t.RChild) + 1);
                }
                else if (cmp < 0)
                {
                    t = t.RChild;
                    idx += (Count(t == null ? null : t.LChild) + 1);
                    if (t == null) return idx;
                }
            }
            return ret == Int32.MaxValue ? Count(torg) : ret;
        }

        public static Node Insert(Node t, T v)
        {
            var ub = LowerBound(t, v);
            return InsertByIdx(t, ub, v);
        }

        static Node InsertByIdx(Node t, int k, T v)
        {
            var s = Split(t, k);
            return Merge(Merge(s.Item1, new Node(v)), s.Item2);
        }

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

        static void Enumerate(Node t, List<T> ret)
        {
            if (t == null) return;
            Enumerate(t.LChild, ret);
            ret.Add(t.Value);
            Enumerate(t.RChild, ret);
        }
    }

    /// <summary>
    /// C-like set
    /// </summary>
    public class Set<T> where T : IComparable
    {
        protected SB_BinarySearchTree<T>.Node _root;

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

        public int Count()
        {
            return SB_BinarySearchTree<T>.Count(_root);
        }

        public virtual void Insert(T v)
        {
            if (_root == null) _root = new SB_BinarySearchTree<T>.Node(v);
            else
            {
                if (SB_BinarySearchTree<T>.Find(_root, v) != null) return;
                _root = SB_BinarySearchTree<T>.Insert(_root, v);
            }
        }

        public void Clear()
        {
            _root = null;
        }

        public void Remove(T v)
        {
            _root = SB_BinarySearchTree<T>.Remove(_root, v);
        }

        public bool Contains(T v)
        {
            return SB_BinarySearchTree<T>.Contains(_root, v);
        }

        public T ElementAt(int k)
        {
            var node = SB_BinarySearchTree<T>.FindByIndex(_root, k);
            if (node == null) throw new IndexOutOfRangeException();
            return node.Value;
        }

        public int Count(T v)
        {
            return SB_BinarySearchTree<T>.UpperBound(_root, v) - SB_BinarySearchTree<T>.LowerBound(_root, v);
        }

        public int LowerBound(T v)
        {
            return SB_BinarySearchTree<T>.LowerBound(_root, v);
        }

        public int UpperBound(T v)
        {
            return SB_BinarySearchTree<T>.UpperBound(_root, v);
        }

        public Tuple<int, int> EqualRange(T v)
        {
            if (!Contains(v)) return new Tuple<int, int>(-1, -1);
            return new Tuple<int, int>(SB_BinarySearchTree<T>.LowerBound(_root, v),
                SB_BinarySearchTree<T>.UpperBound(_root, v) - 1);
        }

        public List<T> ToList()
        {
            return new List<T>(SB_BinarySearchTree<T>.Enumerate(_root));
        }
    }

    #region IO
    class SimpleInput
    {
        private string[] _nextBuffer;
        private int _bufferCnt;
        private readonly char[] _cs = new char[] {' ', '"', ','};

        public SimpleInput()
        {
            _nextBuffer = new string[0];
            _bufferCnt = 0;
        }

        string Next()
        {
            if (_bufferCnt < _nextBuffer.Length) return _nextBuffer[_bufferCnt++];
            var st = ReadLine();
            while (st == string.Empty) st = ReadLine();
            if (st != null) _nextBuffer = st.Split(_cs, StringSplitOptions.RemoveEmptyEntries);
            _bufferCnt = 0;
            return _nextBuffer[_bufferCnt++];
        }

        public string String => Next();
        public char Char => char.Parse(String);
        public int Int => int.Parse(String);
        public long Long => long.Parse(String);
        public double Double => double.Parse(String);
    }

    class SimpleOutput
    {
        private readonly StreamWriter _sw = new StreamWriter(OpenStandardOutput())
        {
#if DEBUG
            AutoFlush = true
#else
            AutoFlush = false
#endif
        };

        public SimpleOutput()
        {
            SetOut(_sw);
        }

        public void Flush() => Out.Flush();
    }
    #endregion

    #region MACRO
    static class Macro
    {
        public static void FOR(int a, int b, int n, Action<int> f)
        {
            for (int i = a; i < b; i += n) f(i);
        }

        public static void FOR(int a, int b, Action<int> f) => FOR(a, b, 1, f);
        public static void REP(int a, Action<int> f) => FOR(0, a, f);
    }
    #endregion

    #region Library
    static class Library
    {
        public static int BinarySearch(int ng, int ok, Func<int, bool> f)
        {
            while (Math.Abs(ok - ng) > 1)
            {
                var mid = (ok + ng) / 2;
                if (f(mid)) ok = mid;
                else ng = mid;
            }
            return ok;
        }

        public static int LowerBound<T>(IReadOnlyList<T> a, T x) where T : IComparable<T> =>
            BinarySearch(-1, a.Count, (m) => a[m].CompareTo(x) >= 0);
        public static int UpperBound<T>(IReadOnlyList<T> a, T x) where T : IComparable<T> =>
            BinarySearch(-1, a.Count(), (m) => a[m].CompareTo(x) > 0);
    }

    class UnionFind
    {
        private int[] data;

        public UnionFind(int n)
        {
            data = new int[n + 1];
            for (var i = 0; i < n + 1; ++i) data[i] = -1;
        }

        public void Unite(int x, int y)
        {
            x = Root(x);
            y = Root(y);
            if (x == y) return;
            if (data[y] < data[x]) Swap(ref x, ref y);
            data[x] += data[y];
            data[y] = x;
        }

        public bool Find(int x, int y) => Root(x) == Root(y);
        public int Root(int x) => data[x] < 0 ? x : Root(data[x]);
        public int Size(int x) => -data[Root(x)];

        private void Swap(ref int a, ref int b)
        {
            var tmp = b;
            b = a;
            a = tmp;
        }
    }
    #endregion
}