atcoder#ABC253C. [ABC253C] Max - Min Query

[ABC253C] Max - Min Query

Score : 300300 points

Problem Statement

We have a multiset of integers SS, which is initially empty.

Given QQ queries, process them in order. Each query is of one of the following types.

  • 1 x: Insert an xx into SS.
  • 2 x c: Remove an xx from SS mm times, where m=min(c,(m = \mathrm{min}(c,( the number of xx's contained in S))S)).
  • 3 : Print (( maximum value of S)(S)-( minimum value of S)S). It is guaranteed that SS is not empty when this query is given.

Constraints

  • 1Q2×1051 \leq Q \leq 2\times 10^5
  • 0x1090 \leq x \leq 10^9
  • 1cQ1 \leq c \leq Q
  • When a query of type 3 is given, SS is not empty.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

QQ

query1\mathrm{query}_1

\vdots

queryQ\mathrm{query}_Q

queryi\mathrm{query}_i, which denotes the ii-th query, is in one of the following formats:

11 xx

22 xx cc

33

Output

Print the answers for the queries of type 3 in the given order, separated by newlines.

8
1 3
1 2
3
1 2
1 7
3
2 2 3
3
1
5
4

The multiset SS transitions as follows.

  • 11-st query: insert 33 into SS. SS is now {3}\lbrace 3 \rbrace.
  • 22-nd query: insert 22 into SS. SS is now {2,3}\lbrace 2, 3 \rbrace.
  • 33-rd query: the maximum value of S={2,3}S = \lbrace 2, 3\rbrace is 33 and its minimum value is 22, so print 32=13-2=1.
  • 44-th query: insert 22 into SS. SS is now {2,2,3}\lbrace 2,2,3 \rbrace.
  • 55-th query: insert 77 into SS. SS is now {2,2,3,7}\lbrace 2, 2,3, 7\rbrace.
  • 66-th query: the maximum value of S={2,2,3,7}S = \lbrace 2,2,3, 7\rbrace is 77 and its minimum value is 22, so print 72=57-2=5.
  • 77-th query: since there are two 22's in SS and min(2,3)=2\mathrm{min(2,3)} = 2, remove 22 from SS twice. SS is now {3,7}\lbrace 3, 7\rbrace.
  • 88-th query: the maximum value of S={3,7}S = \lbrace 3, 7\rbrace is 77 and its minimum value is 33, so print 73=47-3=4.
4
1 10000
1 1000
2 100 3
1 10

If the given queries do not contain that of type 33, nothing should be printed.