Orig Description
Score : 700 points
Problem StatementWe have a sandglass consisting of two bulbs, bulb A and bulb B. These bulbs contain some amount of sand.
When we put the sandglass, either bulb A or B lies on top of the other and becomes the upper bulb. The other bulb becomes the lower bulb.
The sand drops from the upper bulb to the lower bulb at a rate of 1 gram per second.
When the upper bulb no longer contains any sand, nothing happens.
Initially at time 0, bulb A is the upper bulb and contains a grams of sand; bulb B contains X-a grams of sand (for a total of X grams).
We will turn over the sandglass at time r_1,r_2,..,r_K. Assume that this is an instantaneous action and takes no time. Here, time t refer to the time t seconds after time 0.
You are given Q queries.
Each query is in the form of (t_i,a_i).
For each query, assume that a=a_i and find the amount of sand that would be contained in bulb A at time t_i.
Constraints
1≤X≤10^9
1≤K≤10^5
1≤r_1
Extracted Specification
An integer X (1 ≤ X ≤ 10^9), representing some quantity.
An integer K (1 ≤ K ≤ 10^5), representing the number of times an event occurs.
A list of K integers, each between 1 and 10^9, representing specific times when events occur.
An integer Q (1 ≤ Q ≤ 10^5), representing the number of queries.
A list of Q tuples, where each tuple contains:
- An integer t_i (0 ≤ t_i ≤ 10^9), representing a time.
- An integer a_i (0 ≤ a_i ≤ X), representing a quantity at time t_i.
### Example Input:
```
180
3
60 120 180
3
30 90
61 1
180 180
```
### Function Signature:
Write a function f(X, K, event_times, Q, queries) that takes in the input.
def f(X: int, K: int, event_times: List[int], Q: int, queries: List[Tuple[int, int]]):
'''
X: an integer
K: an integer
event_times: a list of integers
Q: an integer
queries: a list of tuples
'''
Test Case Generator
Certainly! Here is a test case generator for the given sandglass problem statement:
```python
import random
def tcgen():
X = random.randint(1, 10**9)
K = random.randint(1, 10**5)
# Generate unique random times for r_1, r_2, ..., r_K in increasing order
r = sorted(random.sample(range(1, 10**9 + 1), K))
Q = random.randint(1, 10**5)
queries = []
for _ in range(Q):
t_i = random.randint(0, 10**9)
a_i = random.randint(0, X)
queries.append((t_i, a_i))
return X, K, r, Q, queries
def main():
X, K, r, Q, queries = tcgen()
# Print the generated test case
print(X)
print(K)
print(" ".join(map(str, r)))
print(Q)
for t_i, a_i in queries:
print(f"{t_i} {a_i}")
if __name__ == "__main__":
main()
```
Explanation:
1. **X**: Total grams of sand, randomly chosen between 1 and \(10^9\).
2. **K**: Number of times the sandglass will be turned over, randomly chosen between 1 and \(10^5\).
3. **r**: A list of K distinct integers representing the times when the sandglass is turned. These are sorted to ensure they are in increasing order.
4. **Q**: Number of queries, randomly chosen between 1 and \(10^5\).
5. **Queries**: Each query consists of \(t_i\) (time) and \(a_i\) (initial sand in bulb A). The time \(t_i\) is chosen randomly between 0 and \(10^9\), and the sand amount \(a_i\) is chosen randomly between 0 and X.
This test case generator ensures that all constraints are respected and generates valid input for the problem.
Extract Arguments
def extract_arguments(fh):
X = int(fh.readline().strip())
K = int(fh.readline().strip())
r = list(map(int, fh.readline().strip().split()))
Q = int(fh.readline().strip())
queries = []
for _ in range(Q):
t, a = map(int, fh.readline().strip().split())
queries.append((t, a))
return X, K, r, Q, queries