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

链接:https://www.nowcoder.com/acm/contest/141/C
来源:牛客网
 

题目描述

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.

输入

5 1
2 3

输出

2 3 4 1 5

 

题意:一个1到n的全排列,m次操作,每次操作将一个区间内所有的数全部移到最前面,求最终的序列

  1. 假设当前操作要移动区间[l, r],先将l-1旋到根,r+1旋到根的右儿子
  2. 旋转之后节点r+1的左子树正是区间[l, r],那么只要把这棵子树重新接到rank为1的节点上就可以了

 

#include<stdio.h>
#include<string.h>
#define inf 1000000000
using namespace std;
int n, m, root, sz, tre[220005][2], fa[220005], deep[220005], a[220005], size[220005], v[220005], pos[220005];
void Update(int k)
{
	size[k] = size[tre[k][0]]+size[tre[k][1]]+1;
}
void Create(int l, int r, int last)
{
	int mid;
	if(l>r)
		return;
	mid = (l+r)/2;
	if(l==r)
	{
		v[mid] = r;
		size[mid] = 1;
		fa[mid] = last;
		if(mid<last)  tre[last][0] = mid;
		else  tre[last][1] = mid;
		return;
	}
	Create(l, mid-1, mid);
	Create(mid+1, r, mid);
	fa[mid] = last;
	Update(mid);
	if(mid<last)  tre[last][0] = mid;
	else  tre[last][1] = mid;
}
void Rotate(int x, int &k)
{
    int l, r, y, z;
    y = fa[x], z = fa[y];
    if(tre[y][0]==x)  l = 0;
    else  l = 1;
    r = l^1;
    if(y==k)
        k = x;
    else
    {
        if(tre[z][0]==y)  tre[z][0] = x;
        else  tre[z][1] = x;
    }
    fa[x] = z, fa[y] = x;
    fa[tre[x][r]] = y;
    tre[y][l] = tre[x][r];
    tre[x][r] = y;
    Update(y);
    Update(x);
}
void Splay(int x, int &k)
{
    int y, z;
    while(x!=k)
    {
        y = fa[x], z = fa[y];
        if(y!=k)
        {
            if((tre[y][0]==x)^(tre[z][0]==y))
                Rotate(x, k);
            else
                Rotate(y, k);
        }
        Rotate(x, k);
    }
}
int Find(int k, int rank)
{
	int l, r;
	l = tre[k][0], r = tre[k][1];
	if(size[l]+1==rank)
		return k;
	else if(size[l]>=rank)
		return Find(l, rank);
	else
		return Find(r, rank-size[l]-1);
}
void DEBUG(int k)
{
	printf("%d %d %d\n", k, tre[k][0], tre[k][1]);
	if(tre[k][0])
		DEBUG(tre[k][0]);
	if(tre[k][1])
		DEBUG(tre[k][1]);
	return;
}
void Move(int k)
{
	Update(k);
	if(fa[k]!=0)
		Move(fa[k]);
}
void Turn(int l, int r)
{
	int x, y, z, p;
	x = Find(root, l-1);
	y = Find(root, r+1);
	Splay(x, root);
	Splay(y, tre[x][1]);
	z = tre[y][0];
	p = Find(root, 1);
	if(tre[p][1]==0)
		tre[p][1] = z;
	else
	{
		p = Find(root, 2);
		tre[p][0] = z;
	}
	fa[z] = p;
	Move(p);
	tre[y][0] = 0;
	Move(y);
}
int main(void)
{
	int x, y, n, i, m;
	scanf("%d%d", &n, &m);
	Create(1, n+2, 0);
	root = (n+3)/2;
	while(m--)
	{
		scanf("%d%d", &x, &y);
		if(x==1)
			continue;
		y = x+y-1;
		Turn(x+1, y+1);
	}
	for(i=2;i<=n+1;i++)
	{
		if(i==2)
			printf("%d", Find(root, i)-1);
		else
			printf(" %d", Find(root, i)-1);
	}
	puts("");
	return 0;
}
/*
3 1
2 1
*/

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值