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 Kotlin (1.0.0) (judge verdict: Runtime Error)

fun main(args : Array<String>) {
    var N = input().toInt()
    var S= input()
    var SA=S.toCharArray().toMutableList()
    var segt= Segtree(N)
    segt.Injent(SA)
    var Q=input().toInt()

    for(i in 1..Q){
        var (x,y,z) =input().split(" ")
        if(x=="1"){
            segt.Change(y.toInt(), z.toCharArray()[0])
        }
        else{
            println(segt.Find_SetLength(y.toInt(), z.toInt()))
        }
    }
    

}


class UFtree(size : Int){
    var root = (1..size).map{-1}.toMutableList()
    var rank = (1..size).map{0}.toMutableList()
    fun find(X: Int):Int{
        if(root[X]<0){
            return X
        }
        var newX=find(root[X])
        root[X]=newX
        return newX
    }
    fun unite(X:Int,Y:Int){
        var XX=find(X)
        var YY=find(Y)

        if(XX==YY){
            return
        }

        if(rank[XX]>rank[YY]){
            root[XX]+=root[YY]
            root[YY]=XX
        }else{
            if(rank[YY]==rank[XX]){
                rank[YY]+=1
            }
            root[YY]+=root[XX]
            root[XX]=YY
        }
    }
}

class Segtree(item_size: Int){
    var tree_size =Get_Smallest_2Pow(item_size)
    var tree=(0..(1+item_size*2)).map{mutableSetOf<Char>()}.toMutableList()

    fun Injent(target : List<Char>){
        for(i in 0..(target.size-1)){
            tree[i+tree_size].add(target[i])
        }
        InitTree(1)
    }

    fun InitTree(index : Int):MutableSet<Char>{
        if(index>=tree_size){
            return tree[index]
        }
        var left =InitTree(index*2)
        var right=InitTree(index*2+1)
        tree[index]=left.union(right).toMutableSet()
        return tree[index]
    }
    fun Find_SetLength(st:Int,ed:Int): Int{
        return Search(1, 1, tree_size, st, ed).size
    }

    fun Search(selfId : Int,selfLeft : Int , selfRight : Int, targetLeft : Int,targetRight:Int) : MutableSet<Char>{
        if(selfLeft==targetLeft && selfRight==targetRight){
            return tree[selfId]
        }
        var endof_left = (selfLeft+selfRight)/2
        if(endof_left<targetLeft){
            return Search(selfId*2+1, endof_left + 1, selfRight, targetLeft, targetRight)
        }
        if(endof_left>=targetRight){
            return Search(selfId * 2, selfLeft, endof_left, targetLeft, targetRight)
        }
            var left=Search(selfId * 2, selfLeft, endof_left, targetLeft, endof_left)
            var right = Search(selfId * 2+1, endof_left+1 , selfRight, endof_left + 1, targetRight)
            return left.union(right).toMutableSet()
    }

    fun Delete(selfId : Int, target : Char){
        var pair = if(selfId % 2 ==0)
            {selfId+1}
        else
            {selfId-1}
        tree[selfId].remove( target)
        if(selfId==1){
            return
        }
        if(tree[pair].contains(target)){
            return
        }
        Delete(selfId/2, target)
    }

    fun Append(selfId : Int, target : Char){
        var pair = if(selfId % 2 ==0)
            {selfId+1}
        else
            {selfId-1}
        if(tree[selfId].contains(target)){
                return
        }
        tree[selfId].add( target)

        if(selfId==1){
            return
        }
        if(tree[pair].contains(target)){
            return
        }
        Append(selfId/2, target)
    }

    fun Change(position : Int, target : Char){
        var selfId=position+tree_size - 1
        var currentChar= tree[selfId].elementAt(0)
        if(currentChar==target){
            return
        }
        Delete(selfId, currentChar)
        Append(selfId, target)
    }

}

fun Get_Smallest_2Pow( N :Int):Int{
    if( (N and (N-1))==0){
        return N
    }
    var N2=1
    var N1=N
    while(N1>0){
        N1=N1.ushr(1)
        N2=N2.shl(1)
    }
    return N2

}


fun ReadInts():MutableList<Int>{
    return readLine()!!.split(" ").map{it.toInt()}.toMutableList()
}

fun ReadLongs():MutableList<Long>{
    return readLine()!!.split(" ").map{it.toLong()}.toMutableList()
}
fun ReadDoubles():MutableList<Double>{
    return readLine()!!.split(" ").map{it.toDouble()}.toMutableList()
}
fun input() : String{
    return readLine()!!

}