#K69957. Warehouse Operations
Warehouse Operations
Warehouse Operations
You are given a simulation problem of a warehouse that stores boxes in a stack. Each box has an identifier k and a weight w. The warehouse supports the following operations:
- push k w: Add a box with identifier \(k\) and weight \(w\) to the top of the stack.
- pop: Remove the box at the top of the stack. If the stack is empty, do nothing.
- top: Print the identifier of the box on the top of the stack. If the stack is empty, print \(-1\).
- query w: Print the identifier of the topmost box (closest to the top of the stack) whose weight is exactly \(w\). If no such box exists, print \(-1\).
Your task is to implement these operations. The commands are given from standard input (stdin). You should output the results of the top
and query
operations to standard output (stdout), each on a separate line.
Note: All the arithmetic for weights and identifiers are done with integers. You may assume that the input operations are valid.
inputFormat
The first line contains an integer \(Q\) representing the number of operations. Each of the next \(Q\) lines contains one of the following commands:
push k w
pop
top
query w
For each top
and query
command, you are required to output the result.
outputFormat
For every top
or query
command in the input, output the resulting identifier in a new line. If the stack is empty or no box with the given weight exists for a query, output \(-1\).
8
push 1 100
push 2 200
top
pop
top
query 100
query 200
pop
2
1
1
-1
</p>