Problem B
Indexed Min Priority Queue
An indexed priority queue is one where your heap consists of indices; for instance, we can have a priority queue of objects, where what you get when you peek or poll is simply the index (id) of the object, and not the actual object itself. In order to know how the indices are sorted on the heap, each index is associated with a comparable key (priority). The great benefit of this data structure is that it allows us to remove an object or change its key in logarithmic time, if we know its index.
Input
On the first line, two integers
-
A (add) - two integers
and follows on the same line, indicating that the index is added to the priority queue with key . If the index is already assigned, print error on the next line and do not process any of the later queries; otherwise, do not print anything for this query. -
C (change) - two integers
and follows on the same line, indicating that the index changes key to . If the index is not already assigned, print error on the next line and do not process any of the later queries; otherwise, do not print anything for this query -
E (peek) - no further tokens follow on this line. If the priority queue is empty, print error on the next line and do not process any of the later queries; otherwise, print the index with the lowest key.
-
G (getKey) - an integer
follows on the same line, indicating that you should print the key assigned to index . If is not in the priority queue, print error on the next line and do not process any of the later queries; otherwise print the key given to in the priority queue. -
P (poll) - no further tokens follows on this line. If the priority queue is empty, print error on the next line of the output and do not process any of the later queries; otherwise, print the index with the lowest key, and then remove it from the priority queue.
-
R (remove) - an integer
follows on the same line, indicating that the index should be removed from the priority queue. If is not in the priority queue, print error on the next line and do not process any of the later queries; otherwise do not print anything for this query. -
S (size) - no further tokens follow on this line. Print on the next line the current size of the priority queue.
For every integer
Output
For each query, output a line according to the specifications given above.
Sample Input 1 | Sample Output 1 |
---|---|
3 11 A 0 300 A 1 100 A 2 200 S G 0 R 1 R 0 S G 2 R 0 S |
3 300 1 200 error |
Sample Input 2 | Sample Output 2 |
---|---|
3 8 A 0 300 A 1 100 A 2 200 E P P P P |
1 1 2 0 error |
Sample Input 3 | Sample Output 3 |
---|---|
3 15 A 0 300 A 1 100 A 2 200 C 0 50 E C 2 40 E C 0 400 C 1 500 C 2 600 C 0 700 C 1 800 P P P |
0 2 2 0 1 |