#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

char S[500001];

int bitcount(uint64_t x)
{
    x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555);
    x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333);
    x = (x & 0x0f0f0f0f0f0f0f0f) + (x >> 4 & 0x0f0f0f0f0f0f0f0f);
    x = (x & 0x00ff00ff00ff00ff) + (x >> 8 & 0x00ff00ff00ff00ff);
    x = (x & 0x0000ffff0000ffff) + (x >>16 & 0x0000ffff0000ffff);
    return (x & 0x00000000ffffffff) + (x >>32 & 0x00000000ffffffff);
}

#define segtree_element_type uint32_t
typedef struct {
    uint64_t size;
    uint64_t size_shift;
    segtree_element_type *a;
} Segment_Tree;

segtree_element_type segtree_e();
segtree_element_type segtree_oper(segtree_element_type x, segtree_element_type y);
void segtree_element_init(uint64_t N, segtree_element_type *a);

Segment_Tree *segtree_init(uint64_t N)
{
    Segment_Tree *st = malloc(sizeof(Segment_Tree));

    for(st->size=0x01, st->size_shift=0; st->size<N; st->size<<=1, st->size_shift++);
    st->a = malloc(sizeof(segtree_element_type) * (2 * st->size));

    segtree_element_init(N, st->a + st->size);
    for(uint64_t ni=N; ni<st->size; ni++){
        st->a[st->size + ni] = segtree_e();
    }

    for(uint64_t i=st->size-1; i>0; i--){
        st->a[i] = segtree_oper(st->a[i<<1], st->a[(i<<1) + 1]);
    }

    return st;
}

void segtree_term(Segment_Tree *st)
{
    free(st->a);
    free(st);
}

void segtree_update(Segment_Tree *st, uint64_t N, segtree_element_type x)
{
    st->a[N + st->size] = x;
    for(uint64_t i=(N+st->size)>>1; i!=0; i>>=1){
        st->a[i] = segtree_oper(st->a[i<<1], st->a[(i<<1) + 1]);
    }
}

segtree_element_type segtree_find_r(Segment_Tree *st, uint64_t p, uint64_t d, uint64_t l, uint64_t u)
{
    uint64_t ls = p << (st->size_shift - d);
    uint64_t us = (p + 1) << (st->size_shift - d);
    uint64_t ms = (ls + us)/2;

    if(l + st->size == ls && u + st->size == us){
        return st->a[p];
    } else if(l + st->size >= ms){
        return segtree_find_r(st, (p<<1)+1, d+1, l, u);
    } else if(ms >= u + st->size){
        return segtree_find_r(st, p<<1, d+1, l, u);
    } else {
        return segtree_oper(
                segtree_find_r(st, p<<1, d+1, l, ms - st->size),
                segtree_find_r(st, (p<<1)+1, d+1, ms - st->size, u));
    }
}

segtree_element_type segtree_find(Segment_Tree *st, uint64_t l, uint64_t u)
{
    return segtree_find_r(st, 1, 0, l, u);
}

segtree_element_type segtree_e()
{
    return 0x00ul;
}

void segtree_element_init(uint64_t N, segtree_element_type *a)
{
    for(int ni=0; ni<N; ni++){
        a[ni] = 0x01ul << (S[ni] - 'a');
    }
}

segtree_element_type segtree_oper(segtree_element_type x, segtree_element_type y)
{
    return x | y;
}

int main()
{
    int N, Q;
    scanf("%d%s%d", &N, S, &Q);

    Segment_Tree *st = segtree_init(N);

    for(int qi=0; qi<Q; qi++){
        int c;
        scanf("%d ", &c);
        if(c == 1){
            int i;
            char c;
            scanf("%d %c", &i, &c);
            segtree_update(st, i-1, 0x01ul << (c - 'a'));
        } else {
            int x, y;
            scanf("%d %d", &x, &y);
            uint32_t r = segtree_find(st, x-1, y);
            printf("%d\n", bitcount(r));
        }
    }

    segtree_term(st);

    return 0;
}


Measured memory_bytes: 4864