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, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}
macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

const M: i64 = 1_000_000_007;

/// Algebra - Monoid

trait Monoid: std::ops::Mul<Output=Self> + Clone + Copy {
    fn unit() -> Self;
}
impl Monoid for i64 { fn unit() -> Self { 0 }}
impl Monoid for f64 { fn unit() -> Self { 0.0 }}

trait Group: std::ops::Add<Output=Self> + std::ops::Sub<Output=Self> + std::ops::Neg<Output=Self>
    + std::iter::Sum + Clone + Copy {
    fn zero() -> Self;
}
impl Group for i32 { fn zero() -> Self { 0 }}
impl Group for i64 { fn zero() -> Self { 0 }}
impl Group for f32 { fn zero() -> Self { 0.0 }}
impl Group for f64 { fn zero() -> Self { 0.0 }}

trait Ring: Group + std::ops::Mul<Output=Self> + std::ops::Div<Output=Self> {
    fn one() -> Self;
}
impl Ring for i32 { fn one() -> Self { 1 }}
impl Ring for i64 { fn one() -> Self { 1 }}
impl Ring for f32 { fn one() -> Self { 1.0 }}
impl Ring for f64 { fn one() -> Self { 1.0 }}

/// Max/Min Int Monoid for RMQ

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
enum MaxInt { Minimal, Val(i64) }
impl std::ops::Mul for MaxInt {
    type Output = Self;
    fn mul(self, other: Self) -> Self {
        if self > other { self } else { other }
    }
}
impl Monoid for MaxInt {
    fn unit() -> Self { MaxInt::Minimal }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
enum MinInt { Val(i64), Maximal }
impl std::ops::Mul for MinInt {
    type Output = Self;
    fn mul(self, other: Self) -> Self {
        if self < other { self } else { other }
    }
}
impl Monoid for MinInt {
    fn unit() -> Self { MinInt::Maximal }
}

#[derive(Clone)]
struct SegmentTree<T> { size: usize, depth: usize, data: Vec<Vec<T>> }
impl<T> std::ops::Index<usize> for SegmentTree<T> {
    type Output = T;
    fn index(&self, i: usize) -> &Self::Output {
        &self.data[self.depth - 1][i]
    }
}
impl<T: Monoid> SegmentTree<T> {
    fn new(size: usize) -> SegmentTree<T> {
        let mut data = vec![];
        let mut i = 1;
        loop {
            data.push(vec![T::unit(); i]);
            if i >= size { break }
            i *= 2;
        }
        SegmentTree { size: size, depth: data.len(), data: data }
    }
    fn to_vec(self) -> Vec<T> {
        (self.data[self.depth - 1][0..self.size]).to_vec()
    }
    fn update(&mut self, idx: usize, t: T) {
        self.data[self.depth - 1][idx] = t;
        let mut i = idx;
        for d in (0..self.depth - 1).rev() {
            i /= 2;
            self.data[d][i] = self.data[d + 1][i * 2] * self.data[d + 1][i * 2 + 1];
        }
    }
    fn product(&self, range: std::ops::Range<usize>) -> T {
        if range.end == 0 || range.start + 1 > range.end {
            T::unit()
        } else if range.start + 1 == range.end {
            self[range.start]
        } else {
            let mut i = range.start;
            let mut d = self.depth - 1;
            let mut width = 1;
            while d > 0 {
                let left = (i / 2) * width * 2;
                let right = left + width * 2;
                if left == range.start && right <= range.end {
                    i /= 2;
                    d -= 1;
                    width *= 2;
                } else {
                    break
                }
            }
            let right = range.start + width;
            self.data[d][i] * self.product(right..range.end)
        }
    }
}
impl<T: std::fmt::Debug> SegmentTree<T> {
    fn debug(&self) {
        for i in 0..self.depth {
            trace!(&self.data[i]);
        }
    }
}

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

fn main() {
    let mut sc = Scanner::new();

    let n: usize = sc.cin();
    let mut s: Vec<usize> = sc.chars().iter().map(|&c| ord(c)).collect();

    use MaxInt::Val;

    let mut seqs: Vec<SegmentTree<MaxInt>> = vec![SegmentTree::new(n); 26];
    for i in 0..n {
        seqs[s[i]].update(i, Val(1));
    }

    let q: usize = sc.cin();
    for _ in 0..q {
        let ty: usize = sc.cin();
        if ty == 1 {
            let i = sc.cin::<usize>() - 1;
            let c: char = *sc.chars().iter().next().unwrap();
            let x = ord(c);
            if s[i] == x { continue }
            seqs[s[i]].update(i, Val(0));
            seqs[x].update(i, Val(1));
            s[i] = x;
        } else {
            let l: usize = sc.cin::<usize>() - 1;
            let r: usize = sc.cin::<usize>() - 1;
            let ans = (0..26).map(|i|
                seqs[i].product(l..r+1)
            ).map(|x| {
                if let Val(x) = x {
                    x
                } else {
                    0
                }
            }).sum::<i64>();
            put!(ans);
        }
    }

}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Self { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> { self.cin::<String>().chars().collect() }
    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> { (0..n).map(|_| self.cin()).collect() }
}