Problem p02763 - Generation 1

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 ≤ 500000), representing some quantity or size.
A string S of length N, consisting of lowercase English letters.
An integer Q (1 ≤ Q ≤ 20000), representing some number of queries.
A list of Q queries, where each query is one of the following two types:
1. "1 i_q c_q" - where i_q (1 ≤ i_q ≤ N) is an integer and c_q is a lowercase English letter.
2. "2 l_q r_q" - where l_q and r_q (1 ≤ l_q ≤ r_q ≤ N) are integers.

### 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.
def f(N: int, S: str, Q: int, queries: List[str]): 
    ''' 
    N: an integer
    S: a string
    Q: an integer
    queries: a list of strings
    '''

Test Case Generator

Certainly! Here’s a test case generator for the given problem statement:

```python
import random
import string

def generate_test_case():
    # Generate random values for N and Q within their respective constraints.
    N = random.randint(1, 500000)
    Q = random.randint(1, 20000)
    
    # Generate a random string S of length N consisting of lowercase English letters.
    S = ''.join(random.choices(string.ascii_lowercase, k=N))
    
    # Initialize a list to store the queries
    queries = []
    
    # Ensure at least one query of type 2
    type2_query = f"2 {random.randint(1, N)} {random.randint(1, N)}"
    
    # Correct the order of l_q and r_q if needed
    l_q, r_q = map(int, type2_query.split()[1:])
    if l_q > r_q:
        l_q, r_q = r_q, l_q
        type2_query = f"2 {l_q} {r_q}"
    
    queries.append(type2_query)
    
    # Generate the remaining Q-1 queries
    for _ in range(Q - 1):
        query_type = random.choice([1, 2])
        
        if query_type == 1:
            i_q = random.randint(1, N)
            c_q = random.choice(string.ascii_lowercase)
            queries.append(f"1 {i_q} {c_q}")
        else:
            l_q = random.randint(1, N)
            r_q = random.randint(1, N)
            if l_q > r_q:
                l_q, r_q = r_q, l_q
            queries.append(f"2 {l_q} {r_q}")
    
    return N, S, Q, queries


# Example of generating a test case
if __name__ == "__main__":
    N, S, Q, queries = generate_test_case()
    
    print(N)
    print(S)
    print(Q)
    for query in queries:
        print(query)
```

### Explanation:

1. **N**: A random integer between 1 and 500,000.
2. **S**: A random string of lowercase letters of length N.
3. **Q**: A random integer between 1 and 20,000.
4. **Queries**: A list of Q queries.
    - **Type 1 Query**: Generates a query of the form `1 i_q c_q` where `1 <= i_q <= N` and `c_q` is a random lowercase letter.
    - **Type 2 Query**: Generates a query of the form `2 l_q r_q` where `1 <= l_q <= r_q <= N`. Ensures `l_q` is not greater than `r_q`.

The generator ensures there is at least one query of type 2 by creating it first, and then randomly generating the remaining queries. It also ensures that the ranges in type 2 queries are valid.

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