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.Globalization;
using System.IO;
using System.Linq;

partial class Solver {
    public void Run() {
        var N = ni();
        var S = ns();
        var q = ni();
        var tree = new SegmentTree<int>((a, b) => a | b, 0);
        tree.Build(N, i => 1 << (S[i] - 'a'));
        for (int _ = 0; _ < q; _++) {
            var t = ni();
            if (t == 1) {
                var i = ni() - 1;
                var c = ns()[0];
                tree[i] = 1 << (c - 'a');
            } else {
                var l = ni() - 1;
                var r = ni() - 1;
                var x = tree.Query(l, r + 1);
                int count = 0;
                for (int i = 0; i < 26; i++) {
                    if (((1 << i) & x) > 0) count++;
                }
                cout.WriteLine(count);
            }
        }
    }
}

public sealed class SegmentTree<TMonoid> {
    public int Count { get; private set; }
    private TMonoid[] _node;
    private readonly TMonoid _identity;
    private readonly Func<TMonoid, TMonoid, TMonoid> _merge;

    public SegmentTree(Func<TMonoid, TMonoid, TMonoid> merge, TMonoid identity) {
        _merge = merge;
        _identity = identity;
    }

    public void Build(IList<TMonoid> array) {
        Build(array.Count, i => array[i]);
    }

    public void Build(int n, Func<int, TMonoid> create) {
        Count = n;

        _node = new TMonoid[2 * Count];

        for (int i = 0; i < n; i++) {
            _node[i + Count] = create(i);
        }

        for (int i = Count - 1; i > 0; i--) {
            _node[i] = _merge(_node[i << 1], _node[i << 1 | 1]);
        }
    }

    public void Set(int index, TMonoid val) {
        int p = index + Count;
        _node[p] = val;
        while (p > 0) {
            p >>= 1;
            _node[p] = _merge(_node[p << 1], _node[p << 1 | 1]);
        }
    }

    public TMonoid Query(int begin, int end) {
        TMonoid lv = _identity, rv = _identity;
        int l = begin + Count;
        int r = end + Count - 1;
        for (; l <= r; l >>= 1, r >>= 1) {
            if ((l & 1) == 1) { lv = _merge(lv, _node[l++]); }
            if ((r & 1) == 0) { rv = _merge(_node[r--], rv); }
        }
        return _merge(lv, rv);
    }

    public TMonoid this[int index] {
        get { return _node[index + Count]; }
        set { Set(index, value); }
    }
};

// PREWRITEN CODE BEGINS FROM HERE
partial class Solver : Scanner {
    public static void Main(string[] args) {
        new Solver(Console.In, Console.Out).Run();
    }

    TextReader cin;
    TextWriter cout;

    public Solver(TextReader reader, TextWriter writer)
        : base(reader) {
        this.cin = reader;
        this.cout = writer;
    }
    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer) {
    }

    public int ni() { return NextInt(); }
    public int[] ni(int n) { return NextIntArray(n); }
    public long nl() { return NextLong(); }
    public long[] nl(int n) { return NextLongArray(n); }
    public double nd() { return NextDouble(); }
    public string ns() { return Next(); }
}

public class Scanner {
    private TextReader Reader;
    private Queue<String> TokenQueue = new Queue<string>();
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    public Scanner()
        : this(Console.In) {
    }

    public Scanner(TextReader reader) {
        this.Reader = reader;
    }

    public int NextInt() { return Int32.Parse(Next(), ci); }
    public long NextLong() { return Int64.Parse(Next(), ci); }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size) {
        var array = new string[size];
        for (int i = 0; i < size; i++) array[i] = Next();
        return array;
    }
    public int[] NextIntArray(int size) {
        var array = new int[size];
        for (int i = 0; i < size; i++) array[i] = NextInt();
        return array;
    }

    public long[] NextLongArray(int size) {
        var array = new long[size];
        for (int i = 0; i < size; i++) array[i] = NextLong();
        return array;
    }

    public String Next() {
        if (TokenQueue.Count == 0) {
            if (!StockTokens()) throw new InvalidOperationException();
        }
        return TokenQueue.Dequeue();
    }

    public bool HasNext() {
        if (TokenQueue.Count > 0)
            return true;
        return StockTokens();
    }

    private bool StockTokens() {
        while (true) {
            var line = Reader.ReadLine();
            if (line == null) return false;
            var tokens = line.Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            if (tokens.Length == 0) continue;
            foreach (var token in tokens)
                TokenQueue.Enqueue(token);
            return true;
        }
    }
}