309453: CF1681B. Card Trick

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Card Trick

题意翻译

给定一个长度为 $n$ 的数组,对其进行 $m$ 次操作,每次操作是一个数 $b_j$,意思是将数组前 $b_j$ 个元素放到数组最后面。问执行完操作后数组第一个元素是啥。

题目描述

Monocarp has just learned a new card trick, and can't wait to present it to you. He shows you the entire deck of $ n $ cards. You see that the values of cards from the topmost to the bottommost are integers $ a_1, a_2, \dots, a_n $ , and all values are different. Then he asks you to shuffle the deck $ m $ times. With the $ j $ -th shuffle, you should take $ b_j $ topmost cards and move them under the remaining $ (n - b_j) $ cards without changing the order. And then, using some magic, Monocarp tells you the topmost card of the deck. However, you are not really buying that magic. You tell him that you know the topmost card yourself. Can you surprise Monocarp and tell him the topmost card before he shows it?

输入输出格式

输入格式


The first line contains a single integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of testcases. The first line of each testcase contains a single integer $ n $ ( $ 2 \le n \le 2 \cdot 10^5 $ ) — the number of cards in the deck. The second line contains $ n $ pairwise distinct integers $ a_1, a_2, \dots, a_n $ ( $ 1 \le a_i \le n $ ) — the values of the cards. The third line contains a single integer $ m $ ( $ 1 \le m \le 2 \cdot 10^5 $ ) — the number of shuffles. The fourth line contains $ m $ integers $ b_1, b_2, \dots, b_m $ ( $ 1 \le b_j \le n - 1 $ ) — the amount of cards that are moved on the $ j $ -th shuffle. The sum of $ n $ over all testcases doesn't exceed $ 2 \cdot 10^5 $ . The sum of $ m $ over all testcases doesn't exceed $ 2 \cdot 10^5 $ .

输出格式


For each testcase, print a single integer — the value of the card on the top of the deck after the deck is shuffled $ m $ times.

输入输出样例

输入样例 #1

3
2
1 2
3
1 1 1
4
3 1 4 2
2
3 1
5
2 1 5 4 3
5
3 2 1 2 1

输出样例 #1

2
3
3

说明

In the first testcase, each shuffle effectively swaps two cards. After three swaps, the deck will be $ [2, 1] $ . In the second testcase, the second shuffle cancels what the first shuffle did. First, three topmost cards went underneath the last card, then that card went back below the remaining three cards. So the deck remained unchanged from the initial one — the topmost card has value $ 3 $ .

Input

题意翻译

给定一个长度为 $n$ 的数组,对其进行 $m$ 次操作,每次操作是一个数 $b_j$,意思是将数组前 $b_j$ 个元素放到数组最后面。问执行完操作后数组第一个元素是啥。

Output

**题意翻译**

给定一个长度为 $ n $ 的数组,进行 $ m $ 次操作,每次操作是将数组前 $ b_j $ 个元素移动到数组末尾。求执行完所有操作后数组的第一个元素是什么。

**题目描述**

Monocarp 刚学会了一个新的纸牌戏法,并且迫不及待地要展示给你看。他展示给你一整副由 $ n $ 张牌组成的牌堆。你看到从顶部到底部的牌的值是整数 $ a_1, a_2, \dots, a_n $,所有的值都是不同的。

然后他要求你洗牌 $ m $ 次。在第 $ j $ 次洗牌时,你应该取最上面的 $ b_j $ 张牌并将它们放到剩余的 $ (n - b_j) $ 张牌下面,在这个过程中不改变牌的顺序。

然后,使用一些魔术,Monocarp 会告诉你牌堆最上面的牌是什么。然而,你并不真的相信这个魔术。你告诉他你知道最上面的牌是什么。你能在 Monocarp 展示之前告诉他最上面的牌是什么,从而给他一个惊喜吗?

**输入输出格式**

**输入格式**

第一行包含一个整数 $ t $($ 1 \le t \le 10^4 $)——测试用例的数量。

每个测试用例的第一行包含一个整数 $ n $($ 2 \le n \le 2 \cdot 10^5 $)——牌堆中牌的数量。

