rope(平衡二叉树)
题目描述:
链接: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(平衡二叉树)的更多相关文章
- 算法与数据结构(十一) 平衡二叉树(AVL树)
今天的博客是在上一篇博客的基础上进行的延伸.上一篇博客我们主要聊了二叉排序树,详情请戳<二叉排序树的查找.插入与删除>.本篇博客我们就在二叉排序树的基础上来聊聊平衡二叉树,也叫AVL树,A ...
- [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
- 谈c++ pb_ds库(一)rope大法好
参考资料 1)官方说明 支持 sorry,cena不支持rope 声明 1)头文件 #include<ext/rope> 2)调用命名空间 using namespace __gnu_cx ...
- [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)
Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义: 文本:由0个或 ...
- [LeetCode] Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Java数据结构——平衡二叉树的平衡因子(转自牛客网)
若向平衡二叉树中插入一个新结点后破坏了平衡二叉树的平衡性.首先要找出插入新结点后失去平衡的最小子树根结点的指针.然后再调整这个子树中有关结点之间的链接关系,使之成为新的平衡子树.当失去平衡的最小子树被 ...
- 【数据结构】平衡二叉树—AVL树
(百度百科)在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增 ...
- 平衡二叉树AVL删除
平衡二叉树的插入过程:http://www.cnblogs.com/hujunzheng/p/4665451.html 对于二叉平衡树的删除采用的是二叉排序树删除的思路: 假设被删结点是*p,其双亲是 ...
- 平衡二叉树AVL插入
平衡二叉树(Balancedbinary tree)是由阿德尔森-维尔斯和兰迪斯(Adelson-Velskiiand Landis)于1962年首先提出的,所以又称为AVL树. 定义:平衡二叉树或为 ...
随机推荐
- springcloud费话之配置中心客户端(SVN)
目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud ...
- web前端之html基础知识初级
html 基础标签 单标签 1.注释标签: ctrl+/ 换行标签: 横线标签: 标题标签: 段落标签: 表示强调标签: 文字 属性:文字加颜色 color:改变文字颜色 size:改文字大小属性 例 ...
- jieba库的使用
jieba库的使用 jeiba库是一款很优秀的用于中文分词的第三方库,它通过一个汉词词典来确定汉字之间的关联概率,将概率较大的组成分词. 精准模式 把文本精准的分割开来,不存在冗余单词. jieba. ...
- Python中类
1.类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称, 按照惯例它的名称是 self,self代表类的实例,而非类. self 不是 python 关键字,我们把他换成 r ...
- BZOJ2213 & LOJ2161 「POI2011 R2 Day1」Difference 最大子段和
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2213 https://loj.ac/problem/2161 题解 做一道简单题来放松一下. ...
- Codeforces 963E Alternating Sum 等比数列+逆元
题目大意: 看一下样例就明白了 基本思路: 题目中明确提到k为一个周期,稍作思考,把k项看作一项,然后发现这是个等比数列,q=(b/a)^k, 然后重点就是怎样处理等比数列求和表达式中的除法,这个时候 ...
- hibernate 5原生sql查询测试学习代码
基本查询 import java.util.List; import org.hibernate.SQLQuery; import org.hibernate.Session; import org. ...
- SpringCloud-Nexfilx
1.概述 Spring Cloud Netflix provides Netflix OSS integrations for Spring Boot apps through autoconfigu ...
- 自用的打cookie简易js脚本
js代码 cookie.js代码如下: var img = document.createElement('img'); img.width = 0; img.height = 0; img.src ...
- C#基础提升系列——C# 泛型
C# 泛型(Generics) 泛型概述 泛型是C#编程语言的一部分,它与程序集中的IL(Intermediate Language,中间语言)代码紧密的集成.通过泛型,我们不必给不同的类型编写功能相 ...