#P2776. [SDOI2007] 小组队列

[SDOI2007] 小组队列

Description

There are mm groups and nn elements. Each element belongs to exactly one group.

Support the following operations:

push x: Enqueue element x. If there is already an element from x’s group ahead in the queue, x will be placed immediately after the last element of its own group currently in the queue; otherwise, x will be placed at the end of the whole queue.

pop: Dequeue. Remove the front element of the queue and output it. This behaves like a normal queue, i.e., elements in front leave first.

Input Format

The first line contains two positive integers nn, mm, representing the number of elements and the number of groups. Elements and groups are numbered starting from 00.

The next line contains nn non-negative integers AiA_i, where AiA_i is the group that element ii belongs to.

The next line contains a positive integer TT, the number of operations.

The next TT lines each contain one operation.

Output Format

For each dequeue operation, output the dequeued element on a separate line.

4 2
0 0 1 1
6
push 2
push 0
push 3
pop
pop
pop
2
3
0

Hint

For 30%30\% of the testdata, 1n1001 \le n \le 100, 1m101 \le m \le 10, T50T \le 50.

For 100%100\% of the testdata, 1n1000001 \le n \le 100000, 1m3001 \le m \le 300, T100000T \le 100000. The input guarantees that all operations are valid.

Translated by ChatGPT 5