E - Sorting Queries Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500500

問題文

空の列 AA があります。クエリが QQ 個与えられるので、与えられた順番に処理してください。
クエリは次の 33 種類のいずれかです。

  • 1 x : AA の最後尾に xx を追加する。
  • 2 : AA の最初の要素を出力する。その後、その要素を削除する。このクエリが与えられるとき、AA は空でないことが保証される。
  • 3 : AA を昇順にソートする。

制約

  • 1Q2×1051 \leq Q \leq 2 \times 10^5
  • 0x1090 \leq x \leq 10^9
  • クエリ 2 が与えられるとき、AA は空でない。
  • 入力は全て整数である。

入力

入力は以下の形式で標準入力から与えられる。

QQ
query1\mathrm{query} 1
query2\mathrm{query} 2
\vdots
queryQ\mathrm{query} Q

ii 番目のクエリ queryi\mathrm{query} i では、まずクエリの種類 cic_i1,2,31, 2, 3 のいずれか)が与えられる。 ci=1c_i = 1 の場合はさらに整数 xx が追加で与えられる。

すなわち、各クエリは以下に示す 33 つの形式のいずれかである。

11 xx
22
33

出力

ci=2c_i = 2 を満たすクエリの回数を qq として qq 行出力せよ。
jj (1jq)(1 \leq j \leq q) 行目では jj 番目のそのようなクエリに対する答えを出力せよ。


入力例 1Copy

Copy
8
1 4
1 3
1 2
1 1
3
2
1 0
2

出力例 1Copy

Copy
1
2

入力例 11 において、 ii 番目のクエリを処理した後の AA の状態を ii 行目に示すと以下のようになります。

  • (4)(4)
  • (4,3)(4, 3)
  • (4,3,2)(4, 3, 2)
  • (4,3,2,1)(4, 3, 2, 1)
  • (1,2,3,4)(1, 2, 3, 4)
  • (2,3,4)(2, 3, 4)
  • (2,3,4,0)(2, 3, 4, 0)
  • (3,4,0)(3, 4, 0)

入力例 2Copy

Copy
9
1 5
1 5
1 3
2
3
2
1 6
3
2

出力例 2Copy

Copy
5
3
5

入力例 22 において、 ii 番目のクエリを処理した後の AA の状態を ii 行目に示すと以下のようになります。

  • (5)(5)
  • (5,5)(5, 5)
  • (5,5,3)(5, 5, 3)
  • (5,3)(5, 3)
  • (3,5)(3, 5)
  • (5)(5)
  • (5,6)(5, 6)
  • (5,6)(5, 6)
  • (6)(6)

Score : 500500 points

Problem Statement

We have an empty sequence AA. You will be given QQ queries, which should be processed in the order they are given. Each query is of one of the three kinds below:

  • 1 x : Append xx to the end of AA.
  • 2 : Print the element at the beginning of AA. Then, delete that element. It is guaranteed that AA will not empty when this query is given.
  • 3 : Sort AA in ascending order.

Constraints

  • 1Q2×1051 \leq Q \leq 2 \times 10^5
  • 0x1090 \leq x \leq 10^9
  • AA will not be empty when a query 2 is given.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

QQ
query1\mathrm{query} 1
query2\mathrm{query} 2
\vdots
queryQ\mathrm{query} Q

The ii-th query, queryi\mathrm{query} i, begins with the kind of query cic_i (11, 22, or 33). If ci=1c_i = 1, the line additionally has an integer xx.

In other words, each query is in one of the three formats below.

11 xx
22
33

Output

Print qq lines, where qq is the number of queries with ci=2c_i = 2.
The jj-th line (1jq)(1 \leq j \leq q) should contain the response for the jj-th such query.


Sample Input 1Copy

Copy
8
1 4
1 3
1 2
1 1
3
2
1 0
2

Sample Output 1Copy

Copy
1
2

The ii-th line below shows the contents of AA after the ii-th query is processed in Sample Input 11.

  • (4)(4)
  • (4,3)(4, 3)
  • (4,3,2)(4, 3, 2)
  • (4,3,2,1)(4, 3, 2, 1)
  • (1,2,3,4)(1, 2, 3, 4)
  • (2,3,4)(2, 3, 4)
  • (2,3,4,0)(2, 3, 4, 0)
  • (3,4,0)(3, 4, 0)

Sample Input 2Copy

Copy
9
1 5
1 5
1 3
2
3
2
1 6
3
2

Sample Output 2Copy

Copy
5
3
5

The ii-th line below shows the contents of AA after the ii-th query is processed in Sample Input 22.

  • (5)(5)
  • (5,5)(5, 5)
  • (5,5,3)(5, 5, 3)
  • (5,3)(5, 3)
  • (3,5)(3, 5)
  • (5)(5)
  • (5,6)(5, 6)
  • (5,6)(5, 6)
  • (6)(6)


2025-04-11 (Fri)
06:12:23 +00:00