#FT0000025. Bổ sung giá trị xuất hiện ít nhất (A += v)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Bổ sung giá trị xuất hiện ít nhất (A += v)
A += v
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
Initially, you have a sequence of integers from through , with positions numbered from . Imagine extending this sequence by performing append operations.
Before each append, consider all candidates , including values not yet appearing in the sequence. Choose a value having the smallest current frequency. If several values tie, choose the smallest numeric value among them, then append it. Frequencies are updated before the next append.
Each of the independent queries specifies a 1-based position in the sequence after all operations. Output the value at that position. Queries refer to the same extended sequence; they do not trigger additional appends.
Input
- Line 1:
n mgives initial length and number of candidate values. - Line 2:
A_1 A_2 ... A_ngives the initial values in index order. - Line 3:
q, the number of position queries. - Final lines: each contains one 1-based index
X, in query order.
Output
Print exactly lines. Line contains the element at position after appends.
Subtasks
Subtask 1 (20 points):
- .
- .
- for all .
- for all .
Subtask 2 (30 points):
- .
- .
- for all .
- for all .
Subtask 3 (50 points):
- .
- .
- for all .
- for all .
Examples
Input
3 3
1 1 2
8
1
2
3
4
5
6
7
8
Output
1
1
2
3
2
3
1
2
Explanation
Initially , and the sequence is [1,1,2]. The starting frequencies of values are respectively . Each append selects a value with minimum frequency, breaking ties toward the smaller value.
- Positions 1, 2, 3: these belong to the initial sequence and are .
- Position 4: value has frequency , lower than the others; append .
- Position 5: frequencies are now . Values and tie for minimum frequency, so append the smaller value .
- Position 6: frequencies become ; append .
- Position 7: all three values now occur twice; append the smallest, .
- Position 8: frequencies are ; choose , the smaller of the least-frequent values.
The first eight elements of the extended sequence are [1, 1, 2, 3, 2, 3, 1, 2]. Queries request positions 1 through 8, so each of these values is printed on its own line.