Problem 901
Design Hit Counter
Medium · 20 pts · Design, Queue, Binary Search
Problem
Count website hits over the last five minutes. 'hit t' records a hit at second t. 'getHits t' prints the number of hits in the 300 seconds ending at t, that is, with timestamps in (t - 300, t]. Timestamps of all operations never decrease, and several hits can share a timestamp. Keep the hit timestamps in a queue and drop those that are too old before answering.
Input
The first line contains Q. Each of the next Q lines contains one operation. There is at least one getHits.
Output
Print the result of every getHits, one per line.
Constraints
1 <= Q <= 10000; 1 <= t <= 2 × 10^9; timestamps are non-decreasing
Example input
6 hit 1 hit 2 hit 3 getHits 4 hit 300 getHits 300
Example output
3 4
Explanation
After hits at 1, 2 and 3, getHits 4 counts all 3. A hit at 300 makes getHits 300 print 4. A query at 301 would no longer count the hit at second 1, because (301 - 300, 301] excludes 1.