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)

#[allow(unused_imports)]
use std::char::from_digit;
#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::fs::File;
#[allow(unused_imports)]
use std::io::prelude::*;
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufReader, BufWriter, Stdin, Stdout, Write};
#[allow(unused_imports)]
use std::mem::swap;
#[allow(unused_imports)]
use std::str::FromStr;

type BoxResult<T> = Result<T, Box<std::error::Error>>;

fn main() {
    let mut scanner = Scanner::new(stdin());
    let n = scanner.read::<usize>().unwrap();
    let s = scanner
        .read_chars()
        .unwrap()
        .into_iter()
        .map(|c| encode(c))
        .collect::<Vec<_>>();
    let mut segtree = SegmentTree::new(n);
    for i in 0..n {
        segtree.update(i, s[i]);
    }
    let q = scanner.read::<usize>().unwrap();
    let mut writer = BufWriter::new(stdout());
    for _ in 0..q {
        let line = scanner.read_line().unwrap();
        let params = line.trim().split_whitespace().collect::<Vec<_>>();
        let t = params[0].parse::<usize>().unwrap();
        if t == 1 {
            let i = params[1].parse::<usize>().unwrap() - 1;
            let x = encode(params[2].chars().next().unwrap());
            segtree.update(i, x);
        } else {
            let l = params[1].parse::<usize>().unwrap() - 1;
            let r = params[2].parse::<usize>().unwrap() - 1;
            writeln!(&mut writer, "{}", segtree.query(l, r)).unwrap();
        }
    }
}

fn encode(c: char) -> usize {
    (c as u8 - 'a' as u8) as usize
}

struct SegmentTree {
    size: usize,
    content: Vec<usize>,
    values: Vec<Vec<usize>>,
}
impl SegmentTree {
    fn new(n: usize) -> SegmentTree {
        let mut size = 1;
        while size < n {
            size *= 2;
        }
        let len = 2 * size - 1;
        SegmentTree {
            size: size,
            content: vec![26; size],
            values: vec![vec![0; 26]; len],
        }
    }
    fn update(&mut self, i: usize, x: usize) {
        if self.content[i] == x {
            return;
        }
        let size = self.size;
        self.update_rec(0, 0, size - 1, i, x);
        self.content[i] = x;
    }
    fn update_rec(&mut self, index: usize, l: usize, r: usize, i: usize, x: usize) {
        if self.content[i] < 26 {
            self.values[index][self.content[i]] -= 1;   
        }
        self.values[index][x] += 1;
        if l == i && r == i {
            return;
        }
        let mid = (l + r) / 2;
        if i <= mid {
            self.update_rec(2 * index + 1, l, mid, i, x);
        } else {
            self.update_rec(2 * index + 2, mid + 1, r, i, x);
        }
    }
    fn query(&self, start: usize, end: usize) -> usize {
        let counts = self.query_rec(0, 0, self.size - 1, start, end);
        counts.into_iter().filter(|val| *val > 0).count()
    }
    fn query_rec(&self, index: usize, l: usize, r: usize, start: usize, end: usize) -> Vec<usize> {
        if l == start && r == end {
            return self.values[index].clone();
        }
        let mid = (l + r) / 2;
        if end <= mid {
            self.query_rec(2 * index + 1, l, mid, start, end)
        } else if start > mid {
            self.query_rec(2 * index + 2, mid + 1, r, start, end)
        } else {
            let mut left = self.query_rec(2 * index + 1, l, mid, start, mid);
            let right = self.query_rec(2 * index + 2, mid + 1, r, mid + 1, end);
            for i in 0..26 {
                left[i] += right[i];
            }
            left
        }
    }
}

#[allow(dead_code)]
struct Scanner<R> {
    reader: BufReader<R>,
    buffer: String,
}
impl<R> Scanner<R>
where
    R: Read,
{
    #[allow(dead_code)]
    fn new(inner: R) -> Scanner<R> {
        let reader = BufReader::new(inner);
        let buffer = String::new();
        Scanner {
            reader: reader,
            buffer: buffer,
        }
    }
    #[allow(dead_code)]
    fn with_capacity(capacity: usize, inner: R) -> Scanner<R> {
        let reader = BufReader::with_capacity(capacity, inner);
        let buffer = String::new();
        Scanner {
            reader: reader,
            buffer: buffer,
        }
    }
    #[allow(dead_code)]
    fn read_line(&mut self) -> BoxResult<String> {
        self.reader.read_line(&mut self.buffer)?;
        let cloned = self.buffer.trim().to_string();
        self.buffer.clear();
        Ok(cloned)
    }
    #[allow(dead_code)]
    fn read<T>(&mut self) -> BoxResult<T>
    where
        T: FromStr,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        self.reader.read_line(&mut self.buffer)?;
        let parsed = self.buffer.trim().parse()?;
        self.buffer.clear();
        Ok(parsed)
    }
    #[allow(dead_code)]
    fn read_two<T, U>(&mut self) -> BoxResult<(T, U)>
    where
        T: FromStr,
        T: Copy,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
        U: FromStr,
        U: Copy,
        U::Err: std::fmt::Debug,
        U::Err: std::error::Error,
        U::Err: 'static,
    {
        self.reader.read_line(&mut self.buffer)?;
        let (a, b) = {
            let params = self.buffer.trim().split_whitespace().collect::<Vec<_>>();
            let a = params[0].parse()?;
            let b = params[1].parse()?;
            (a, b)
        };
        self.buffer.clear();
        Ok((a, b))
    }
    #[allow(dead_code)]
    fn read_three<T>(&mut self) -> BoxResult<(T, T, T)>
    where
        T: FromStr,
        T: Copy,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        let nums = self.read_vector()?;
        Ok((nums[0], nums[1], nums[2]))
    }
    #[allow(dead_code)]
    fn read_vector<T>(&mut self) -> BoxResult<Vec<T>>
    where
        T: FromStr,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        self.reader.read_line(&mut self.buffer)?;
        let parsed_vec = self
            .buffer
            .trim()
            .split_whitespace()
            .map(|s| s.parse())
            .collect::<Result<Vec<_>, _>>()?;
        self.buffer.clear();
        Ok(parsed_vec)
    }
    #[allow(dead_code)]
    fn read_matrix<T>(&mut self, lines: usize) -> BoxResult<Vec<Vec<T>>>
    where
        T: FromStr,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        let mut parsed_matrix = vec![];
        for _ in 0..lines {
            self.reader.read_line(&mut self.buffer)?;
            parsed_matrix.push(
                self.buffer
                    .trim()
                    .split_whitespace()
                    .map(|s| s.parse())
                    .collect::<Result<Vec<_>, _>>()?,
            );
            self.buffer.clear();
        }
        Ok(parsed_matrix)
    }
    #[allow(dead_code)]
    fn read_chars(&mut self) -> BoxResult<Vec<char>> {
        self.reader.read_line(&mut self.buffer)?;
        let chars = self.buffer.trim().chars().collect::<Vec<_>>();
        self.buffer.clear();
        Ok(chars)
    }
}