#K94332. Bookshelf Operations

    ID: 38618 Type: Default 1000ms 256MiB

Bookshelf Operations

Bookshelf Operations

You are given a bookshelf with an initial collection of books. Each book is described by three integers: an ID, a category, and the number of pages. You must then perform a series of operations on the bookshelf. There are three types of operations:

  • Type 1: Add a book to the bookshelf. The operation is given as: 1 id category pages.
  • Type 2: Remove a book from the bookshelf by its ID. The operation is given as: 2 id. If the book does not exist, ignore the operation.
  • Type 3: Query the total number of pages in books that belong to a given category. The operation is given as: 3 category.

For each type 3 operation, you must output the sum of pages of all books in that category.

The problem can be modeled using the following function signature in Python (for illustration):

def process_bookshelf(n: int, q: int, initial_books: List[Tuple[int, int, int]], operations: List[Tuple[int, ...]]) -> List[int]:
    """
    ...
    """
    pass

Note: In your solution, use the LaTeX format for any formulas if needed.

inputFormat

The first line contains two integers n and q, where n is the number of initial books and q is the number of operations.

The next n lines each contain three integers: id, category, and pages, describing a book.

The following q lines describe the operations. Each operation line begins with an integer that indicates the type of operation:

  • If the integer is 1, then it is followed by three integers: id category pages (add a book).
  • If the integer is 2, then it is followed by a single integer: id (remove a book).
  • If the integer is 3, then it is followed by a single integer: category (query total pages in that category).

Input is provided via standard input (stdin).

outputFormat

For each query operation (type 3), output a single line containing the total number of pages in all books of the queried category.

Output is printed to standard output (stdout).

## sample
5 6
1 2 350
2 3 500
3 2 200
4 1 150
5 3 100
3 2
1 6 2 300
3 2
2 1
3 2
3 3
550

850 500 600

</p>