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.
Medium6Dynamic programmingBinary searchPrefix sumArrayInterviewNo attempts yetTime limit2sMemory limit512 MBSeonggwan 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.
An integer put in at time t stays from time t onward, and an integer taken out at time t is gone from time t onward. So the number of copies of x at time t is the number of already processed type 1 operations on x whose time is at most t, minus the number of already processed type 2 operations on x whose time is at most t.
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.
The first line contains the number of queries N (1≤N≤100000).
Each of the next N lines contains one query as three positive integers ai, ti, xi (1≤ai≤3, 1≤ti,xi≤109). ai is the operation type, and ti and xi are the t and x from the statement.
For every query with ai=3, print its answer on its own line.