P11043 [Lanqiao Cup 2024 NOI Qualifier Java B] Distributed Queue
Description
Xiao Lan recently learned about a magical queue: a distributed queue. Simply put, a distributed queue contains $N$ nodes (numbered from $0$ to $N-1$, where node $0$ is the master node). There is only one master node, and the rest are follower nodes. Both the master and follower nodes each maintain their own queue. When adding an element to the distributed queue, it is always done by the master node (each time the element is appended to the end of the queue). Follower nodes are only responsible for synchronizing the queue from the master node. You may consider the queues in the master/follower nodes as an infinite one-dimensional array with indices $0,1,2,3,\dots$. Also, the synchronization order of elements in a follower node is consistent with the order in which elements are added in the master node.
Because replicas synchronize at different speeds, to ensure data consistency, after an element is added to the master node, it becomes visible only after it has been synchronized to all follower nodes.
Given the running state of a distributed queue, all operations are executed in the input order. You need to answer: at a certain moment, how many elements in the queue are visible.
Input Format
The first line contains an integer $N$, representing the number of nodes.
Then there are multiple lines. Each line contains an operation. There are three types of operations: add, sync, and query. Their input formats are as follows:
1. `add element`: This is an add operation, which adds the element $element$ to the queue.
2. `sync follower_id`: This is a synchronization operation. Follower node $follower\_id$ will synchronize the next element that it is missing from the master node.
3. `query`: A query operation, asking how many elements in the distributed queue are currently visible.
Output Format
For each query operation, output one line containing an integer representing the answer.
Explanation/Hint
**[Sample Explanation]**
When executing the first query, the queue contents are as follows:
- Node $0$: $[1,2]$
- Node $1$: $[]$
- Node $2$: $[]$
Both follower nodes have no elements, so the answer is $0$.
When executing the second query, the queue contents are as follows:
- Node $0$: $[1,2,1]$
- Node $1$: $[1,2]$
- Node $2$: $[1]$
Only the element at index $0$ has been synchronized by all nodes, so the answer is $1$.
When executing the third query, the queue contents are as follows:
- Node $0$: $[1,2,1]$
- Node $1$: $[1,2,1]$
- Node $2$: $[1]$
Only the element at index $0$ has been synchronized by all nodes, so the answer is $1$.
When executing the fourth query, the queue contents are as follows:
- Node $0$: $[1,2,1]$
- Node $1$: $[1,2,1]$
- Node $2$: $[1,2,1]$
All three elements have been synchronized by all nodes, so the answer is $3$.
**[Constraints and Notes for Test Cases]**
Let the number of operations in the input be $q$.
For $30\%$ of the test cases: $1\leq q \leq 100$.
For $100\%$ of the test cases: $1\leq q\leq 2000$, $1\leq N\leq 10$, $1\leq follower\_id