Organizing Bags in Mabinogi
Time limit4sMemory limit1024 MB
After each record, find the maximum total value of non-overlapping items (sizes 1x1 to 2x2, no rotation) that fit in the current bag.
- Level
Hard9 of 10
- Topics
- Segment tree, Divide and conquer, Dynamic programming
- Solved
- No attempts yet
Problem
In Mabinogi, the area of items you can carry in a bag is limited, so Jeonghu has run out of bag space. A bag in Mabinogi is a rectangle made of rows and columns, which is grid cells in total. An item of size occupies a contiguous block of rows and columns in the bag. Items cannot be rotated, different items cannot overlap, and no item can extend past the bag boundary. Jeonghu wants to organize a rectangular bag so that the total value of the items inside is maximized.
At first, Jeonghu has no items and no bags.
Over records, Jeonghu acquires or discards items, and buys or discards bags. Each record is given in one of the following formats.
1 R C: Jeonghu buys a new bag. This bag does not need to be placed inside another bag, and the cost of buying a bag is not considered.2 x y w: Jeonghu acquires an item of size with value . The values of all items ever acquired are distinct.3 e: Jeonghu discards the bag or item that was acquired in record .
After each record, Jeonghu wants to know the maximum total value of items that can be packed into the bag. Each record is handled independently when packing items. Help Jeonghu.
Input
The first line contains the number of records .
The next lines each contain one record in the format described above, which records that Jeonghu acquired or discarded an item, or bought or discarded a bag.
Output
After each record, print on its own line the maximum total value of items that can be packed into the bag. If no item can be packed, print 0.
Constraints
- The values of all items ever acquired are distinct.
- Record
3 eis always valid. That is, record is a earlier record that bought a bag or acquired an item, and the values of are distinct. - All given numbers are integers.