#K43607. String Operation Queries

    ID: 27347 Type: Default 1000ms 256MiB

String Operation Queries

String Operation Queries

You are given a string S of length N and Q queries to perform on it. The queries can be of two types:

  • Update Query: Given in the format 1 k c, update the character at position k (1-indexed) of the string to the character c. Formally, perform the update \( S[k] = c \) for \( 1 \leq k \leq N \).
  • Distinct Query: Given in the format 2 l r, count the number of distinct characters in the substring from position l to r (inclusive). In other words, compute \( f(l, r) = |\{ S[l], S[l+1], \dots, S[r] \}| \) for \( 1 \leq l \leq r \leq N \).

You need to process the queries in the given order and output the answers for all distinct queries (type 2) in the order they appear.

The input consists of multiple test cases. For each test case, you are given the initial string and a sequence of queries. Process each test case independently and aggregate all answers for type 2 queries.

inputFormat

The first line contains an integer T representing the number of test cases.

For each test case:

  1. The first line contains two space-separated integers N and Q — the length of the string and the number of queries.
  2. The second line contains the string S of length N.
  3. Each of the next Q lines contains a query in one of the following formats:
    • 1 k c (update query)
    • 2 l r (distinct query)

Note: All indices are 1-indexed.

outputFormat

For each distinct query (type 2), output an integer denoting the number of distinct characters found in the specified substring. The answers should be printed in the order the queries are processed, each on a new line.

## sample
4
7 3
abacaba
2 1 4
1 3 z
2 1 4
5 4
abcde
2 1 5
1 5 a
2 1 5
2 2 5
3 2
aaa
1 1 b
2 1 3
4 2
aabb
1 2 c
2 1 4
3

4 5 4 4 2 3

</p>