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树. 定义:平衡二叉树或为 ...
随机推荐
- JS-03 牛客网练习
1.很多人都使用过牛客网这个在线编程网站,下面是自己做的该项所有练习,已通过网站和老师检查无误,分享给大家. 2.先说一下题目的位置:牛客网https://www.nowcoder.com/activ ...
- Linux修改密码指令
1.在选择系统菜单界面,按 "e" 进入编辑模式 2.在以字符串“Linux16”开头的行,将光标移动到该行的结尾,然后输入“init=/bin/bash”,按 "Ctr ...
- 二 shell 基础
一 文件的 权限基础 文件有三类权限 user,group,other, 权限分为 r w x 代表数字分别为 4 2 1 修改权限命令 chmod 权限还有特殊权限,在执行的时候代表某一身 ...
- 逗号导致hive报“SemanticException Error in parsing”错误
> select p.dt, p.cookie_qunar_global, p.refer_domain, p.kwid, p.query_word, p,traffic_type--, p.p ...
- apt-get update 101错误解决办法
在一次装好Ubuntu系统, 执行 sudo apt-get update 时,报了错 " W: Failed to fetch http://security.ubuntu.com/ubu ...
- Sass Maps的函数-map-values($map)、map-merge($map1,$map2)
map-values($map) map-values($map) 函数类似于 map-keys($map) 功能,不同的是 map-values($map )获取的是 $map 的所有 value ...
- Hibernate 一对多配置 级联操作(级联失败问题分析解决)
一方: package com.xdfstar.domain; import java.io.Serializable;import java.util.Date;import java.util.H ...
- WPF 几种常用控件样式的总结
这里把wpf中几种常用样式总结一下,后期可以直接拷贝使用,呵呵 一.Button <ResourceDictionary xmlns="http://schemas.microsoft ...
- Testng报错:method-selectors?,parameter*,groups?,packages?,classes?
以上的报错信息有两种可能的原因: 1.xml的格式确实有误 2.xml的格式正确,但是工程中testng的jar包不止一个,从而导致有歧义或者冲突(这个很可能发生在同一个workspace有多个工程的 ...
- Java线程与线程、进程与进程之间通信方式
1.1 基本概念以及线程与进程之间的区别联系 关于进程和线程,首先从定义上理解就有所不同: 进程是具有一定独立功能的程序.它是系统进行资源分配和调度的一个独立单位,重点在系统调度和单独的单位,也就是说 ...