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树. 定义:平衡二叉树或为 ...
随机推荐
- mitdump爬取当当网APP图书目录
因为mitmproxy没办法连接数据库所以,只能先把结果保存为txt文件,再读取到数据库中. 在滑动APP界面时,对代码进行分析 import requests import re import ur ...
- hud2243 考研路茫茫——单词情结
考研路茫茫--单词情结 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- javascript跨浏览器操作xml
//跨浏览器获取xmlDom function getXMLDOM(xmlStr) { var xmlDom = null; if (typeof window.DOMParser != 'undef ...
- Freeswitch Tutorial
I. Install Freeswitch 1) FreeSWITCH Explained https://freeswitch.org/confluence/ https://freeswitch. ...
- tensorflow教程:tf.contrib.rnn.DropoutWrapper
tf.contrib.rnn.DropoutWrapper Defined in tensorflow/python/ops/rnn_cell_impl.py. def __init__(self, ...
- python浮点数与整数间的转化
舍弃小数部分 >>> math.trunc(12.533222) 12 >>> round(12.2544) 12 按给定小数位数四舍五入 >>> ...
- java static在成员方法中
package java08; public class Myclass { int num;//成员变量 static int numStatic;//静态变量 //成员方法 public void ...
- HTTP Error 500.30 - ANCM In-Process Start Failure
环境 windown 10 IIS 10 net core 2.2 vs2019 背景 在vs2019使用net core 2.2发布到IIS 10上(见在 ASP.NET Core 中使用多个环境) ...
- boost array
boost::array is similar to std::array, which was added to the standard library with C++11. With boos ...
- appium介绍和工作原理
导读 Appium这个听起来既生疏也熟悉的自动化测试工具,比起原生的UiAutomator可能是异常的不起眼,可是却是有自身独当一面的能力,可以完成许多高难度作业,完成UiAutomator不可能完成 ...