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: Time Limit Exceeded)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.function.BiFunction;
import java.util.Scanner;
import java.util.function.BinaryOperator;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author silviase
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(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, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            StringBuilder s = new StringBuilder(in.next());
            SegmentTree<Boolean>[] segTree = new SegmentTree[26];
            for (int i = 0; i < 26; i++) {
                segTree[i] = new SegmentTree(this::or, false);
                segTree[i].setSize(n);
            }


            for (int i = 0; i < s.length(); i++) {
                segTree[s.charAt(i) - 'a'].update(i, true);
            }


            int m = in.nextInt();
            for (int i = 0; i < m; i++) {
                // out.println(s);
                int t = in.nextInt();
                if (t == 1) {
                    int num = in.nextInt() - 1;
                    String hoge = in.next();
                    segTree[s.charAt(num) - 'a'].update(num, false);
                    s.setCharAt(num, hoge.charAt(0));
                    segTree[s.charAt(num) - 'a'].update(num, true);
                } else {
                    int cnt = 0;
                    int l = in.nextInt();
                    int r = in.nextInt();
                    for (int j = 0; j < 26; j++) {
                        // out.println(segTree[j].query(l - 1, r));
                        if (segTree[j].query(l - 1, r)) {
                            cnt++;
                        }
                    }
                    out.println(cnt);
                }

            }

        }

        private Boolean or(Object x, Object y) {
            return (boolean) x || (boolean) y;
        }

    }

    static class SegmentTree<T> {
        protected ArrayList<T> dat;
        protected BinaryOperator<T> bo;
        protected T unit;
        protected int size;

        public SegmentTree(BinaryOperator<T> binaryOperator, T unit) {
            bo = binaryOperator;
            this.unit = unit;
        }

        public void setSize(int size) {
            int sz = 1;
            while (sz < size) {
                sz <<= 1;
            }
            this.size = sz;
            dat = new ArrayList<>();
            for (int i = 0; i < 2 * this.size - 1; i++) {
                dat.add(unit);
            }
        }

        public void update(int i, T x) {
            i += size - 1;
            dat.set(i, x);
            while (i > 0) {
                i = (i - 1) / 2;
                dat.set(i, bo.apply(dat.get(i * 2 + 1), dat.get(i * 2 + 2)));
            }
        }

        public T query(int l, int r) {
            int ll = l + size - 1;
            int rr = r + size - 1;
            T res = unit;
            while (ll < rr) {
                if ((rr & 1) == 0) {
                    rr--;
                    res = bo.apply(res, dat.get(rr));
                }
                if ((ll & 1) == 0) {
                    res = bo.apply(res, dat.get(ll));
                    ll++;
                }
                ll = (ll - 1) >> 1;
                rr = (rr - 1) >> 1;
            }
            return res;
        }

    }
}