Problem p02457 - Generation 2

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 some quantity or number of operations.
A list of operations, each operation is either one of:
- Tuple (0, x) where 0 ≤ x ≤ 1,000,000,000
- Tuple (1, x) where 0 ≤ x ≤ 1,000,000,000
- Tuple (2, x) where 0 ≤ x ≤ 1,000,000,000
- Tuple (3, L, R) where 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, operations) that takes in the input.
```python
def f(q: int, operations: List[Tuple[int, ...]]):
    ''' 
    q: an integer 
    operations: a list of tuples 
    '''
```

Test Case Generator

Sure, I can help you write a test case generator for the "Set: Range Search" problem. Below is the Python code for generating random test cases.

```python
import random

def tcgen():
    q = random.randint(1, 200000)  # Number of queries
    
    queries = []
    inserted_values = set()
    
    for _ in range(q):
        operation = random.randint(0, 3)
        
        if operation == 0:  # insert(x)
            x = random.randint(0, 1000000000)
            if x not in inserted_values:
                inserted_values.add(x)
            queries.append(f"0 {x}")
        
        elif operation == 1:  # find(x)
            if inserted_values:
                x = random.choice(list(inserted_values))
            else:
                x = random.randint(0, 1000000000)
            queries.append(f"1 {x}")
        
        elif operation == 2:  # delete(x)
            if inserted_values:
                x = random.choice(list(inserted_values))
                inserted_values.remove(x)
            else:
                x = random.randint(0, 1000000000)
            queries.append(f"2 {x}")
        
        elif operation == 3:  # dump(L, R)
            if inserted_values:
                L = random.choice(list(inserted_values))
                R = random.choice(list(inserted_values))
                if L > R:
                    L, R = R, L
            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 a test case
q, queries = tcgen()
print(q)
for query in queries:
    print(query)
```

This generator follows these rules:

1. The number of queries \( q \) is a random integer between 1 and 200,000.
2. Each query is randomly chosen from the four possible operations (`insert`, `find`, `delete`, `dump`).
3. For `insert` operations, a random integer \( x \) between 0 and 1,000,000,000 is generated and added to the set, ensuring uniqueness.
4. For `find` operations, if there are already inserted values, a random choice from the existing values is used; otherwise, a new random integer is generated.
5. For `delete` operations, if there are values in the set, a random value is chosen from the set and removed.
6. For `dump` operations, if there are values in the set, two random values from the set are chosen and ordered as \( L \) and \( R \); otherwise, two new random integers are generated and ordered.

This approach ensures that the generated test cases are valid and conform to the problem constraints.

Extract Arguments

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

# if __name__ == "__main__":
#     input_path = sys.argv[1]
#     with open(input_path, 'r') as fh: 
#     q, queries = extract_arguments(fh)
#     f(q, queries)