Distinct Characters Queries (Codeforces 1234D)
Tags: Others, data structures

You are given a string $$$s$$$ consisting of lowercase Latin letters and $$$q$$$ queries for this string.Recall that the substring $$$s[l; r]$$$ of the string $$$s$$$ is the string $$$s_l s_{l + 1} \dots s_r$$$. For example, the substrings of "codeforces" are "code", "force", "f", "for", but not "coder" and "top".There are two types of queries:   $$$1~ pos~ c$$$ ($$$1 \le pos \le |s|$$$, $$$c$$$ is lowercase Latin letter): replace $$$s_{pos}$$$ with $$$c$$$ (set $$$s_{pos} := c$$$);  $$$2~ l~ r$$$ ($$$1 \le l \le r \le |s|$$$): calculate the number of distinct characters in the substring $$$s[l; r]$$$. 

Input: The first line of the input contains one string $$$s$$$ consisting of no more than $$$10^5$$$ lowercase Latin letters.The second line of the input contains one integer $$$q$$$ ($$$1 \le q \le 10^5$$$) — the number of queries.The next $$$q$$$ lines contain queries, one per line. Each query is given in the format described in the problem statement. It is guaranteed that there is at least one query of the second type.

Output: For each query of the second type print the answer for it — the number of distinct characters in the required substring in this query.


Sample Inputs:
abacaba
5
2 1 4
1 4 b
1 5 b
2 4 6
2 1 7

dfcbbcfeeedbaea
15
1 6 e
1 4 b
2 6 14
1 7 b
1 12 c
2 6 8
2 1 6
1 7 c
1 2 f
1 10 a
2 7 9
1 10 a
1 14 b
1 1 f
2 1 11

Sample Outputs:
3
1
2

5
2
5
2
6

Solution (Python):
class SegmentTree:

    def __init__(self, size, identity, combiner):
        self.size = size
        self.identity = identity
        self.combiner = combiner
        self.tree = [identity] * (2 * size)

    def build(self, data):
        for i in range(self.size):
            self.tree[i + self.size] = data[i]
        for i in range(self.size - 1, 0, -1):
            self.tree[i] = self.combiner(self.tree[i * 2], self.tree[i * 2 + 1])

    def update(self, index, value):
        index += self.size
        self.tree[index] = value
        while index > 1:
            index //= 2
            self.tree[index] = self.combiner(self.tree[index * 2], self.tree[index * 2 + 1])

    def query(self, left, right):
        left += self.size
        right += self.size
        res_left = self.identity
        res_right = self.identity
        while left < right:
            if left % 2:
                res_left = self.combiner(res_left, self.tree[left])
                left += 1
            if right % 2:
                right -= 1
                res_right = self.combiner(self.tree[right], res_right)
            left //= 2
            right //= 2
        return self.combiner(res_left, res_right)

def main():
    s = input()
    tree_data = [1 << ord(c) - ord('a') for c in s]
    tree = SegmentTree(len(s), 0, lambda x, y: x | y)
    tree.build(tree_data)
    for _ in range(int(input())):
        query = input().split()
        t = int(query[0])
        if t == 1:
            (pos, c) = (int(query[1]) - 1, query[2])
            tree.update(pos, 1 << ord(c) - ord('a'))
        else:
            (l, r) = (int(query[1]) - 1, int(query[2]))
            result = bin(tree.query(l, r)).count('1')
            print(result)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline

class SegmentTree:

    def __init__(self, size):
        self.size = 1 << (size - 1).bit_length()
        self.tree = [0] * (2 * self.size)

    def set(self, idx, x):
        x_val = ord(x) - 97
        self.tree[idx + self.size - 1] = 1 << x_val

    def build(self):
        for i in range(self.size - 2, -1, -1):
            self.tree[i] = self.tree[i * 2 + 1] | self.tree[i * 2 + 2]

    def update(self, idx, x):
        idx += self.size - 1
        x_val = ord(x) - 97
        self.tree[idx] = 1 << x_val
        while idx > 0:
            idx = (idx - 1) // 2
            self.tree[idx] = self.tree[idx * 2 + 1] | self.tree[idx * 2 + 2]

    def query(self, l, r):
        (left, right) = (l + self.size, r + self.size)
        ret = 0
        while left < right:
            if right & 1:
                right -= 1
                ret |= self.tree[right - 1]
            if left & 1:
                ret |= self.tree[left - 1]
                left += 1
            left >>= 1
            right >>= 1
        return ret

def main():
    S = input().rstrip()
    Q = int(input())
    queries = tuple((input().split() for _ in range(Q)))
    seg = SegmentTree(len(S))
    for (i, s) in enumerate(S):
        seg.set(i, s)
    seg.build()
    ans = []
    for (q_type, var1, var2) in queries:
        if q_type == '1':
            (idx, char) = (int(var1) - 1, var2)
            seg.update(idx, char)
        else:
            (left, right) = (int(var1) - 1, int(var2))
            x = seg.query(left, right)
            cnt = bin(x)[2:].count('1')
            ans.append(cnt)
    print('\n'.join(map(str, ans)))
if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline

class SegmentTree:

    def __init__(self, size):
        self.size = 1 << (size - 1).bit_length()
        self.tree = [0] * (2 * self.size)

    def set(self, idx, x):
        x_val = ord(x) - 97
        self.tree[idx + self.size - 1] = 1 << x_val

    def build(self):
        for i in range(self.size - 2, -1, -1):
            self.tree[i] = self.tree[i * 2 + 1] | self.tree[i * 2 + 2]

    def update(self, idx, x):
        idx += self.size - 1
        x_val = ord(x) - 97
        self.tree[idx] = 1 << x_val
        while idx > 0:
            idx = (idx - 1) // 2
            self.tree[idx] = self.tree[idx * 2 + 1] | self.tree[idx * 2 + 2]

    def query(self, l, r):
        (left, right) = (l + self.size, r + self.size)
        ret = 0
        while left < right:
            if right & 1:
                right -= 1
                ret |= self.tree[right - 1]
            if left & 1:
                ret |= self.tree[left - 1]
                left += 1
            left >>= 1
            right >>= 1
        return ret

def main():
    S = list(input().rstrip())
    Q = int(input())
    queries = tuple((input().split() for _ in range(Q)))
    seg = SegmentTree(len(S))
    for (i, s) in enumerate(S):
        seg.set(i, s)
    seg.build()
    ans = []
    for (q_type, var1, var2) in queries:
        if q_type == '1':
            (idx, char) = (int(var1) - 1, var2)
            seg.update(idx, char)
        else:
            (left, right) = (int(var1) - 1, int(var2))
            x = seg.query(left, right)
            cnt = bin(x)[2:].count('1')
            ans.append(cnt)
    print('\n'.join(map(str, ans)))
if __name__ == '__main__':
    main()