## CEOI '22 P1 - Abracadabra

View as PDF

Points: 20 (partial)
Time limit: 3.0s
Memory limit: 512M

Problem type

Tin Golubić, also known as Mr. Magic Man, is one of Varaždin's most talented young magicians. His specialty is card magic, and this task is an homage to some of the truly impressive feats of magic we have witnessed over the years.

Tin's trick featured in this task involves a deck of cards, where each card has a unique integer from to written on its face, and the total number of cards is even. Tin is going to perform what seems to be a series of riffle shuffles, and at any time an audience member may shout out a question: "What was the number written on the face of the -th card from the bottom after you performed shuffles?". Naturally, Tin will immediately respond with the correct answer.

The secret behind the trick involves a mix of Tin's incredible mental abilities and his card-handling dexterity. First, he is going to perfectly remember the starting state of the deck, meaning he knows exactly which card is in which position initially.

Then, he will use a slightly nuanced version of a standard riffle shuffle that goes unnoticed by the audience. Similar to your typical riffle, Tin will take the bottom half of the cards in his left hand, and the top half of the cards in his right hand, having them face down at all times, and proceed to drop them one by one to form the new deck on the table. Instead of arbitrarily dropping a bottom card from one of his hands, he will always drop the bottom card with the smaller number written on its face. Additionally, once he has dropped all cards from one of his hands, he drops all the remaining cards from his other hand as well. The dropped cards are then collected, and the shuffle is complete.

Starting from the initial deck, Tin will repeatedly perform his shuffle on the current deck, obtaining a new ordering of the cards on which the next shuffle will be executed.

Your task is to write a program that simulates Tin's trick, i.e. given an initial state of the deck, you'll need to answer queries from the audience.

#### Input Specification

The first line contains two space-separated integers and from the task description. It is guaranteed that is even.

The second line contains space-separated positive integers, a permutation of the set representing the initial state of the deck from the bottom to the top.

The -th of the next lines contains two space-separated integers and , describing the -th query from the audience. More precisely, the query asks about the number written on the -th card from the bottom of the deck after completing shuffles.

#### Output Specification

Output lines, the -th of which contains a single positive integer between and , the answer to the -th query.

#### Constraints

All queries have the same value.

#### Sample Input 1

6 3
1 5 6 2 3 4
1 2
0 4
1 5

#### Sample Output 1

2
2
5

#### Sample Input 2

6 6
2 1 5 4 6 3
0 1
1 1
0 3
1 3
0 6
10 6

#### Sample Output 2

2
2
5
4
3
3

#### Sample Input 3

10 10
7 5 2 9 10 8 4 3 6 1
3 1
3 2
3 3
3 4
3 5
3 6
3 7
3 8
3 9
3 10

#### Sample Output 3

2
3
6
1
7
5
8
4
9
10

#### Explanation for Sample 3

The table below shows the state of the deck after each shuffle. All the queries have , so the output is precisely the state of the deck after shuffles.

Number of shuffles Deck (bottom to top)
7 5 2 9 10 8 4 3 6 1
7 5 2 8 4 3 6 1 9 10
3 6 1 7 5 2 8 4 9 10
2 3 6 1 7 5 8 4 9 10