#P3871. [TJOI2010] 中位数

    ID: 2808 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2010各省省选平衡树O2优化排序天津

[TJOI2010] 中位数

Description

Given an integer sequence of NN elements, there are two operations:

  • 1 add a\texttt{1 add }\textit{a}: Append an integer aa to the end of the sequence, forming a sequence of length N+1N + 1.
  • 2 mid\texttt{2 mid}: Output the median of the current sequence.

The median is the number that lies in the middle after sorting the sequence in non-decreasing order. If the sequence length is even, it is the smaller of the two middle numbers.

Example 1: [1,2,13,14,15,16][1, 2, 13, 14, 15, 16] has median 1313.
Example 2: [1,3,5,7,10,11,17][1, 3, 5, 7, 10, 11, 17] has median 77.
Example 3: [1,1,1,2,3][1, 1, 1, 2, 3] has median 11.

Input Format

The first line contains the initial sequence length NN.
The second line contains NN integers, representing the sequence, separated by spaces.
The third line contains the number of operations MM, meaning you will perform MM operations.
Then follow MM lines, each in the format described above.

Output Format

For each mid\verb!mid! operation, output the value of the median.

6
1 2 13 14 15 16
5
add 5
add 3
mid
add 20
mid

5
13

Hint

Constraints

  • For 30%30\% of the testdata, 1N10,0001 \le N \le 10{,}000, 0M1,0000 \le M \le 1{,}000.
  • For 100%100\% of the testdata, 1N100,0001 \le N \le 100{,}000, 0M10,0000 \le M \le 10{,}000.

The absolute value of each integer in the sequence does not exceed 10910^9, and numbers in the sequence may repeat.

Translated by ChatGPT 5