Under construction

Still being built. Things may change

Organizing Bags in Mabinogi

Time limit4sMemory limit1024 MB

Summary
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 R×CR\times C bag in Mabinogi is a rectangle made of RR rows and CC columns, which is RCRC grid cells in total. An item of size x×yx\times y occupies a contiguous block of xx rows and yy 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 QQ 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 R×CR\times C 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 x×yx\times y with value ww. The values of all items ever acquired are distinct.
  • 3 e: Jeonghu discards the bag or item that was acquired in record ee.

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 QQ.

The next QQ 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

  • 1≤Q≤30 0001 \leq Q \leq 30\,000
  • 1≤R,C≤60 0001 \leq R, C \leq 60\,000
  • 1≤w≤1091 \leq w \leq 10^9
  • The values of all items ever acquired are distinct.
  • 1≤x,y≤21 \leq x, y \leq 2
  • 1≤e≤Q1 \leq e \leq Q
  • Record 3 e is always valid. That is, record ee is a earlier record that bought a bag or acquired an item, and the values of ee are distinct.
  • All given numbers are integers.

Examples2

  1. Example 1

    Input
    8
    1 3 3
    2 2 1 4
    2 2 1 3
    2 1 2 5
    2 1 2 1
    2 1 1 2
    2 1 1 10
    3 6
    
    Expected output
    0
    4
    7
    12
    13
    15
    24
    23
    
  2. Example 2

    Input
    15
    1 5 3
    2 2 1 288
    2 2 2 646
    3 2
    2 2 1 421
    2 1 1 117
    2 1 1 136
    2 2 1 289
    2 1 1 9
    2 2 2 40
    2 2 1 318
    2 1 1 225
    1 10 3
    2 2 2 701
    3 5
    
    Expected output
    0
    288
    934
    646
    1067
    1184
    1320
    1609
    1618
    1658
    1936
    2161
    2201
    2902
    2481