Buy and Sell !
Practice
4.2 (4 votes)
Data structures
Easy
Fenwick tree
Problem
65% Success 3678 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

A good and robust is always difficult to design, however, its benefits are unlimited and is much better in the long term.

Today, you need to do just the same, i.e. you need to create a system to successfully process the incoming and outgoing stock of a retail outlet.

The outlet stocks and sells N types of items. Each item has a name and a price per unit associated with it. The name of each item on sale is guaranteed to be unique. You shall be given the names and prices for each of the N items.

Now, based on this list of items, You need to process 3 types of queries:

  1. X : Given a String X, add one unit of item X to the available stock. It is guaranteed that item X shall be among the list of items the shop trades in.

  2. X: If item X exists in the available stock, remove one occurrence of it from the available stock.

  3. \( ? \space Y\) : Now, you need to find the summation of available units of each item having price strictly greater than Y.

Initially the stock list is empty. For each query of the 3rd type, you need to print the answer on a new line.

Input Format :

The first line contains a single integers N denoting the number of items the shop trades in. Each of the next N lines contains a String \(X_{i}\) and an integer \(k_{i}\) where the \(i^{th}\) line denotes that the \(i^{th}\) item has name \(X_{i}\) and price \(k_{i}\). The next line contains a single integer Q. Each of the next Q lines contains either of the 3 types of queries. The first symbol of each query denotes the type of query. A symbol '+' denotes the first type of query, '-' denotes a query of the second type, '?' of the third respectively.

Output Format :

For each query of the 3rd type, print the answer on a new line

Constraints :

\( 1 \le N \le 10^5 \)

\( 1 \le Q \le 10^5 \)

\( 1 \le | X_{i} | \le 10 \)

\( 1 \le k_{i} \le 10^5 \)

\( 0 \le Y \le 10^5 \)

Note :

The name of each item on sale shall consist of lowercase English alphabets only

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
50 votes
Tags:
ApprovedData StructuresEasyOpenSegment TreesTrees
Points:30
8 votes
Tags:
Data StructuresEasyFenwick TreeFenwick tree
Points:30
8 votes
Tags:
ApprovedData StructuresEasyFenwick TreeFenwick tree