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 (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>

#define KI_MAX (1 << 19) /* 524288 */

int ki[KI_MAX * 2 - 1];

void ki_set(int idx, int value) {
	int pos = (KI_MAX - 1) + idx;
	ki[pos] = value;
	do {
		pos = (pos - 1) / 2;
		ki[pos] = ki[pos * 2 + 1] | ki[pos * 2 + 2];
	} while (pos > 0);
}

int ki_get_i(int idx, int qmin, int qmax, int smin, int smax) {
	if (qmax <= smin || smax <= qmin) { /* 完全に外れている */
		return 0;
	} else if (qmin <= smin && smax <= qmax) { /* セグメントがクエリに完全に含まれる */
		return ki[idx];
	} else {
		int smid = smin + (smax - smin) / 2;
		int left = ki_get_i(idx * 2 + 1, qmin, qmax, smin, smid);
		int right = ki_get_i(idx * 2 + 2, qmin, qmax, smid, smax);
		return left | right;
	}
}

int ki_get(int min, int max) {
	return min < max ? ki_get_i(0, min, max, 0, KI_MAX) : 0;
}

struct queri {
	int type;
	union {
		struct {
			int i;
			char c;
		} type1;
		struct {
			int l, r;
		} type2;
	} data;
};

int N;
char S[555555];
int Q;
struct queri Query[22222];

int main(void) {
	int i;
	if (scanf("%d", &N) != 1) return 1;
	if (scanf("%555554s", S) != 1) return 1;
	if (scanf("%d", &Q) != 1) return 1;
	for (i = 0; i < Q; i++) {
		if (scanf("%d", &Query[i].type) != 1) return 1;
		if (Query[i].type == 1) {
			char c[4];
			if (scanf("%d%3s", &Query[i].data.type1.i, c) != 2) return 1;
			Query[i].data.type1.c = c[0];
		} else if (Query[i].type == 2) {
			if (scanf("%d%d", &Query[i].data.type2.l, &Query[i].data.type2.r) != 2) return 1;
		} else {
			puts("unknown query type!");
			return 2;
		}
	}

	for (i = 0; i < N; i++) {
		ki_set(i, 1 << (S[i] - 'a'));
	}
	for (i = 0; i < Q; i++) {
		if (Query[i].type == 1) {
			ki_set(Query[i].data.type1.i - 1, 1 << (Query[i].data.type1.c - 'a'));
		} else if (Query[i].type == 2) {
			int tamurayukari = ki_get(Query[i].data.type2.l - 1, Query[i].data.type2.r);
			int kaziyuuki = 0;
			while (tamurayukari > 0) {
				if (tamurayukari & 1) kaziyuuki++;
				tamurayukari >>= 1;
			}
			printf("%d\n", kaziyuuki);
		}
	}
	return 0;
}