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.Linq;

public static class Bits
{
    public static int PopCount(int bits)
    {
        bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
        bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
        bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
        bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
        return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
    }
}

public class Query
{
    public int QueryType;
    public int i;
    public char c;
    public int l;
    public int r;

    public Query(string s)
    {
        var vals = s.Split(' ');

        QueryType = int.Parse(vals[0]);
        if (QueryType == 1)
        {
            i = int.Parse(vals[1]);
            c = vals[2][0];
        }
        else
        {
            l = int.Parse(vals[1]);
            r = int.Parse(vals[2]);
        }
    }
}

public class SegmentTree
{
    // 元の配列の大きさ
    public int N { get; set; }
    // セグメント木のデータ(2^n-1)
    public int[] Node { get; set; }

    public SegmentTree(int[] a)
    {
        N = 1;
        while (N < a.Length) N *= 2;

        this.Node = new int[2 * N - 1];
        for (int i = 0; i < this.Node.Length; i++) this.Node[i] = 0;

        for (int i = 0; i < a.Length; i++) this.Node[N + i - 1] = a[i];
        for (int i = N - 2; i >= 0; i--)
        {
            this.Node[i] = this.Compare(this.Node[2 * i + 1], this.Node[2 * i + 2]);
        }
    }

    public void Update(int x, int v)
    {
        x += (this.N - 1);
        this.Node[x] = v;

        while (x > 0)
        {
            x = (x - 1) / 2;
            this.Node[x] = this.Compare(this.Node[2 * x + 1], this.Node[2 * x + 2]);
        }
    }

    public int GetKinds(int a, int b)
    {
        return this.GetKinds(a, b, 0, 0, this.N);
    }

    private int GetKinds(int a, int b, int k, int l, int r)
    {
        if (r <= a || b <= l) return 0;

        if (a <= l && r <= b) return this.Node[k];

        var vl = this.GetKinds(a, b, 2 * k + 1, l, (l + r) / 2);
        var vr = this.GetKinds(a, b, 2 * k + 2, (l + r) / 2, r);
        return this.Compare(vl, vr);
    }

    private int Compare(int a, int b)
    {
        return a | b;
    }
}


public class Solution
{
    public static void Main()
    {
        var N = int.Parse(Console.ReadLine());
        var S = Console.ReadLine();
        var Q = int.Parse(Console.ReadLine());

        var queries = new Query[Q];
        for (int i = 0; i < Q; i++)
        {
            queries[i] = new Query(Console.ReadLine());
        }

        var segTree = new SegmentTree(S.Select(c => 1 << (c - 'a')).ToArray());

        var result = new List<int>();
        foreach (var q in queries)
        {
            if (q.QueryType == 1)
            {
                segTree.Update(q.i - 1, 1 << (q.c - 'a'));
            }
            else
            {
                var r = Bits.PopCount(segTree.GetKinds(q.l - 1, q.r));
                result.Add(r);
            }
        }

        Console.WriteLine(string.Join(Environment.NewLine, result));
    }
}