Time Travel and Multiset
InterviewTime limit2sMemory limit512 MB
Process insert, delete, and count operations on a time-indexed multiset, where each value's count at time t depends on prior operations with time at most t.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Binary search, Prefix sum, Array
- Solved
- No attempts yet
Problem
Seonggwan built a time machine. This is an algorithm problem, so instead of doing something impressive with it he decided to apply time travel to a multiset.
A multiset with time travel supports three operations.
- Go to time and put one copy of the integer into the multiset.
- Go to time and take one copy of the integer out of the multiset. At least one copy of is guaranteed to be present at time .
- Print how many copies of the integer the multiset holds at time .
An integer put in at time stays from time onward, and an integer taken out at time is gone from time onward. So the number of copies of at time is the number of already processed type 1 operations on whose time is at most , minus the number of already processed type 2 operations on whose time is at most .
For example, suppose two copies of the integer 1 are put in at time 2 and one copy is taken out at time 5. The integer 1 then has two copies from time 2 to time 4 and one copy from time 5 onward. If one more copy is taken out at time 4, the integer 1 has two copies from time 2 to time 3, one copy at time 4, and none from time 5 onward. In that state an operation that takes the integer 1 out at time 5 never appears in the input, because no copy exists at that moment.
Operations are processed in the order given in the input. The answer to a type 3 operation reflects only the type 1 and type 2 operations that come before it.
Write a program that behaves as described above.
Input
The first line contains the number of queries ().
Each of the next lines contains one query as three positive integers , , (, ). is the operation type, and and are the and from the statement.
Output
For every query with , print its answer on its own line.