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;

class SegmentTree<T> {
    public int Size;
    public T[] Node;
    public Func<T, T, T> Calc;
    Func<T, T, bool> Eq;
    T Def;

    public SegmentTree(int n, T[] init, T def, Func<T, T, T> calc, Func<T, T, bool> eq) {
        Calc = calc;
        Def = def;
        Eq = eq;
        Size = 1;
        while(Size < n) Size *= 2;
        Node = new T[2 * Size - 1];
        for(int i = 0; i < init.Length; i++) {
            Node[(Size - 1) + i] = init[i];
        }
        for(int i = init.Length; i < Size; i++) {
            Node[(Size - 1) + i] = Def;
        }
        for(int i = (Size - 1) - 1; 0 <= i; i--) {
            Node[i] = Calc(Node[2 * i + 1], Node[2 * i + 2]);
        }
    }

    public void Update(int index, T value) {
        int pos = (Size - 1) + index;
        if(!Eq(Node[pos], value)) {
            Node[pos] = value;
            while(0 < pos) {
                pos = (pos - 1) / 2;
                Node[pos] = Calc(Node[2 * pos + 1], Node[2 * pos + 2]);
            }
        }
    }

    public T GetRange(int a, int b) {
        return GetRange(a, b, 0, 0, Size);
    }

    public T GetRange(int a, int b, int k, int l, int r) {
        if(r <= a || b <= l) {
            return Def;
        }
        if(a <= l && r <= b) {
            return Node[k];
        }
        T vl = GetRange(a, b, k * 2 + 1, l, (l + r) / 2);
        T vr = GetRange(a, b, k * 2 + 2, (l + r) / 2, r);
        return Calc(vl, vr);
    }
}
class E {
    static StreamWriter Out = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
    static void Main(string[] args) {
        int N;
        Input(out N);
        string S;
        Input(out S);
        int Q;
        Input(out Q);
        bool[][] init = new bool[N][];
        for(int i = 0; i < N; i++) {
            init[i] = new bool[26];
            init[i][S[i] - 97] = true;
        }
        SegmentTree<bool[]> seg = new SegmentTree<bool[]>(N, init, new bool[26],
            delegate (bool[] a, bool[] b) {
                bool[] ret = new bool[26];
                for(int i = 0; i < 26; i++) {
                    ret[i] = a[i] || b[i];
                }
                return ret;
            },
            delegate (bool[] a, bool[] b) {
                for(int i = 0; i < 26; i++) {
                    if(a[i] != b[i]) {
                        return false;
                    }
                }
                return true;
            }
        );
        for(int i = 0; i < Q; i++) {
            string t, a, b;
            Input(out t, out a, out b);
            if(int.Parse(t) == 1) {
                bool[] ins = new bool[26];
                ins[b[0] - 97] = true;
                seg.Update(int.Parse(a) - 1, ins);
            } else {
                bool[] result = seg.GetRange(int.Parse(a) - 1, int.Parse(b));
                int count = 0;
                for(int j = 0; j < 26; j++) {
                    if(result[j] == true) {
                        count++;
                    }
                }
                Print(count);
            }
        }
        Out.Flush();
    }
    static T ChangeT<T>(string s) => (T)Convert.ChangeType(s, typeof(T));
    static void Input<A>(out A a) {
        var words = Console.ReadLine().Split(new[] { ' ' }, options: StringSplitOptions.RemoveEmptyEntries);
        a = ChangeT<A>(words[0]);
    }
    static void Input<A, B>(out A a, out B b) {
        var words = Console.ReadLine().Split(new[] { ' ' }, options: StringSplitOptions.RemoveEmptyEntries);
        a = ChangeT<A>(words[0]);
        b = ChangeT<B>(words[1]);
    }
    static void Input<A, B, C>(out A a, out B b, out C c) {
        var words = Console.ReadLine().Split(new[] { ' ' }, options: StringSplitOptions.RemoveEmptyEntries);
        a = ChangeT<A>(words[0]);
        b = ChangeT<B>(words[1]);
        c = ChangeT<C>(words[2]);
    }
    static void Input<A, B, C, D>(out A a, out B b, out C c, out D d) {
        var words = Console.ReadLine().Split(new[] { ' ' }, options: StringSplitOptions.RemoveEmptyEntries);
        a = ChangeT<A>(words[0]);
        b = ChangeT<B>(words[1]);
        c = ChangeT<C>(words[2]);
        d = ChangeT<D>(words[3]);
    }
    static void Input<A, B, C, D, E>(out A a, out B b, out C c, out D d, out E e) {
        var words = Console.ReadLine().Split(new[] { ' ' }, options: StringSplitOptions.RemoveEmptyEntries);
        a = ChangeT<A>(words[0]);
        b = ChangeT<B>(words[1]);
        c = ChangeT<C>(words[2]);
        d = ChangeT<D>(words[3]);
        e = ChangeT<E>(words[4]);
    }
    static void Input<A, B, C, D, E, F>(out A a, out B b, out C c, out D d, out E e, out F f) {
        var words = Console.ReadLine().Split(new[] { ' ' }, options: StringSplitOptions.RemoveEmptyEntries);
        a = ChangeT<A>(words[0]);
        b = ChangeT<B>(words[1]);
        c = ChangeT<C>(words[2]);
        d = ChangeT<D>(words[3]);
        e = ChangeT<E>(words[4]);
        f = ChangeT<F>(words[5]);
    }
    static void InputArray<T>(out T[] array) {
        array = Console.ReadLine().Split(new[] { ' ' }, options: StringSplitOptions.RemoveEmptyEntries).Select(s => ChangeT<T>(s)).ToArray();
    }
    static void Print<T>(T s) => Out.WriteLine(s);
    static void Print<T>(params T[] s) => Out.WriteLine(string.Join(" ", s));
    static void Print<T>(IEnumerable<T> e) => Out.WriteLine(string.Join(" ", e));
}