题目描述:

链接: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

题意:

给你N张牌,初始顺序为1....n;有M次洗牌操作;(qi,si) 表示从【1,n】中 拿第qi张牌+si张牌 放到最上面(类似洗牌)问你到最后牌面的顺序是?

题解:

rope大法好

代码:

#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
rope<int>r;
typedef long long ll;
typedef unsigned long long ull;
#define mod 1000000007
#define pb push_back
int main()
{
ios_base::sync_with_stdio();
//cin.tie(0);
int n,m;
while(cin>>n>>m)
{
for(int i=;i<=n;i++)
r.pb(i);
for(int i=;i<m;i++)
{
int x,y;
cin>>x>>y;
r=r.substr(x-,y)+r.substr(,x-)+r.substr(x+y-,n-x-y+);
}
for(int i=;i<n;++i) cout<<r[i]<<" ";
} return ;
}

好像这里用了rope之后如果再用cin.tie(0);会输不出来,不知道为什么

另外关于rope有博客参考:

https://blog.csdn.net/tianwei0822/article/details/81240063

https://blog.csdn.net/tianwei0822/article/details/81240063

下面是splay的做法:

#include <bits/stdc++.h>
using namespace std;
const int N=;
namespace Splay{
int a[N];
int val[N],mn[N],tag[N],size[N],son[N][],f[N],tot,root;bool rev[N];
int build(int,int,int);
void Initialize(int n){tot=;root=build(,n+,);}
void rev1(int x){if(!x)return;swap(son[x][],son[x][]);rev[x]^=;}
void add1(int x,int p){if(!x)return;val[x]+=p;mn[x]+=p;tag[x]+=p;}
void pb(int x){
if(rev[x]){
rev1(son[x][]);
rev1(son[x][]);
rev[x]=;
}
if(tag[x]){
add1(son[x][],tag[x]);
add1(son[x][],tag[x]);
tag[x]=;
}
}
void up(int x){
size[x]=,mn[x]=val[x];
if(son[x][]){
size[x]+=size[son[x][]];
if(mn[x]>mn[son[x][]])mn[x]=mn[son[x][]];
}
if(son[x][]){
size[x]+=size[son[x][]];
if(mn[x]>mn[son[x][]])mn[x]=mn[son[x][]];
}
}
void rotate(int x){
int y=f[x],w=son[y][]==x;
son[y][w]=son[x][w^];
if(son[x][w^])f[son[x][w^]]=y;
if(f[y]){
int z=f[y];
if(son[z][]==y)son[z][]=x;
if(son[z][]==y)son[z][]=x;
}f[x]=f[y];son[x][w^]=y;f[y]=x;up(y);
}
void splay(int x,int w){
int s=,i=x,y;a[]=x;
while(f[i])a[++s]=i=f[i];
while(s)pb(a[s--]);
while(f[x]!=w){
y=f[x];
if(f[y]!=w){if((son[f[y]][]==y)^(son[y][]==x))rotate(x);else rotate(y);}
rotate(x);
}if(!w)root=x;
up(x);
}
int build(int l,int r,int fa){
int x=++tot,mid=(l+r)>>;
f[x]=fa;val[x]=a[mid];
if(l<mid)son[x][]=build(l,mid-,x);
if(r>mid)son[x][]=build(mid+,r,x);
up(x);
return x;
}
int kth(int k){
int x=root,tmp;
while(){
pb(x);
tmp=size[son[x][]]+;
if(k==tmp)return x;
if(k<tmp)x=son[x][];else k-=tmp,x=son[x][];
}
}
void REVOLVE(int x,int y,int z){
z%=y-x+;
if(z){
int u=x,t;
x=kth(y-z+),y=kth(y+);
splay(x,),splay(y,x),t=son[y][];
son[y][]=,up(y),up(x);
x=kth(u),y=kth(u+);
splay(x,),splay(y,x),son[y][]=t,f[t]=y;
up(y),up(x);
}
}
int A(int x){
int y=kth(x+);
return val[y];
}
}
using namespace Splay;
int n,m;
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)a[i]=i;
root=build(,n+,);
while(m--){
int x,y;
scanf("%d%d",&x,&y);
REVOLVE(,x+y-,y);
}
for(int i=;i<n;i++)printf("%d ",A(i));
printf("%d\n",A(n));
return ;
}

