链接: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

题意:一个1到n的全排列,m次操作,每次操作将一个区间内所有的数全部移到最前面,求最终的序列

  1. 假设当前操作要移动区间[l, r],先将l-1旋到根,r+1旋到根的右儿子
  2. 旋转之后节点r+1的左子树正是区间[l, r],那么只要把这棵子树重新接到rank为1的节点上就可以了
#include<stdio.h>
#include<string.h>
#define inf 1000000000
using namespace std;
int n, m, root, sz, tre[220005][2], fa[220005], deep[220005], a[220005], size[220005], v[220005], pos[220005];
void Update(int k)
{size[k] = size[tre[k][0]]+size[tre[k][1]]+1;
}
void Create(int l, int r, int last)
{int mid;if(l>r)return;mid = (l+r)/2;if(l==r){v[mid] = r;size[mid] = 1;fa[mid] = last;if(mid<last)  tre[last][0] = mid;else  tre[last][1] = mid;return;}Create(l, mid-1, mid);Create(mid+1, r, mid);fa[mid] = last;Update(mid);if(mid<last)  tre[last][0] = mid;else  tre[last][1] = mid;
}
void Rotate(int x, int &k)
{int l, r, y, z;y = fa[x], z = fa[y];if(tre[y][0]==x)  l = 0;else  l = 1;r = l^1;if(y==k)k = x;else{if(tre[z][0]==y)  tre[z][0] = x;else  tre[z][1] = x;}fa[x] = z, fa[y] = x;fa[tre[x][r]] = y;tre[y][l] = tre[x][r];tre[x][r] = y;Update(y);Update(x);
}
void Splay(int x, int &k)
{int y, z;while(x!=k){y = fa[x], z = fa[y];if(y!=k){if((tre[y][0]==x)^(tre[z][0]==y))Rotate(x, k);elseRotate(y, k);}Rotate(x, k);}
}
int Find(int k, int rank)
{int l, r;l = tre[k][0], r = tre[k][1];if(size[l]+1==rank)return k;else if(size[l]>=rank)return Find(l, rank);elsereturn Find(r, rank-size[l]-1);
}
void DEBUG(int k)
{printf("%d %d %d\n", k, tre[k][0], tre[k][1]);if(tre[k][0])DEBUG(tre[k][0]);if(tre[k][1])DEBUG(tre[k][1]);return;
}
void Move(int k)
{Update(k);if(fa[k]!=0)Move(fa[k]);
}
void Turn(int l, int r)
{int x, y, z, p;x = Find(root, l-1);y = Find(root, r+1);Splay(x, root);Splay(y, tre[x][1]);z = tre[y][0];p = Find(root, 1);if(tre[p][1]==0)tre[p][1] = z;else{p = Find(root, 2);tre[p][0] = z;}fa[z] = p;Move(p);tre[y][0] = 0;Move(y);
}
int main(void)
{int x, y, n, i, m;scanf("%d%d", &n, &m);Create(1, n+2, 0);root = (n+3)/2;while(m--){scanf("%d%d", &x, &y);if(x==1)continue;y = x+y-1;Turn(x+1, y+1);}for(i=2;i<=n+1;i++){if(i==2)printf("%d", Find(root, i)-1);elseprintf(" %d", Find(root, i)-1);}puts("");return 0;
}
/*
3 1
2 1
*/

