「luogu2569」[ZJOI2006]书架

题目大意

给定一个长度为 \(n\) 序列,序列中第 \(i\) 个元素有编号 \(a_i(a_i \in \Z \cap [1,n])\),需要支持五种操作:

  1. \(Top\) \(S\) ——表示把编号为 \(S\) 的书放在最上面;
  2. \(Bottom\) \(S\)——表示把编号为 \(S\) 的书放在最下面;
  3. \(Insert\) \(S\) \(T\)——\(T \in \{-1,0,1\}\),若编号为 \(S\) 的书上面有 \(X\) 本书,则这条命令表示把这本书放回去后它的上面有 \(X+T\) 本书;
  4. \(Ask\) \(S\)——询问编号为 \(S\) 的书的上面目前有多少本书;
  5. \(Query\) \(S\)——询问从上面数起的第 \(S\) 本书的编号。

对于每个 \(Ask\),\(Query\) 操作,输出答案。

(以上摘自luogu)

题解

\(fhq\_treap\) 复健,没有题解。

代码如下:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <cstdlib>
  5. inline int in() {
  6. int x=0;char c=getchar();bool f=false;
  7. while(c<'0'||c>'9') f|=c=='-', c=getchar();
  8. while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48), c=getchar();
  9. return f?-x:x;
  10. }
  11. const int N = 8e4+5;
  12. int mp[N];
  13. struct fhq_treap {
  14. #define t a[p]
  15. #define lson a[a[p].l]
  16. #define rson a[a[p].r]
  17. struct node {
  18. int l, r, key, size, fa, rnd;
  19. }a[N];
  20. int tot, rt;
  21. inline int new_node(int key) {
  22. a[++tot]=(node){0, 0, key, 1, 0, rand()};
  23. mp[key]=tot;
  24. return tot;
  25. }
  26. inline void push_up(int p) {
  27. if(p) {
  28. t.size=lson.size+rson.size+1;
  29. lson.fa=rson.fa=p;
  30. }
  31. }
  32. void split(int p, int &x, int &y, int k) {
  33. if(!p) return (void)(x=y=0);
  34. if(lson.size<k) x=p, split(t.r, t.r, y, k-lson.size-1);
  35. else y=p, split(t.l, x, t.l, k);
  36. push_up(p);
  37. }
  38. int merge(int x, int y) {
  39. if(!x||!y) return x?x:y;
  40. int p=0;
  41. if(a[x].rnd<a[y].rnd) p=x, t.r=merge(t.r, y);
  42. else p=y, t.l=merge(x, t.l);
  43. push_up(p);
  44. return p;
  45. }
  46. inline int kth_id(int k) {
  47. int p=rt;
  48. while(lson.size+1!=k)
  49. if(lson.size>=k) p=t.l;
  50. else k-=lson.size+1, p=t.r;
  51. return a[p].key;
  52. }
  53. inline int find_rank(int p) {
  54. int ret=lson.size+1;
  55. for(;p!=rt;p=t.fa)
  56. if(a[t.fa].r==p) ret+=a[a[t.fa].l].size+1;
  57. return ret;
  58. }
  59. void debug(int p) {
  60. if(t.l) debug(t.l);
  61. printf(" %d", a[p].key);
  62. if(t.r) debug(t.r);
  63. }
  64. #undef t
  65. #undef lson
  66. #undef rson
  67. }T;
  68. int main() {
  69. srand(20021111);
  70. char opt[10];
  71. int n=in(), m=in();
  72. for(int i=1;i<=n;++i) T.rt=T.merge(T.rt, T.new_node(in()));
  73. int a, b, c, d, s, t, k;
  74. while(m--) {
  75. scanf("%s", opt), s=in();
  76. if(opt[0]=='T') {
  77. k=T.find_rank(mp[s]);
  78. T.split(T.rt, a, b, k);
  79. T.split(a, a, c, k-1);
  80. T.rt=T.merge(T.merge(c, a), b);
  81. }
  82. else if(opt[0]=='B') {
  83. k=T.find_rank(mp[s]);
  84. T.split(T.rt, a, b, k);
  85. T.split(a, a, c, k-1);
  86. T.rt=T.merge(a, T.merge(b, c));
  87. }
  88. else if(opt[0]=='I') {
  89. t=in();
  90. k=T.find_rank(mp[s]);
  91. if(t>0) {
  92. T.split(T.rt, a, b, k);
  93. T.split(a, a, c, k-1);
  94. T.split(b, b, d, k-T.a[a].size-T.a[c].size+1);
  95. T.rt=T.merge(T.merge(a, b), T.merge(c, d));
  96. }
  97. else if(t<0) {
  98. T.split(T.rt, a, b, k-1);
  99. T.split(a, a, c, k-2);
  100. T.split(b, b, d, k-T.a[a].size-T.a[c].size);
  101. T.rt=T.merge(T.merge(a, b), T.merge(c, d));
  102. }
  103. }
  104. else if(opt[0]=='A') printf("%d\n", T.find_rank(mp[s])-1);
  105. else printf("%d\n", T.kth_id(s));
  106. //T.debug(T.rt);
  107. //putchar('\n');
  108. }
  109. return 0;
  110. }

