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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Jaynil
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ESimpleStringQueries solver = new ESimpleStringQueries();
        solver.solve(1, in, out);
        out.close();
    }

    static class ESimpleStringQueries {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            StringBuilder x = new StringBuilder(in.next());
            SegmentTree st[] = new SegmentTree[26];
            for (int i = 0; i < 26; i++) {
                Node a[] = new Node[n];
                for (int j = 0; j < n; j++) {
                    if (x.charAt(j) == ('a' + i)) {
                        a[j] = new Node(1);
                    } else {
                        a[j] = new Node(0);
                    }
                }
                st[i] = new SegmentTree(a, new Node(0));
            }
            int q = in.nextInt();
            while (q-- > 0) {
                int t = in.nextInt();
                if (t == 1) {
                    int indi = in.nextInt() - 1;
                    Character c = in.next().charAt(0);
                    if (x.charAt(indi) != c) {
                        st[c - 'a'].updateValue(indi, new Node(1));
                        st[x.charAt(indi) - 'a'].updateValue(indi, new Node(-1));
                    }
                } else {
                    int l = in.nextInt() - 1;
                    int r = in.nextInt() - 1;
                    int ans = 0;
                    for (int i = 0; i < 26; i++) {
                        if (st[i].get(l, r).x > 0) {
                            ans++;
                        }
                    }
                    out.println(ans);
                }
            }
        }

    }

    static class SegmentTree {
        Node[] st;
        Node nu;
        int n;

        SegmentTree(Node arr[], Node nu) {
            n = arr.length;
            this.nu = nu;
            int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
            int max_size = 2 * (int) Math.pow(2, x) - 1;
            st = new Node[max_size]; // Memory allocation
            constructSTUtil(arr, 0, n - 1, 0);
        }

        Node value(Node x, Node y) {

            //Range Minimum
//        if(x.x < y.x){
//            return x;
//        }
//        else {
//            return y;
//        }

            //Summation
            return new Node(x.x + y.x);
        }

        int getMid(int s, int e) {
            return s + (e - s) / 2;
        }

        Node get(int qs, int qe) {
            int a = Math.min(qs, qe);
            int b = Math.max(qs, qe);
            qs = a;
            qe = b;
            if (qs < 0 || qe > n - 1 || qs > qe) {
                System.out.println("Invalid Input");
                return nu;
            }
            return getUtil(0, n - 1, qs, qe, 0);
        }

        Node getUtil(int ss, int se, int qs, int qe, int si) {
            // If segment of this node is a part of given range, then return
            // the sum of the segment
            if (qs <= ss && qe >= se) {
                return st[si];

            }

            // If segment of this node is outside the given range
            if (se < qs || ss > qe)
                return nu;
            // If a part of this segment overlaps with the given range
            int mid = getMid(ss, se);
            return value(getUtil(ss, mid, qs, qe, 2 * si + 1), getUtil(mid + 1, se, qs, qe, 2 * si + 2));

        }

        void updateValueUtil(int ss, int se, int i, Node diff, int si) {
            // Base Case: If the input index lies outside the range of
            // this segment
            if (i < ss || i > se)
                return;

            // If the input index is in range of this node, then update the
            // value of the node and its children
            st[si] = value(diff, st[si]);
            if (se != ss) {
                int mid = getMid(ss, se);
                updateValueUtil(ss, mid, i, diff, 2 * si + 1);
                updateValueUtil(mid + 1, se, i, diff, 2 * si + 2);
            }
        }

        void updateValue(int i, Node new_val) {
            // Check for erroneous input index
            if (i < 0 || i > n - 1) {
                System.out.println("Invalid Input");
                return;
            }

            // Update the values of nodes in segment tree
            updateValueUtil(0, n - 1, i, new_val, 0);
        }

        Node constructSTUtil(Node arr[], int ss, int se, int si) {
            // If there is one element in array, store it in current node of
            // segment tree and return
            if (ss == se) {
                st[si] = arr[ss];
                return arr[ss];
            }
            int mid = getMid(ss, se);
            st[si] = value(constructSTUtil(arr, mid + 1, se, si * 2 + 2), constructSTUtil(arr, ss, mid, si * 2 + 1));
            return st[si];
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }

    static class Node {
        int x;

        Node(int x) {
            this.x = x;
        }

    }
}