#C8603. Inventory Operations Simulation
Inventory Operations Simulation
Inventory Operations Simulation
You are given an inventory of products. Initially, you have N products, each identified by a unique item number and a corresponding price. The inventory is maintained in sorted order according to the item numbers.
You need to process Q operations on this inventory. Each operation is one of the following types:
- add x y: Add a product with item number x and price y. If the item already exists, update its price.
- del x: Delete the product with item number x if it exists.
- search x: Search for the product with item number x and output its price if it exists; otherwise, output
-1
.
The operations should be processed in the order they are given. For each search operation, print the result to the standard output.
Mathematically, let \(I\) denote the inventory where each product is represented as \((x, y)\) with \(x\) as the item number and \(y\) as the price. The operations can be described as follows:
- Add: \(I = I \cup \{(x, y)\}\) (or update if \(x\) exists).
- Delete: Remove \(x\) from \(I\) if \(x \in I\).
- Search: Output \(y\) if \((x, y) \in I\), otherwise output \(-1\).
inputFormat
The input is read from stdin and has the following format:
N item_number_1 price_1 item_number_2 price_2 ... (N lines for the initial inventory) Q operation_1 operation_2 ... (Q lines, each describing an operation in one of the following formats: "add x y", "del x", or "search x")
Here, N is a non-negative integer representing the number of initial products and Q is the number of operations to perform.
outputFormat
The output is written to stdout. For each search
operation from the input, output an integer representing the price of the product if it exists; otherwise, output -1
. Each result should be printed on a new line.
5
101 50
102 40
103 60
104 70
105 80
8
add 106 90
add 100 30
del 102
del 108
search 104
search 102
search 106
search 100
70
-1
90
30
</p>