题目描述:

链接: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. Failed to load C:\ProgramFilesTwo\Android\sdk\build-tools\27.0.3\lib\dx.jar

    Eclipse遇到如下错误: Failed to load C:\ProgramFilesTwo\Android\sdk\build-tools\27.0.3\lib\dx.jar 原因: eclip ...

  2. Shell 脚本举例

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

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

  4. linux安装jdk环境(多种方式)

    通过tar.gz压缩包安装 此方法适用于绝大部分的linux系统 1.先下载tar.gz的压缩包,这里使用官网下载. 进入: http://www.oracle.com/technetwork/jav ...

  5. Windows 搭建MongoDB分片集群(一)

    一.角色说明 要构建一个MongoDB分片集群,需要三个角色: shard server  即存储实际数据得分片,每个shard 可以是一个Mongod实例,也可以是一组mongod实例构成得Repl ...

  6. spring整合消息队列rabbitmq

    ps:本文只是简单一个整合介绍,属于抛砖引玉,具体实现还需大家深入研究哈.. 1.首先是生产者配置 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  7. Django前后端分离跨域请求问题

    一.问题背景 之前使用django+vue进行前后端分离碰到跨域请求问题,跨域(域名或者端口不同)请求问题的本质是由于浏览器的同源策略导致的,当请求的响应不是处于同一个域名和端口下,浏览器不会接受响应 ...

  8. 判断是否为PC

    function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = ["Android", " ...

  9. oracle跟踪

    select sql_text --sql文本的前1000个字符 ,first_load_time --初次载入时间 ,last_load_time --最后载入时间 ,s.sql_fulltext ...

  10. Sublime Text 注册及使用相关

    sublime text3 注册码 2019-07-01 注册码可以直接用 地址: 2019-07-01 亲测可用 2019-07-18 亲测可用 -– BEGIN LICENSE -– Die So ...