#K75317. Digital Library Query System
Digital Library Query System
Digital Library Query System
In this problem, you are required to implement a digital library query system. The system supports two types of operations: adding a book to a genre, and querying all book IDs associated with a specific genre. When querying, you should output the list of book IDs in ascending order.
Formally, the system accepts operations in the following format:
• ADD id genre – add a book with the given integer id under the given genre (a string).
• QUERY genre – output all book ids associated with the genre in increasing order, separated by a space. If there are no books for the queried genre, output an empty line.
Input is given via standard input. The first line contains an integer N, the number of operations. Each of the following N lines contains one operation.
The solution must process all operations and print the results of the query operations to standard output.
The underlying logic is straightforward: maintain a mapping from genres to a set of unique book IDs, and then retrieve and sort the IDs whenever a QUERY operation is encountered.
The system guarantees that all operations follow the valid format.
inputFormat
The input starts with an integer N (1 ≤ N ≤ 10^5) representing the number of operations. Each of the next N lines is either an ADD or a QUERY operation.
• An ADD operation is formatted as: ADD book_id genre
, where book_id
is a positive integer and genre
is a string without spaces.
• A QUERY operation is formatted as: QUERY genre
. For each QUERY operation, you must output the sorted list of book IDs associated with that genre (if any) in ascending order, separated by a space. If there are no books for the queried genre, output an empty line.
outputFormat
For every QUERY operation in the input, print a line containing the list of corresponding book IDs in ascending order, separated by spaces. If a genre has no associated books, print an empty line.## sample
8
ADD 100 Science
ADD 200 Fiction
ADD 150 Science
QUERY Science
ADD 120 Literature
ADD 110 Fiction
QUERY Fiction
QUERY Literature
100 150
110 200
120
</p>