Problem p02457 - Generation 3

Orig Description

Set: Range Search
  For a set $S$ of integers, perform a sequence of the following operations. Note that each value in $S$ must be unique.
insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.
find($x$): Report the number of $x$ in $S$ (0 or 1).
delete($x$): Delete $x$ from $S$.
dump($L$, $R$): Print elements $x$ in $S$ such that $L \leq x \leq R$.
Input
  The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $x$
or
2 $x$
or
3 $L$ $R$
where the first digits 0, 1, 2 and 3 represent insert, find, delete and dump operations respectively.
Output
For each insert operation, print the number of elements in $S$.
For each find operation, print the number of specified elements in $S$.
For each dump operation, print the corresponding elements in ascending order. Print an element in a line.
Constraints
$1 \leq q \leq 200,000$
$0 \leq x \leq 1,000,000,000$
The total number of elements printed by dump operations does not exceed $1,000,000$
Sample Input 1
9
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 2 4
Sample Output 1
1
2
3
1
0
1
3
3
4

Extracted Specification

An integer \( q \) (1 ≤ \( q \) ≤ 200,000), representing the number of queries.
A list of \( q \) queries, each query being one of the following:
- A tuple (0, \( x \)) where \( x \) is an integer (0 ≤ \( x \) ≤ 1,000,000,000).
- A tuple (1, \( x \)) where \( x \) is an integer (0 ≤ \( x \) ≤ 1,000,000,000).
- A tuple (2, \( x \)) where \( x \) is an integer (0 ≤ \( x \) ≤ 1,000,000,000).
- A tuple (3, \( L \), \( R \)) where \( L \) and \( R \) are integers (0 ≤ \( L \), \( R \) ≤ 1,000,000,000).

### Example Input:

```
9
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 2 4
```

### Function Signature:
Write a function f(q, queries) that takes in the input. 
```python
def f(q: int, queries: List[Tuple[int, ...]]):
    '''
    q: an integer
    queries: a list of tuples, where each tuple represents a query
    '''
```

Test Case Generator

Certainly! Here is a test case generator for the "Set: Range Search" problem:

```python
import random

def tcgen():
    # Number of queries q
    q = random.randint(1, 200000)
    
    queries = []
    current_set = set()
    
    for _ in range(q):
        query_type = random.choice([0, 1, 2, 3])
        if query_type == 0:  # insert(x)
            x = random.randint(0, 1000000000)
            queries.append(f"0 {x}")
            current_set.add(x)
        elif query_type == 1:  # find(x)
            x = random.randint(0, 1000000000)
            queries.append(f"1 {x}")
        elif query_type == 2:  # delete(x)
            if current_set:
                x = random.choice(list(current_set))
                queries.append(f"2 {x}")
                current_set.remove(x)
            else:
                x = random.randint(0, 1000000000)
                queries.append(f"2 {x}")
        elif query_type == 3:  # dump(L, R)
            if current_set:
                elements = sorted(current_set)
                L = random.choice(elements)
                R = random.choice(elements)
                if L > R:
                    L, R = R, L
                queries.append(f"3 {L} {R}")
            else:
                L = random.randint(0, 1000000000)
                R = random.randint(0, 1000000000)
                if L > R:
                    L, R = R, L
                queries.append(f"3 {L} {R}")
    
    return q, queries

# Example of generating test cases
q, queries = tcgen()
print(q)
for query in queries:
    print(query)
```

### Explanation:

1. **Number of Queries**: We randomly generate the number of queries \( q \) between 1 and 200,000.
2. **Generating Queries**: We loop \( q \) times to generate each query:
   - **Insert (`0 x`)**: Randomly generate a value \( x \) between 0 and 1,000,000,000 and add it to the set.
   - **Find (`1 x`)**: Randomly generate a value \( x \) to search in the set.
   - **Delete (`2 x`)**: Randomly select a value \( x \) from the set if it's not empty, otherwise generate a random value.
   - **Dump (`3 L R`)**: If the set is not empty, randomly select two values \( L \) and \( R \) from the sorted set and make sure \( L \leq R \). Otherwise, generate two random values and ensure \( L \leq R \).
3. **Output the Queries**: The function returns the number of queries and the list of generated queries.

This generator should provide a good mixture of operations to thoroughly test the problem constraints and edge cases.

Extract Arguments

def extract_arguments(fh):
    q = int(fh.readline().strip())
    queries = []
    for _ in range(q):
        parts = list(map(int, fh.readline().strip().split()))
        queries.append(tuple(parts))
    return q, queries