牛客网暑期ACM多校训练营(第三场): C. Shuffle Cards(splay)相关推荐

  1. 牛客网暑期ACM多校训练营(第三场): E. Sort String(KMP)

    链接:https://www.nowcoder.com/acm/contest/141/E 来源:牛客网 题目描述 Eddy likes to play with string which is a ...

  2. 牛客网暑期ACM多校训练营(第三场): A. Ternary String(欧拉降幂+递推)

    题目描述 A ternary string is a sequence of digits, where each digit is either 0, 1, or 2. Chiaki has a t ...

  3. 牛客网暑期ACM多校训练营(第三场): C. Chiaki Sequence Reloaded(数位DP)

    题目描述 Chiaki is interested in an infinite sequence a1, a2, a3, ..., which defined as follows: Chiaki ...

  4. 牛客网暑期ACM多校训练营(第九场)

    牛客网暑期ACM多校训练营(第九场) A. Circulant Matrix 做法:看到下标 \(xor\) 这种情况就想 \(FWT\),可是半天没思路,于是放弃了..其实这个 \(n\) 疯狂暗示 ...

  5. 牛客网暑期ACM多校训练营(第一场)

    牛客网暑期ACM多校训练营(第一场) A. Monotonic Matrix 考虑0和1的分界线,1和2的分界线,发现问题可以转化为两条不互相穿过的路径的方案数(可重叠),题解的做法就是把一条路径斜着 ...

  6. 牛客网暑期ACM多校训练营(第二场): H. travel(树形线头DP)

    链接:https://ac.nowcoder.com/acm/contest/140/H 来源:牛客网 题目描述 White Cloud has a tree with n nodes.The roo ...

  7. 牛客网暑期ACM多校训练营(第二场)A .run

    链接:https://www.nowcoder.com/acm/contest/140/A 来源:牛客网 题目描述 White Cloud is exercising in the playgroun ...

  8. 牛客网暑期ACM多校训练营(第一场) J (莫队算法)

    题目链接:https://www.nowcoder.com/acm/contest/139/J 题目大意:给一个序列,进行q次查询,问1~l和r~n中有多少个不同的数字 题目思路:之前只是听说过莫队算 ...

  9. 牛客网暑期ACM多校训练营(第二场)J farm (二维树状数组)

    题目链接: https://www.nowcoder.com/acm/contest/140/J 思路: 都写在代码注释里了,非常好懂.. for_each函数可以去看一下,遍历起vector数组比较 ...

最新文章

  1. 为什么要读源代码,如何阅读源代码
  2. SAP 科目主数据属性定义
  3. uniapp无法使用substr_SQLite 3.34.0发布,世界上使用量最大的数据库引擎
  4. 如果你也想做实时数仓…
  5. [AWS vs Azure] 云计算里AWS和Azure的探究(2)
  6. SSL/TLS协议信息泄露漏洞(CVE-2016-2183)【原理扫描】 服务器支持 TLS Client-initiated 重协商攻击(CVE-2011-1473)【原理扫描】
  7. 朝阳工程技术学校计算机应用,超8成高职院校开设“计算机应用技术专业”
  8. 人民日报:研究生期间该懂的47件事,你认可吗?
  9. Tensorflow实现CNN
  10. WE出海增长图书馆 | 世界杯豪门面纱下,不容忽视的【增长】沃土
  11. 整数(奇偶)+分数分频器的verilog实现(大合集)
  12. 让虚拟机接入办公网络
  13. 华为云服务器安装Linux并实现本地连接访问
  14. php 黄页,PHPCMS企业黄页
  15. 微信扫一扫功能扫描二维码调用外部浏览器打开指定页面实现微信中下载APP的功能
  16. vmtools官方下载地址
  17. 语音标注的具体应用场景
  18. CRM哪家好?这5个CRM管理系统很好用!
  19. 行业分析-全球与中国AI支援X光影像解决方案市场现状及未来发展趋势
  20. 2022年通信专业技术人员职业水平考试

热门文章

  1. 自学python能干些什么副业-学会Python有哪些可以做的兼职?
  2. python3下载-python下载 v3.7.0 官方正式版
  3. 聚焦2016:关于语音识别、图像识别及大数据
  4. 搜狗王小川:搜狗的语音识别比阿里和科大讯飞的好
  5. win8.1计算机开启远程桌面连接不上,Win8.1电脑远程桌面无法连接提示“你的凭据不工作”怎么办...
  6. vscode npm install下载权限问题解决
  7. 自定义vue中的验证码组件
  8. 【java笔记】包装类
  9. 2020PHP版本,phpmaker2020
  10. jQuery 学习-DOM篇(三):jQuery 在 DOM 外部插入元素