Problem p02763 - Generation 3

Orig Description

Score : 500 points
Problem StatementYou 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.
InputInput 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
OutputFor each query of type 2, print a line containing the answer.
Sample Input 17
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7
Sample Output 13
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.

Extracted Specification

An integer N (1 ≤ N ≤ 500,000), representing some quantity or size.
A string S of length N, consisting of lowercase English letters.
An integer Q (1 ≤ Q ≤ 20,000), representing the number of queries.
Q lines of queries, each being either:
1 i_q c_q (where 1 ≤ i_q ≤ N and c_q is a lowercase English letter)
2 l_q r_q (where 1 ≤ l_q ≤ r_q ≤ N)

### Example Input:

```
7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7
```

### Function Signature:
Write a function f(N, S, Q, queries) that takes in the input.
```python
def f(N: int, S: str, Q: int, queries: List[Tuple[int, Union[Tuple[int, str], Tuple[int, int]]]]):
    '''
    N: an integer
    S: a string
    Q: an integer
    queries: a list of tuples
    '''
```

Test Case Generator

Certainly! Below is the test case generator for the provided problem statement:

```python
import random
import string

def tcgen():
    # Generate random length for the string S within the constraints
    N = random.randint(1, 500000)
    
    # Generate the string S consisting of lowercase English letters
    S = ''.join(random.choices(string.ascii_lowercase, k=N))
    
    # Generate random number of queries within the constraints
    Q = random.randint(1, 20000)
    
    queries = []
    type2_present = False
    
    for _ in range(Q):
        query_type = random.choice([1, 2])
        
        if query_type == 1:
            # Type 1 query: change the i_q-th character of S to c_q
            i_q = random.randint(1, N)
            c_q = random.choice(string.ascii_lowercase)
            queries.append(f"1 {i_q} {c_q}")
        else:
            # Type 2 query: answer the number of different characters in the substring S[l_q:r_q]
            l_q = random.randint(1, N)
            r_q = random.randint(l_q, N)
            queries.append(f"2 {l_q} {r_q}")
            type2_present = True
    
    # Ensure there is at least one query of type 2
    if not type2_present:
        l_q = random.randint(1, N)
        r_q = random.randint(l_q, N)
        queries.append(f"2 {l_q} {r_q}")
    
    return N, S, Q, queries

# Example usage:
N, S, Q, queries = tcgen()
print(N)
print(S)
print(Q)
for query in queries:
    print(query)
```

### Explanation:
1. **N Generation**: Randomly generate the length of the string \( N \) within the constraint \( 1 \leq N \leq 500000 \).
2. **String S Generation**: Create a string \( S \) of length \( N \) consisting of random lowercase English letters.
3. **Q Generation**: Randomly generate the number of queries \( Q \) within the constraint \( 1 \leq Q \leq 20000 \).
4. **Queries Generation**: Generate \( Q \) queries:
   - **Type 1 Query**: Randomly choose an index \( i_q \) within the range \( 1 \leq i_q \leq N \) and a lowercase letter \( c_q \).
   - **Type 2 Query**: Randomly choose \( l_q \) and \( r_q \) such that \( 1 \leq l_q \leq r_q \leq N \).
5. **Guarantee Type 2 Query**: Ensure at least one query of type 2 is present in the generated queries.

The function returns \( N \), \( S \), \( Q \), and the list of queries.

Extract Arguments

def extract_arguments(fh):
    N = int(fh.readline().strip())
    S = fh.readline().strip()
    Q = int(fh.readline().strip())
    queries = []
    for _ in range(Q):
        query = fh.readline().strip().split()
        if query[0] == '1':
            queries.append((1, int(query[1]), query[2]))
        else:
            queries.append((2, int(query[1]), int(query[2])))
    return N, S, Q, queries