BZOJ 5063: 旅游 splay

5063: 旅游

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 49  Solved: 26
[Submit][Status][Discuss]

Description

小奇成功打开了大科学家的电脑。
大科学家打算前往n处景点旅游,他用一个序列来维护它们之间的顺序。初
始时,序列为1,2,...,n。
接着,大科学家进行m次操作来打乱顺序。每次操作有6步:
1、从序列开头(左端)取出A个数(此时序列剩下n-A个数)
2、从序列开头取出B个数
3、将第1步取出的A个数按原顺序放回序列开头
4、从序列开头取出C个数
5、将第2步取出的B个数逆序放回序列开头
6、将第4步取出的C个数按原顺序放回序列开头
你需要求出最终序列。

Input

第一行两个数n,m。接下来m行,每行三个数A,B,C。
n,m<=100000

Output

输出一行n个数表示最终序列。

Sample Input

10 2
6 2 2
5 3 6

Sample Output

1 2 8 7 3 9 6 5 4 10

splay练习题

支持区间翻转

区间平移(觉得这个说法不太对 但也没想到别的。。)


最开始没跳出来 最后发现打rever标记的时候竟然直接赋等 太傻了。。

#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;

typedef long long ll;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=100100;

int ch[N][2],fa[N],size[N];
bool rev[N];
int root;

inline void pushup(int k)
{size[k]=size[ch[k][0]]+size[ch[k][1]]+1;}

inline void pushdown(int k)
{
	if(rev[k])
	{
		rev[ch[k][0]]^=1;rev[ch[k][1]]^=1;
		swap(ch[k][0],ch[k][1]);
		rev[k]=0;
	}
}

int build(int l,int r,int pre)
{
	if(l>r)return 0;
	int mid=(l+r)>>1;
	fa[mid]=pre;
	ch[mid][0]=build(l,mid-1,mid);
	ch[mid][1]=build(mid+1,r,mid);
	pushup(mid);
	return mid;
}

int find(int k,int rk)
{
	pushdown(k);
	if(size[ch[k][0]]>=rk)
		return find(ch[k][0],rk);
	if(size[ch[k][0]]+1<rk)
		return find(ch[k][1],rk-1-size[ch[k][0]]);
	return k;
}

inline void rotate(int x,int &k)
{
	int y=fa[x],z=fa[y],l,r;
	l=(x==ch[y][1]);r=l^1;
	if(y==k)k=x;
	else ch[z][ch[z][1]==y]=x;
	fa[x]=z;fa[y]=x;fa[ch[x][r]]=y;
	ch[y][l]=ch[x][r];ch[x][r]=y;
	pushup(y);pushup(x);
}

int st[N],top;

void splay(int x,int &k)
{
	int y,z;
	while(x!=k)
	{
		y=fa[x];z=fa[y];
		if(y!=k)
		{
			if((ch[y][0]==x)^(ch[z][0]==y))rotate(x,k);
			else rotate(y,k);
		}
		rotate(x,k);
	}
}

void dfs(int k)
{
	if(!k)return ;
	pushdown(k);
	dfs(ch[k][0]);
	st[++top]=k-1;
	dfs(ch[k][1]);
}

int main()
{
	int n=read(),Q=read();
	register int x,A,B,C;
	root=build(1,n+2,0);
	while(Q--)
	{
		A=read();B=read();C=read();
		splay(find(root,A+1),root);splay(find(root,A+B+2),ch[root][1]);
		x=ch[ch[root][1]][0];
		ch[ch[root][1]][0]=0;
		pushup(ch[root][1]);pushup(root);
		rev[x]^=1;
		splay(find(root,C+1),root);splay(find(root,C+2),ch[root][1]);
		fa[x]=ch[root][1];
		ch[ch[root][1]][0]=x;
		pushup(ch[root][1]);pushup(root);
	}
	top=0;
	dfs(root);
	for(x=2;x<=n+1;++x)print(st[x]),putchar(' ');
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值