#C6694. Warehouse Inventory Management

    ID: 50482 Type: Default 1000ms 256MiB

Warehouse Inventory Management

Warehouse Inventory Management

You are required to implement a warehouse inventory management system. The system maintains an inventory of items with their corresponding quantities and supports the following operations:

  • Add Item: "0 <itemID> <quantity>" — Add the specified quantity to the item with id itemID. If the item already exists, increase its quantity.
  • Query Item: "1 <itemID>" — Get the current quantity of the specified item. If the item does not exist, the quantity is 0.
  • Remove Item: "2 <itemID> <quantity>" — Remove the specified quantity from the item. The quantity will not go below 0. If after removal the quantity becomes 0, the item is removed from the inventory.
  • List Items: "3 <L> <R>" — List all items whose IDs fall in the inclusive range $[L, R]$ in lexicographical order. For each such item, output a line in the format: <itemID> <quantity>.

The input will be read from stdin and the output must be written to stdout. The first line of input contains an integer Q indicating the number of queries. Each of the following Q lines contains a query. Process the commands as described and print results for queries of type 1 and 3 in the order they appear.

Note: When listing items (query type 3), if no items fall in the interval, nothing should be printed for that query.

The solution must handle all operations efficiently and correctly update the inventory state.

inputFormat

The input consists of multiple lines. The first line contains a single integer Q representing the number of queries. Each of the next Q lines represents a query in one of the following formats:

  • 0 itemID quantity
  • 1 itemID
  • 2 itemID quantity
  • 3 L R

Each query is separated by a newline.

outputFormat

For each query of type 1 (query item) or type 3 (list items), output the result in the order the queries appear. For a query of type 1, output a single line containing the quantity of the item (or 0 if it does not exist). For a query of type 3, output one line for each item in the specified range in the format itemID quantity. If no items are in the range, output nothing for that query.

## sample
7
0 item1 10
0 item2 20
1 item1
2 item1 5
1 item1
3 item1 item3
3 itema itemz
10

5 item1 5 item2 20

</p>