牛客网多校第3场C-shuffle card 平衡树或stl(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 to N from top to bottom.
- For example, there are cards and Eddy has done shuffling. He takes out -nd card from top to -th card from top(indexed from ) and put them on the top of rest cards. Then, the final order of cards from top will be [,,,,].
- 输入描述:
- 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-)-th card from top(indexed from ) and put them on the top of rest cards.
- ≤ N, M ≤
- ≤ pi ≤ N
- ≤ si ≤ N-pi+
- 输出描述:
- Output one line contains N space-separated integers indicating the final order of the cards from top to bottom.
- 示例1
- 输入
- 复制
- 输出
- 复制
- 示例2
- 输入
- 复制
- 输出
- 复制
- 示例3
- 输入
- 复制
- 输出
- 复制
- #include<bits/stdc++.h>
- #include<ext/rope> //固定写法
- using namespace std;
- using namespace __gnu_cxx; //固定写法
- rope<int> s; //实质是可持久化平衡树
- int main()
- {
- int n,m,l,e,i;
- scanf("%d%d",&n,&m);
- for(i=;i<=n;i++){
- s.push_back(i); //放元素
- }
- while(m--){
- scanf("%d%d",&l,&e);
- s=s.substr(l-,e)+s.substr(,l-)+s.substr(l+e-,n-e-(l-)); //将区间放置首位,重新组合数组,substr(起始字符,元素个数)
- }
- for(i=;i<s.size();i++){
- if(i>) printf(" ");
- printf("%d",s[i]); //元素按下标输出
- }
- return ;
- }
平衡树:他是区间旋转。你可以通过旋转3次得倒。
- #include<iostream>
- #include<stdio.h>
- using namespace std;
- int n,m,sz,rt;
- int fa[],c[][],id[];
- int size[];
- bool rev[];
- void pushup(int k)
- {
- int l=c[k][],r=c[k][];
- size[k]=size[l]+size[r]+;
- }
- void pushdown(int k)
- {
- int l=c[k][],r=c[k][];
- if(rev[k])
- {
- swap(c[k][],c[k][]);
- rev[l]^=;rev[r]^=;
- rev[k]=;
- }
- }
- void rotate(int x,int &k)
- {
- int y=fa[x],z=fa[y],l,r;
- if(c[y][]==x)l=;else l=;r=l^;
- if(y==k)k=x;
- else {if(c[z][]==y)c[z][]=x;else c[z][]=x;}
- fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
- c[y][l]=c[x][r];c[x][r]=y;
- pushup(y);pushup(x);
- }
- void splay(int x,int &k)
- {
- while(x!=k)
- {
- int y=fa[x],z=fa[y];
- if(y!=k)
- {
- if(c[y][]==x^c[z][]==y)rotate(x,k);
- else rotate(y,k);
- }
- rotate(x,k);
- }
- }
- int find(int k,int rank)
- {
- pushdown(k);
- int l=c[k][],r=c[k][];
- if(size[l]+==rank)return k;
- else if(size[l]>=rank)return find(l,rank);
- else return find(r,rank-size[l]-);
- }
- void rever(int l,int r)
- {
- int x=find(rt,l),y=find(rt,r+);
- splay(x,rt);splay(y,c[x][]);
- int z=c[y][];
- rev[z]^=;
- }
- void build(int l,int r,int f)
- {
- if(l>r)return;
- int now=id[l],last=id[f];
- if(l==r)
- {
- fa[now]=last;size[now]=;
- if(l<f)c[last][]=now;
- else c[last][]=now;
- return;
- }
- int mid=(l+r)>>;now=id[mid];
- build(l,mid-,mid);build(mid+,r,mid);
- fa[now]=last;pushup(mid);
- if(mid<f)c[last][]=now;
- else c[last][]=now;
- }
- int main()
- {
- scanf("%d%d",&n,&m);
- for(int i=;i<=n+;i++)
- id[i]=++sz;
- build(,n+,);rt=(n+)>>;
- for(int i=;i<=m;i++)
- {
- int l,r;
- scanf("%d%d",&l,&r);
- rever(,l+r-);
- rever(,r);
- rever(r+,l+r-);
- }
- for(int i=;i<=n+;i++)
- printf("%d ",find(rt,i)-);
- return ;
- }
牛客网多校第3场C-shuffle card 平衡树或stl(rope)的更多相关文章
- 牛客网多校第3场Esort string (kmp)
链接:https://www.nowcoder.com/acm/contest/141/E 来源:牛客网 题目描述 Eddy likes to play with string which is a ...
- 牛客网多校赛第九场A-circulant matrix【数论】
链接:https://www.nowcoder.com/acm/contest/147/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...
- 牛客网多校训练第二场D Kth Minimum Clique
链接:https://ac.nowcoder.com/acm/contest/882/D来源:牛客网 Given a vertex-weighted graph with N vertices, fi ...
- 牛客网多校第5场 H subseq 【树状数组+离散化】
题目:戳这里 学习博客:戳这里 题意:给n个数为a1~an,找到字典序第k小的序列,输出该序列所有数所在位置. 解题思路:先把所有序列预处理出来,方法是设一个数组为dp,dp[i]表示以i为开头的序列 ...
- 牛客网多校第5场 I vcd 【树状数组+离散化处理】【非原创】
题目:戳这里 学习博客:戳这里 作者:阿狸是狐狸啦 n个点,一个点集S是好的,当且仅当对于他的每个子集T,存在一个右边无限延长的矩形,使的这个矩形包含了T,但是和S-T没有交集. 求有多少个这种集合. ...
- 牛客网多校第4场 J Hash Function 【思维+并查集建边】
题目链接:戳这里 学习博客:戳这里 题意: 有n个空位,给一个数x,如果x%n位数空的,就把x放上去,如果不是空的,就看(x+1)%n是不是空的. 现在给一个已经放过数的状态,求放数字的顺序.(要求字 ...
- 牛客网多校第4场 A.Ternary String 【欧拉降幂】
题目:戳这里 学习博客:戳这里 欧拉函数的性质: ① N是不为0的整数.φ(1)=1(唯一和1互质的数就是1本身) ② 除了N=2,φ(N)都是偶数. ③ 小于N且与N互质的所有数的和是φ(n)*n/ ...
- 牛客网多校训练第一场 J - Different Integers(树状数组 + 问题转换)
链接: https://www.nowcoder.com/acm/contest/139/J 题意: 给出n个整数的序列a(1≤ai≤n)和q个询问(1≤n,q≤1e5),每个询问包含两个整数L和R( ...
- 牛客网多校训练第一场 I - Substring(后缀数组 + 重复处理)
链接: https://www.nowcoder.com/acm/contest/139/I 题意: 给出一个n(1≤n≤5e4)个字符的字符串s(si ∈ {a,b,c}),求最多可以从n*(n+1 ...
随机推荐
- Asp.net 程序连接orcle如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,
本人使用orcale11g 安装orcale 之类以及navicat配置在这里不提,之后会写一篇文章来说明. 到此已经安装和配置navicat访问数据正常,但是运行Asp.net 程序报错 问题如下 ...
- CentOS 6.8 安装TigerVNC 实现 Linux 远程桌面并安装火狐浏览器
CentOS 6.8 安装TigerVNC 实现 Linux 远程桌面并安装火狐浏览器 vnc客户端地址:https://files.cnblogs.com/files/MYSQLZOUQI/vnc- ...
- jmeter录制https请求
工具:Jmeter4.0 + Java1.8 需求:对某https网站进行 登录-修改信息-退出 场景的压力测试 方法:使用Apache JMeter HTTP(S) Test Script Reco ...
- [LeetCode] 系统刷题2_排列组合
要用到backtracking,是否要跟backtracking放到一起总结? 适用范围: 几乎所有搜索问题 什么时候输出 哪些情况需要跳过 相关题目: [LeetCode] 78. Subsets ...
- 使用git将项目上传到github(最简单方法)
首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路直接安装即可: https://gi ...
- python 爬虫-1
买了本书在自学,我也不知道自己能学到什么地步,反正不用这个找工作,纯属爱好,有可能之后就会放弃 233333333.... 先来一个特别简单点的:将百度搜索主页 扒下来,并保存到一个文件里面 fir ...
- C++ Windows API 读写INI文件
BOOL WritePrivateProfileString( LPCTSTR lpAppName, // INI文件中的一个字段名[节名]可以有很多个节名 LPCTSTR lpKeyName, // ...
- AARRR海盗模型简介
整理下AARRR模型的概念.实际应用场景等问题,初步感觉这个模型主要应用在APP应用分析中. 1.什么是AARRR模型 AARRR是Acquisition.Activation.Retention.R ...
- SpringDataJpa开发环境的搭建以及使用
一.所需工具 安装jdk.IntelliJ IDEA和mysql数据库. 二.SpringDataJpa快速起步 开发环境的搭建: ①.在IDEA软件上添加依赖包:在http://mvnreposit ...
- Shell 变量、脚本参数
定义变量:可将脚本或者多个命令定义成一个变量. ()格式n(){脚本命令}. 脚本常用参数 命令:seq –w 0 23 #以01开头往上的. 命令:echo –ne #输出n换行,e扩展. 命令:b ...