splay

rope(平衡二叉树)的更多相关文章

  1. 算法与数据结构(十一) 平衡二叉树(AVL树)

    今天的博客是在上一篇博客的基础上进行的延伸.上一篇博客我们主要聊了二叉排序树,详情请戳<二叉排序树的查找.插入与删除>.本篇博客我们就在二叉排序树的基础上来聊聊平衡二叉树,也叫AVL树,A ...

  2. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

  3. 谈c++ pb_ds库(一)rope大法好

    参考资料 1)官方说明 支持 sorry,cena不支持rope 声明 1)头文件 #include<ext/rope> 2)调用命名空间 using namespace __gnu_cx ...

  4. [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)

    Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义:   文本:由0个或 ...

  5. [LeetCode] Balanced Binary Tree 平衡二叉树

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  6. Java数据结构——平衡二叉树的平衡因子(转自牛客网)

    若向平衡二叉树中插入一个新结点后破坏了平衡二叉树的平衡性.首先要找出插入新结点后失去平衡的最小子树根结点的指针.然后再调整这个子树中有关结点之间的链接关系,使之成为新的平衡子树.当失去平衡的最小子树被 ...

  7. 【数据结构】平衡二叉树—AVL树

    (百度百科)在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增 ...

  8. 平衡二叉树AVL删除

    平衡二叉树的插入过程:http://www.cnblogs.com/hujunzheng/p/4665451.html 对于二叉平衡树的删除采用的是二叉排序树删除的思路: 假设被删结点是*p,其双亲是 ...

  9. 平衡二叉树AVL插入

    平衡二叉树(Balancedbinary tree)是由阿德尔森-维尔斯和兰迪斯(Adelson-Velskiiand Landis)于1962年首先提出的,所以又称为AVL树. 定义:平衡二叉树或为 ...

随机推荐

  1. 2018-8-10-win10-UWP-序列化

    title author date CreateTime categories win10 UWP 序列化 lindexi 2018-08-10 19:16:50 +0800 2018-2-13 17 ...

  2. R语言ggplot2软件包

    相比r语言自带软件包,ggplot2有以下特色 图形语法的核心:统计图形是数据向几何对象属性的一个映射.

  3. quotaon - 开启关闭文件系统配额

    总览 (SYNOPSIS) quotaon [ -e | d ] [ -vug ] filesystem... quotaon [ -e | d ] [ -avug ] quotaoff [ -e | ...

  4. mangodb语句

    { field: { $exists: <boolean> } }

  5. 前端每日实战:53# 视频演示如何用纯 CSS 创作一个文本淡入淡出的 loader 动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/ERwpeG 可交互视频 此视频是可 ...

  6. Factory Methods

    The static factory method pattern is a way to encapsulate object creation. Without a factory method, ...

  7. python爬取“美团美食”汕头地区的所有店铺信息

    一.目的 获取美团美食每个店铺所有的评论信息,并保存到数据库和本地 二.实现步骤 获取所有店铺的poiId 首先观察详情页的url,后面是跟着一串数字的,而这一串数字代表着每个店铺特有的id号,我们称 ...

  8. .NET面试题集锦②

    一.前言部分 文中的问题及答案多收集整理自网络,不保证100%准确,还望斟酌采纳. 1.实现产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复. ]; ArrayList my ...

  9. Activity(工作流-1)

    1.activity自带数据表的含义(23张表) (1)资源库流程规则表 1)act_re_deployment 部署信息表 2)act_re_model  流程设计模型部署表 3)act_re_pr ...

  10. click和blur冲突的问题

    昨天在前端群里讨论到一个问题,大家平时做表单验证的时候一般都有个input框和删除按钮,然后习惯性在失去焦点的时候去验证输入的内容是否正确,做验证,发请求等等.这个时候,那个点击删除按钮往往也就触发了 ...