第二行包含 $ n $ 个互不相同的整数 $ a_1, a_2, \dots, a_n $($ 1 \le a_i \le n $)——牌的值。

第三行包含一个整数 $ m $($ 1 \le m \le 2 \cdot 10^5 $)——洗牌的次数。

第四行包含 $ m $ 个整数 $ b_1, b_2, \dots, b_m $($ 1 \le b_j \le n - 1 $)——在第 $ j $ 次洗牌时移动的牌的数量。

所有测试用例的 $ n $ 之和不超过 $ 2 \cdot 10^5 $。所有测试用例的 $ m $ 之和不超过 $ 2 \cdot 10^5 $。

**输出格式**

对于每个测试用例,打印一个整数——在洗牌 $ m $ 次后牌堆最上面的牌的值。

**输入输出样例**

**输入样例 #1**

```
3
2
1 2
3
1 1 1
4
3 1 4 2
2
3 1
5
2 1 5 4 3
5
3 2 1 2 1
```

**输出样例 #1**

```
2
3
3
```

**说明**

在第一个测试用例中,每次洗牌实际上都是交换两张牌。三次交换后,牌堆将是 $ [2, 1] $。

在第二个测试用例中,第二次洗牌抵消了第一次洗牌的效果。首先,最上面的三张牌被移到最后一张牌下面,然后那张牌又回到了剩余的三张牌下面。因此,牌堆保持不变——最上面的牌的值是 $ 3 $。**题意翻译** 给定一个长度为 $ n $ 的数组,进行 $ m $ 次操作,每次操作是将数组前 $ b_j $ 个元素移动到数组末尾。求执行完所有操作后数组的第一个元素是什么。 **题目描述** Monocarp 刚学会了一个新的纸牌戏法,并且迫不及待地要展示给你看。他展示给你一整副由 $ n $ 张牌组成的牌堆。你看到从顶部到底部的牌的值是整数 $ a_1, a_2, \dots, a_n $,所有的值都是不同的。 然后他要求你洗牌 $ m $ 次。在第 $ j $ 次洗牌时,你应该取最上面的 $ b_j $ 张牌并将它们放到剩余的 $ (n - b_j) $ 张牌下面,在这个过程中不改变牌的顺序。 然后,使用一些魔术,Monocarp 会告诉你牌堆最上面的牌是什么。然而,你并不真的相信这个魔术。你告诉他你知道最上面的牌是什么。你能在 Monocarp 展示之前告诉他最上面的牌是什么,从而给他一个惊喜吗? **输入输出格式** **输入格式** 第一行包含一个整数 $ t $($ 1 \le t \le 10^4 $)——测试用例的数量。 每个测试用例的第一行包含一个整数 $ n $($ 2 \le n \le 2 \cdot 10^5 $)——牌堆中牌的数量。 第二行包含 $ n $ 个互不相同的整数 $ a_1, a_2, \dots, a_n $($ 1 \le a_i \le n $)——牌的值。 第三行包含一个整数 $ m $($ 1 \le m \le 2 \cdot 10^5 $)——洗牌的次数。 第四行包含 $ m $ 个整数 $ b_1, b_2, \dots, b_m $($ 1 \le b_j \le n - 1 $)——在第 $ j $ 次洗牌时移动的牌的数量。 所有测试用例的 $ n $ 之和不超过 $ 2 \cdot 10^5 $。所有测试用例的 $ m $ 之和不超过 $ 2 \cdot 10^5 $。 **输出格式** 对于每个测试用例,打印一个整数——在洗牌 $ m $ 次后牌堆最上面的牌的值。 **输入输出样例** **输入样例 #1** ``` 3 2 1 2 3 1 1 1 4 3 1 4 2 2 3 1 5 2 1 5 4 3 5 3 2 1 2 1 ``` **输出样例 #1** ``` 2 3 3 ``` **说明** 在第一个测试用例中,每次洗牌实际上都是交换两张牌。三次交换后,牌堆将是 $ [2, 1] $。 在第二个测试用例中,第二次洗牌抵消了第一次洗牌的效果。首先,最上面的三张牌被移到最后一张牌下面,然后那张牌又回到了剩余的三张牌下面。因此,牌堆保持不变——最上面的牌的值是 $ 3 $。

加入题单

算法标签: