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 Rust (1.15.1) (judge verdict: Accepted)

// macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut iter = s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; }
// macro_rules ! input_inner { ( $ iter : expr ) => { } ; ( $ iter : expr , ) => { } ; ( $ iter : expr , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , mut $ var : ident $ ( $ r : tt ) * ) => { let mut $ var = $ iter . next ( ) . unwrap ( ) . parse ::< usize > ( ) . unwrap ( ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident $ ( $ r : tt ) * ) => { let $ var = $ iter . next ( ) . unwrap ( ) . parse ::< usize > ( ) . unwrap ( ) ; input_inner ! { $ iter $ ( $ r ) * } } ; }
// macro_rules ! read_value { ( $ iter : expr , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ iter , $ t ) ) ,* ) } ; ( $ iter : expr , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ iter , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ iter : expr , chars ) => { read_value ! ( $ iter , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ iter : expr , usize1 ) => { read_value ! ( $ iter , usize ) - 1 } ; ( $ iter : expr , $ t : ty ) => { $ iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) } ; }
trait Monoid {
    type T: Clone + PartialEq;
    fn identity(&self) -> Self::T;
    fn binary_operate(&self, x: &Self::T, y: &Self::T) -> Self::T;
}
struct BinaryIndexedTree<M: Monoid> {
    bit: Vec<M::T>,
    monoid: M,
}
impl<M: Monoid> BinaryIndexedTree<M> {
    fn new(n: usize, monoid: M) -> BinaryIndexedTree<M> {
        let bit = vec![monoid.identity(); n + 1];
        BinaryIndexedTree {
            bit: bit,
            monoid: monoid,
        }
    }
    fn query(&self, k: usize) -> M::T {
        let mut res = self.monoid.identity();
        let mut k = k as isize + 1;
        while k > 0 {
            res = self.monoid.binary_operate(&res, &self.bit[k as usize]);
            k -= k & -k;
        }
        res
    }
    fn update(&mut self, k: usize, x: M::T) {
        let mut k = k as isize + 1;
        while k < self.bit.len() as isize {
            self.bit[k as usize] = self.monoid.binary_operate(&self.bit[k as usize], &x);
            k += k & -k;
        }
    }
}
struct AnyMonoid<T: Clone, F: Fn(&T, &T) -> T> {
    id: T,
    op: F,
}
impl<T: Clone, F: Fn(&T, &T) -> T> AnyMonoid<T, F> {
    fn new(id: T, op: F) -> AnyMonoid<T, F> {
        AnyMonoid { id: id, op: op }
    }
}
impl<T: Clone + PartialEq, F: Fn(&T, &T) -> T> Monoid for AnyMonoid<T, F> {
    type T = T;
    fn identity(&self) -> T {
        self.id.clone()
    }
    fn binary_operate(&self, x: &T, y: &T) -> T {
        (self.op)(x, y)
    }
}
fn main() {
    let ss = {
        use std::io::Read;
        let mut ss = String::new();
        std::io::stdin().read_to_string(&mut ss).unwrap();
        ss
    };
    let mut iter = ss.split_whitespace();
    // input_inner!{iter, $($r)*}
    let n = iter.next().unwrap().parse::<usize>().unwrap();
    let s = iter
        .next()
        .unwrap()
        .parse::<String>()
        .unwrap()
        .chars()
        .collect::<Vec<char>>();
    // input! { n, s: chars, q };
    let q = iter.next().unwrap().parse::<usize>().unwrap();
    let s = s
        .into_iter()
        .map(|x| (x as u8 - 'a' as u8) as usize)
        .collect::<Vec<_>>();
    let mut bits = vec![];
    for _i in 0..26 {
        bits.push(BinaryIndexedTree::new(
            n + 1,
            AnyMonoid::new(0isize, |&x, &y| x + y),
        ));
    }
    for i in 0..n {
        bits[s[i]].update(i + 1, 1);
    }
    for _i in 0..q {
        // input! {x};
        let x = iter.next().unwrap().parse::<usize>().unwrap();
        if x == 1 {
            let i = iter.next().unwrap().parse::<usize>().unwrap();
            let c = iter
                .next()
                .unwrap()
                .parse::<String>()
                .unwrap()
                .chars()
                .collect::<Vec<char>>()[0];
            // input! {i, c: char};
            for j in 0..26 {
                let m = bits[j].query(i) - bits[j].query(i - 1);
                bits[j].update(i, -m);
            }
            bits[(c as u8 - 'a' as u8) as usize].update(i, 1);
        } else {
            let mut res = 0;
            let l = iter.next().unwrap().parse::<usize>().unwrap();
            let r = iter.next().unwrap().parse::<usize>().unwrap();
            // input! {l,r};
            for j in 0..26 {
                let x = bits[j].query(r) as isize - bits[j].query(l - 1) as isize;
                if x > 0 {
                    res += 1;
                }
            }
            println!("{}", res);
        }
    }
}