Problem: 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).

Submission attempts (in order):
def order(c):
    return 1 << (ord(c) - 97)

def count(n):
    return bin(n).count("1")

def merge(x, y):
    return x | y

def main():
    N = int(input())
    S = [x for x in input()]
    Q = int(input())
    L = 1
    while L < N:
        L *= 2
    L*=2 
    Seg = [0]*(L*2)

    def update(k, x):
        k += L 
        Seg[k] = x
        while k > 0:
            k = (k - 1) // 2
            Seg[k] = Seg[2*k+1]|Seg[2*k+2]
        Seg[0] = Seg[1]|Seg[2]

    def find(a, b, k, l, r):
        if r <= a or b <= l:
            return 0
        if a <= l and r <= b:
            
            return Seg[k]
        vl = find(a, b, 2*k+1, l, (l+r)//2)
        vr = find(a, b, 2*k+2, (l+r)//2, r)
        
        return merge(vl, vr)

    # read S, and update tree
    for i in range(N):
        update(i, order(S[i]))
        #print("Seg", *[count(x) for x in Seg])
    #print("Seg=", [count(x) for x in Seg])
    return
    for _ in range(Q):
        q, i, c = input().split()
        if q == '1':
            i = int(i)-1
            if S[i] == c:
                continue
            S[i] = c
            update(i, order(S[i]))
            #print("Seg=", [count(x) for x in Seg])
        else:
            l, r = int(i), int(c)
            #print((l,r))
            print(count(find(l, r+1, 0, 0, L)))
main()


import numpy as np

def popcount(n):
    return bin(n).count("1")
d = {c:(1 << (ord(c)-ord('a'))) for c in 'abcdefghijklmnopqrstuvwxyz'}

class SegmentTree:
    def __init__(self, A):
        N = len(A)
        n = (N-1).bit_length()
        self.N = 1 << n
        self.Seg = [0 for i in range(2*self.N)]
        for i in range(N):
            self.Seg[self.N+i-1] = d[A[i]]
        for i in range(0, self.N-1)[::-1]:
            self.Seg[i] = self.merge(self.Seg[i*2+1], self.Seg[i*2+2])

    def merge(self, x, y):
        return x | y

    def update(self, k, x):
        i = self.N+k-1
        self.Seg[i] = d[x]
        while i > 0:
            i = (i-1)//2
            self.Seg[i] = self.merge(self.Seg[i*2+1], self.Seg[i*2+2])

    def query(self, a, b):
        return self.query_(a, b, 0, 0, self.N)

    def query_(self, a, b, k, l, r):
        if r <= a or b <= l:
            return 0
        if a <= l and r <= b:
            return self.Seg[k]
        vl = self.query_(a, b, 2*k+1, l, (l+r)//2)
        vr = self.query_(a, b, 2*k+2, (l+r)//2, r)
        return self.merge(vl, vr)

import sys
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

def main():
    N = int(input())
    S = input()
    Q = int(input())
    Seg = SegmentTree(S)

    for _ in range(Q):
        q, i, c = input().split()
        if q == '1':
            i = int(i)-1
            Seg.update(i, c)
        else:
            l, r = int(i), int(c)
            print(popcount(Seg.query(l-1, r)))
main()


Verdicts: Wrong Answer

Accepted