Defile

Time limit3sMemory limit1024 MB

Summary
Maintain minion healths under insert and delete, and after each update report how many would die in repeated 1-damage waves.
Level

Medium7 of 10

Topics
Math, Number theory, Stack, Implementation
Solved
No attempts yet

Problem

Defile, a Hearthstone spell card that Junwon likes, has the following effect: "Deal 1 damage to all minions. If any die, cast this again."

In other words: represent the health of the minions currently on the board as a sequence of rr positive integers a1,…,ara_1, \dots, a_r. Playing Defile performs the following operation on the sequence aa.

  1. Decrease a1,…,ara_1, \dots, a_r all by 1.
  2. If any element aia_i equals 0, remove all such aia_i from the sequence and return to step 1. Increase the "number of dead minions" count by the number of removed elements. If no such aia_i exists, stop the process.

Currently there are no minions on the board. Hearthstone has many variables, but assume only the following two changes occur.

  1. A minion with health kk is added to the board.
  2. A minion with health kk is removed from the board.

After each change occurs, you must find the number of minions that die when Defile is played once.

All changes accumulate. Also note that you do not actually play Defile; you only suppose the situation in which the card is played.

Input

The first line gives QQ, the number of changes that occur on the board. (1≤Q≤1,000,0001 \le Q \le 1{,}000{,}000)

The next QQ lines each give two integers T,KT, K describing a change on the board. (T=1T = 1 or T=2T = 2, 1≤K≤1,000,0001 \le K \le 1{,}000{,}000)

When T=1T = 1, a minion with health KK is added to the board.

When T=2T = 2, a minion with health KK is removed from the board. It is guaranteed that at least one minion with health KK is on the board at this time.

Output

After each change given in the input occurs, find the number of minions that would die if Defile were played, and print one per line.

Because the input and output are very large, using fast input and output is recommended.

Examples1

  1. Example 1

    Input
    10
    1 1
    1 2
    1 3
    1 4
    2 2
    1 3
    1 2
    1 5
    2 1
    1 1
    
    Expected output
    1
    2
    3
    4
    1
    1
    5
    6
    0
    6