#C2188. Efficient Mode Finder Data Structure

    ID: 45476 Type: Default 1000ms 256MiB

Efficient Mode Finder Data Structure

Efficient Mode Finder Data Structure

You are required to implement a data structure that maintains a collection of integers and efficiently supports three operations:

  • add x: Add integer x to the collection.
  • remove x: Remove one occurrence of integer x from the collection, if it exists.
  • mode: Query the mode (i.e. the number that occurs most frequently) in the collection. In the event of a tie, return the smallest integer. If the collection is empty, output None.

Initially, the collection is empty. You will be given a series of operations. Process these operations sequentially and, for each mode operation, output the result on a new line.

Note: For tie-breaking, if multiple numbers have the highest frequency, choose the smallest one.

inputFormat

The first line contains a single integer n (1 ≤ n ≤ 105), representing the number of operations. Each of the following n lines contains one of the following operations:

  • add x — where x is an integer.
  • remove x — where x is an integer.
  • mode
Only the mode operations produce output.

outputFormat

For each mode operation, output the mode of the current collection on a new line. If the collection is empty, output None.## sample

6
add 1
add 2
add 2
mode
remove 1
mode
2

2

</p>