Buggy python submission (verdict: Time Limit Exceeded):
class BIT:
    def __init__(self, N):
        self.tree = [0] * (N + 1)  # 1-indexed
        self.N = N

    def add(self, i, x):
        """tree[i]と関連個所にxを加える"""
        tree = self.tree
        N = self.N
        while i <= N:
            tree[i] += x
            i += i & (-i)

    def sum(self, i):
        tree = self.tree
        s = 0
        while i:
            s += tree[i]
            i -= i & (-i)
        return s


def main():
    from string import ascii_lowercase
    import sys
    input = sys.stdin.readline

    N = int(input())
    s = input().rstrip()
    Q = int(input())

    conv = {c: i for i, c in enumerate(ascii_lowercase)}
    s = [conv[c] for c in s]

    bs = [BIT(N) for _ in range(26)]
    for i, c in enumerate(s, 1):
        bs[c].add(i, 1)

    ans = []
    for _ in range(Q):
        it = iter(input().split())
        if next(it) == '1':
            # change
            i = int(next(it))  # 1-indexed
            c = conv[next(it)]

            p = s[i - 1]
            bs[p].add(i, -1)

            s[i - 1] = c
            bs[c].add(i, 1)

        else:
            # count
            l, r = map(int, it)
            cnt = 0
            for b in bs:
                cnt += int(b.sum(r) - b.sum(l - 1) > 0)
            ans.append(cnt)
    print(*ans, sep='\n')


if __name__ == '__main__':
    main()


Fixed submission:
class BIT:
    def __init__(self, n):
        self.n = n
        self.data = [0] * (n + 1)
        self.el = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.data[i]
            i -= i & -i
        return s

    def add(self, i, x):
        # assert i > 0
        self.el[i] += x
        while i <= self.n:
            self.data[i] += x
            i += i & -i

    def get(self, i, j=None):
        if j is None:
            return self.el[i]
        return self.sum(j) - self.sum(i)


def main():
    import sys

    input = sys.stdin.readline

    n = int(input())  # 5 * 10 ** 5
    s = input().rstrip()

    base = ord('a')
    *s, = (ord(c) - base for c in s)

    q = int(input())  # 2 * 10 ** 4

    bits = [BIT(n + 1) for _ in range(26)]
    for i, x in enumerate(s, 1):
        bits[x].add(i, 1)

    ret = []
    for _ in range(q):
        query = iter(input().split())
        if next(query) == '1':
            # 変更
            pos = int(next(query))

            bf = s[pos - 1]
            char = ord(next(query)) - base

            bits[bf].add(pos, -1)
            s[pos - 1] = char

            bits[char].add(pos, 1)

        else:
            # 返答
            l = int(next(query))
            r = int(next(query))

            tt = 0
            for x in range(26):
                if bits[x].get(l - 1, r) > 0:
                    tt += 1
            ret.append(tt)

    print(*ret, sep='\n')


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Buggy python submission (verdict: Time Limit Exceeded):
# https://tjkendev.github.io/procon-library/python/range_query/bit.html
# Binary Indexed Tree (Fenwick Tree)
class BIT:
    def __init__(self, n):
        self.n = n
        self.data = [0] * (n + 1)
        self.el = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.data[i]
            i -= i & -i
        return s

    def add(self, i, x):
        # assert i > 0
        self.el[i] += x
        while i <= self.n:
            self.data[i] += x
            i += i & -i

    def get(self, i, j=None):
        if j is None:
            return self.el[i]
        return self.sum(j) - self.sum(i)


def main():
    from string import ascii_lowercase
    import sys
    input = sys.stdin.readline

    n = int(input())
    s = [ascii_lowercase.index(c) for c in input().rstrip()]
    q = int(input())

    bits = [BIT(n) for _ in range(26)]
    for base1, ord in enumerate(s, 1):
        bits[ord].add(base1, +1)

    ret = []
    for _ in range(q):
        it = iter(input().split())
        if next(it) == '1':
            # 変更
            base1 = int(next(it))

            prev_ord = s[base1 - 1]
            next_ord = ascii_lowercase.index(next(it))

            bits[prev_ord].add(base1, -1)
            bits[next_ord].add(base1, +1)

            s[base1 - 1] = next_ord

        else:
            # 返答
            start, end = map(int, it)
            res = sum(
                bits[ord].get(start - 1, end) > 0
                for ord in range(26)
            )

            ret.append(res)

    print(*ret, sep='\n')


if __name__ == '__main__':
    main()


Fixed submission:
class BIT:
    def __init__(self, n):
        self.n = n
        self.data = [0] * (n + 1)
        self.el = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.data[i]
            i -= i & -i
        return s

    def add(self, i, x):
        # assert i > 0
        self.el[i] += x
        while i <= self.n:
            self.data[i] += x
            i += i & -i

    def get(self, i, j=None):
        if j is None:
            return self.el[i]
        return self.sum(j) - self.sum(i)


def main():
    import sys

    input = sys.stdin.readline

    n = int(input())  # 5 * 10 ** 5
    s = input().rstrip()

    base = ord('a')
    *s, = (ord(c) - base for c in s)

    q = int(input())  # 2 * 10 ** 4

    bits = [BIT(n + 1) for _ in range(26)]
    for i, x in enumerate(s, 1):
        bits[x].add(i, 1)

    ret = []
    for _ in range(q):
        query = iter(input().split())
        if next(query) == '1':
            # 変更
            pos = int(next(query))

            bf = s[pos - 1]
            char = ord(next(query)) - base

            bits[bf].add(pos, -1)
            s[pos - 1] = char

            bits[char].add(pos, 1)

        else:
            # 返答
            l = int(next(query))
            r = int(next(query))

            tt = 0
            for x in range(26):
                if bits[x].get(l - 1, r) > 0:
                    tt += 1
            ret.append(tt)

    print(*ret, sep='\n')


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())