「luogu2569」[ZJOI2006] 书架的更多相关文章

  1. 嘴巴题5 「BZOJ1864」[ZJOI2006] 三色二叉树

    1864: [Zjoi2006]三色二叉树 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1195 Solved: 882 [Submit][Status ...

  2. 「bzoj1003」「ZJOI2006」物流运输 最短路+区间dp

    「bzoj1003」「ZJOI2006」物流运输---------------------------------------------------------------------------- ...

  3. 「MoreThanJava」Java发展史及起航新世界

    「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...

  4. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  5. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  6. JavaScript OOP 之「创建对象」

    工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...

  7. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  8. 「JavaScript」四种跨域方式详解

    超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...

  9. 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management

    写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...

随机推荐

  1. iOS 打包.framework(包括第三方、图片、xib、plist文件)详细步骤及需要注意的地方

    https://www.cnblogs.com/yk123/p/9340268.html // 加载自定义名称为Resources.bundle中对应images文件夹中的图片// 思路:从mainb ...

  2. BSScrollViewEdgePop

    https://blog.csdn.net/qq_17190231/article/details/84201956 2018年11月18日 16:52:39 FreeBaiShun 阅读数:66 标 ...

  3. 算法01 C语言设计

    8.21 #include <stdio.h> void bubbleSort(int **p, int n); int main(void){ int a[100]; int *b[10 ...

  4. 腾讯云服务器哪个地区节点好?来ping一下速度就知道了

    腾讯云服务器怎么样?速度快吗?相信很多网友在购买云服务器之前都有此疑惑. 本人找了很久也没找到各区域节点的测试速度的域名和IP.即使有也不能批量查看对比哪个区域的速度. 网上都说这个区域节点速度快,那 ...

  5. Apache No installed service named "Apache2.4"的解决办法

    windows安装Apache后,用cmd开启apache服务时,提示No installed service named "Apache2.4" 解决步骤: 1.cmd窗口,进入 ...

  6. Jetson TX1 SD card启动

    上网DNS /var/run/NetworkManager/resolv.conf nameserver 211.100.225.34 nameserver 219.239.26.42

  7. Go语言中Loop的注意点

    Go语言和其他语言不一样,它只有一种循环方式,就是for语句 可以参考如下公式: for initialisation; condition; post{ //Do Something } 执行顺序 ...

  8. Comet OJ - Contest #1

    A:随便怎么暴力. #include<bits/stdc++.h> using namespace std; #define ll long long #define N 25 char ...

  9. django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

    异常汇总:https://www.cnblogs.com/dotnetcrazy/p/9192089.html 这个是Django对MySQLdb版本的限制,我们使用的是PyMySQL,所以不用管它 ...

  10. logback输出json格式日志(包括mdc)发送到kafka

    1,pom.xml <!-- kafka --> <dependency> <groupId>com.github.danielwegener</groupI ...