题目描述
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].
输入描述:
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 one line contains N space-separated integers indicating the final order of the cards from top to bottom.
示例1
输入
复制
5 1
2 3
输出
复制
2 3 4 1 5
示例2
输入
复制
5 2
2 3
2 3
输出
复制
3 4 1 2 5
示例3
输入
复制
5 3
2 3
1 4
2 4
输出
复制
3 4 1 5 2
题意:N个数的数组初始分别是1~N,有M个操作,每个操作将一段区间移到数组开头,问最终数组是怎样的。
思路:这题最简单就是用C++封装好的容器rope,也可以写平衡树,比如无旋treap,treap就是堆和二叉搜索树的结合体,无旋treap就是没有旋转操作的treap,功能更强大。本题属于该数据结构的基本操作,将p下标的s个数移到开头,那么把树砍成三部分再连接起来就行。
# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
inline int Rand()
{
static int seed=233;
return seed=int(seed*48271LL%2147483647);
}
struct Treap
{
Treap* son[2];
int weight,sze,data;
Treap(int v)
{
sze=1; data=v; weight=Rand();
son[1] = son[0] = NULL;
return;
}
inline void update()
{
sze = 1+(son[0]!=NULL?son[0]->sze:0)+(son[1]!=NULL?son[1]->sze:0);
return;
}
}*root;
typedef pair<Treap*,Treap*>D;
inline int sze(Treap* pos){return pos?pos->sze:0;}
Treap* merge(Treap* a,Treap* b)
{
if(!a) return b;
if(!b) return a;
if(a->weight < b->weight)
{
a->son[1] = merge(a->son[1],b);
a->update();
return a;
}else
{
b->son[0] = merge(a,b->son[0]);
b->update();
return b;
}
}
D split(Treap* pos,int k)
{
if(pos == NULL) return D(NULL,NULL);
D y;
if(sze(pos->son[0]) >= k)
{
y = split(pos->son[0],k);
pos->son[0] = y.second;
pos->update();
y.second = pos;
}
else
{
y=split(pos->son[1],k-1-sze(pos->son[0]));
pos->son[1] = y.first;
pos->update();
y.first = pos;
}
return y;
}
vector<int>ans;
void output(Treap* a)
{
if(a == NULL) return;
output(a->son[0]);
ans.push_back(a->data);
output(a->son[1]);
}
int main()
{
int n, m, l, r;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; ++i)
root = merge(root, new Treap(i));
while(m--)
{
scanf("%d%d",&l,&r);
D x = split(root, l+r-1);
D y = split(x.first, l-1);
merge(y.second, merge(y.first, x.second));
}
output(root);
for(int i=0; i<ans.size(); ++i) printf("%d%c",ans[i],i==ans.size()-1?'\n':' ');
return 0;
}