luogu#P8996. [CEOI 2022] Abracadabra

    ID: 8348 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>线段树平衡树树状数组2022CEOI(中欧)

[CEOI 2022] Abracadabra

Problem Description

Tin is a famous magician. One of his classic tricks is about shuffling cards.

Tin prepares a deck of cards with a total of nn cards (it is guaranteed that nn is even), numbered 1n1 \sim n. At the beginning, the deck is in a random order and placed face down on the table. Then he starts performing the shuffle. At any moment during the shuffle, the audience may ask Tin which card is the tt-th card from the bottom. Obviously, Tin can always immediately give the correct answer.

In fact, Tin performs the trick as follows. First, he memorizes the initial order of the nn cards, and then shuffles using this technique:

  1. Pick up the top n2\frac{n}{2} cards with his right hand, and the bottom n2\frac{n}{2} cards with his left hand, with the faces of the cards towards the table.
  2. Using his memory, compare the bottom cards of the left and right hands. Put down the card with the smaller number. Repeat this operation until one hand becomes empty.
  3. Put down all remaining cards in the non-empty hand.

Please write a program to simulate Tin’s trick.

Input Format

The first line contains two integers N,QN, Q.

The next line contains NN integers pip_i, describing the whole deck from bottom to top.

The next QQ lines each contain one query t,it, i, meaning: after tt shuffles, what is the card number of the ii-th card from the bottom.

Output Format

For each query, output the answer.

6 3
1 5 6 2 3 4
1 2
0 4
1 5
2
2
5
6 6
2 1 5 4 6 3
0 1
1 1
0 3
1 3
0 6
10 6
2
2
5
4
3
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
2
3
6
1
7
5
8
4
9
10

Hint

Explanation for Sample 3

Number of shuffles Deck from bottom to top
00 7 5 2 9 10 8 4 3 6 17\ 5\ 2\ 9\ 10\ 8\ 4\ 3\ 6\ 1
11 7 5 2 8 4 3 6 1 9 107\ 5\ 2\ 8\ 4\ 3\ 6\ 1\ 9\ 10
22 3 6 1 7 5 2 8 4 9 103\ 6\ 1\ 7\ 5\ 2\ 8\ 4\ 9\ 10
33 2 3 6 1 7 5 8 4 9 102\ 3\ 6\ 1\ 7\ 5\ 8\ 4\ 9\ 10

Constraints

For all testdata, 1N2×1051 \le N \le 2 \times 10^5, NN is even, 1Q1061 \le Q \le 10^6, 0t1090 \le t \le 10^9, pp is a permutation of 1n1 \sim n, and 1iN1 \le i \le N.

Subtask ID Special constraints Score
11 N103N \le 10^3 1010
22 All queries have the same tt 4040
33 N,Q105N, Q \le 10^5 2525
44 No special constraints

Translated by ChatGPT 5