牛客网暑期ACM多校训练营(第三场) C Shuffle Cards

  • 2018-10-08
  • 23
  • 0

Description:

Eddy likes to play cards game since there are always lots of randomness in the game. For most of the cards game, the very first step in the game is shuffling the cards. And, mostly the randomness in the game is from this step. However, Eddy doubts that if the shuffling is not done well, the order of the cards is predictable!

To prove that, Eddy wants to shuffle cards and tries to predict the final order of the cards. Actually, Eddy knows only one way to shuffle cards that is taking some middle consecutive cards and put them on the top of rest. When shuffling cards, Eddy just keeps repeating this procedure. After several rounds, Eddy has lost the track of the order of cards and believes that the assumption he made is wrong. As Eddy’s friend, you are watching him doing such foolish thing and easily memorizes all the moves he done. Now, you are going to tell Eddy the final order of cards as a magic to surprise him.

Eddy has showed you at first that the cards are number from 1 to N from top to bottom.

For example, there are 5 cards and Eddy has done 1 shuffling. He takes out 2-nd card from top to 4-th card from top(indexed from 1) and put them on the top of rest cards. Then, the final order of cards from top will be [2,3,4,1,5].

Input:

The first line contains two space-separated integer N, M indicating the number of cards and the number of shuffling Eddy has done.

Each of following M lines contains two space-separated integer pi, si indicating that Eddy takes pi-th card from top to (pi+si-1)-th card from top(indexed from 1) and put them on the top of rest cards.

1 ≤ N, M ≤ 105

1 ≤ pi ≤ N

1 ≤ si ≤ N-pi+1

Output

Output one line contains N space-separated integers indicating the final order of the cards from top to bottom.

Sample Input:

5 1
2 3

Sample Output:

2 3 4 1 5

Sample Input:

5 2
2 3
2 3

Sample Output:

3 4 1 2 5

Sample Input:

5 3
2 3
1 4
2 4

Sample Output:

3 4 1 5 2

题目链接

对一串N个数的序列(1~N)进行M此操作,每次操作把P~P+S段区间移动到序列起始位置,求最后序列。

将一段区间移动到序列起始位置可以用三次序列区间翻转达到,而区间翻转则可以通过Splay高效实现。

例若将序列1,2,3,4,5,6,7,8,9,10中4~7区间序列移动到整个序列的起始位置首先翻转1~7区间(调换目标区间与其左侧区间位置)后获得:7,6,5,4,3,2,1,8,9,10,接着翻转1~4区间(翻转目标区间调整序列顺序)序列获得:4,5,6,7,3,2,1,8,9,10,最后翻转5~7区间(翻转区间调整序列顺序)获得:4,5,6,7,1,2,3,8,9,10,此序列即为序列移动操作结果。

AC代码:

评论

还没有任何评论,你来说两句吧