【BZOJ3506】排序机械臂(Splay)

题面

神TMBZOJ没有题面,感谢SYC的题面

洛谷的题面也不错

题解

对于每次旋转的物体

显然可以预处理出来

现在只要模拟旋转操作就行了

至于在哪里放标记的问题

我只在第K大放会鬼。。

所以在Splay里面也放了一次(和LCT一样的)

然而我每次都把排到了正确位置的元素直接给删掉了。。。

所以跑的很慢很慢。。。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<queue>
  11. using namespace std;
  12. #define MAX 120000
  13. #define lson (t[x].ch[0])
  14. #define rson (t[x].ch[1])
  15. inline int read()
  16. {
  17. int x=0,t=1;char ch=getchar();
  18. while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
  19. if(ch=='-')t=-1,ch=getchar();
  20. while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
  21. return x*t;
  22. }
  23. struct Node
  24. {
  25. int ch[2],ff;
  26. int v,size;
  27. int rev;
  28. }t[MAX];
  29. int root,n;
  30. int S[MAX],top;
  31. void pushup(int x)
  32. {
  33. if(!x)return;
  34. t[x].size=t[lson].size+t[rson].size+1;
  35. }
  36. void rotate(int x)
  37. {
  38. int y=t[x].ff,z=t[y].ff;
  39. int k=t[y].ch[1]==x;
  40. if(z)t[z].ch[t[z].ch[1]==y]=x;t[x].ff=z;
  41. t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
  42. t[x].ch[k^1]=y;t[y].ff=x;
  43. pushup(y);pushup(x);
  44. }
  45. void putrev(int x)
  46. {
  47. swap(lson,rson);
  48. t[x].rev^=1;
  49. }
  50. void pushdown(int x)
  51. {
  52. if(!t[x].rev)return;
  53. t[x].rev^=1;
  54. if(lson)putrev(lson);
  55. if(rson)putrev(rson);
  56. }
  57. void Splay(int x,int goal)
  58. {
  59. S[top=1]=x;
  60. for(int i=x;i!=root;i=t[i].ff)S[++top]=t[i].ff;
  61. while(top)pushdown(S[top--]);
  62. while(t[x].ff!=goal)
  63. {
  64. int y=t[x].ff,z=t[y].ff;
  65. if(z!=goal)
  66. (t[y].ch[1]==x)^(t[z].ch[1]==y)?rotate(x):rotate(y);
  67. rotate(x);
  68. }
  69. if(!goal)root=x;
  70. }
  71. int Build(int l,int r)
  72. {
  73. if(l>r)return 0;
  74. int mid=(l+r)>>1;
  75. t[mid].ch[0]=Build(l,mid-1);
  76. t[mid].ch[1]=Build(mid+1,r);
  77. t[t[mid].ch[0]].ff=t[t[mid].ch[1]].ff=mid;
  78. pushup(mid);
  79. return mid;
  80. }
  81. int Kth(int k)
  82. {
  83. int x=root;
  84. while(233)
  85. {
  86. if(t[x].rev)pushdown(x);
  87. if(t[lson].size+1>=k)
  88. {
  89. if(t[lson].size+1==k)return x;
  90. else x=lson;
  91. }
  92. else k-=t[lson].size+1,x=rson;
  93. }
  94. return 0;
  95. }
  96. int Rank(int x)//查找x的排名
  97. {
  98. Splay(x,0);
  99. return t[lson].size+1;
  100. }
  101. void Delete(int x)//删除节点x
  102. {
  103. int tt=Rank(x);
  104. int L=Kth(tt-1);
  105. int R=Kth(tt+1);
  106. Splay(L,0);Splay(R,L);
  107. t[R].ch[0]=0;
  108. pushup(R);pushup(L);
  109. }
  110. void Outp(int x)
  111. {
  112. if(lson)Outp(lson);
  113. printf("%d ",t[x].v);
  114. if(rson)Outp(rson);
  115. }
  116. pair<int,int> w[MAX];
  117. int main()
  118. {
  119. t[0].v=1e9;
  120. n=read();
  121. for(int i=2;i<=n+1;++i)t[i].v=read(),w[i-1]=make_pair(t[i].v,i);
  122. sort(&w[1],&w[n+1]);
  123. t[1].v=t[n+2].v=1e9;
  124. root=Build(1,n+2);
  125. for(int i=1;i<=n;++i)
  126. {
  127. int pp=w[i].second;
  128. int gg=Rank(pp);
  129. printf("%d ",gg-2+i);
  130. Splay(1,0);
  131. Splay(pp,1);
  132. if(t[pp].ch[0])putrev(t[pp].ch[0]);
  133. Delete(pp);
  134. }
  135. return 0;
  136. }
  137. /*
  138. 9
  139. 1 3 2 3 3 2 1 2 1
  140. */

【BZOJ3506】排序机械臂(Splay)的更多相关文章

  1. [BZOJ3506] [Cqoi2014] 排序机械臂 (splay)

    Description 同OJ1552 Input Output Sample Input Sample Output HINT Source Solution Q:哎不是同一道题吗为什么分两篇博客来 ...

  2. 【BZOJ-1552&3506】robotic sort&排序机械臂 Splay

    1552: [Cerc2007]robotic sort Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 806  Solved: 329[Submit][ ...

  3. 洛谷P3165 [CQOI2014]排序机械臂 Splay维护区间最小值

    可以将高度定义为小数,这样就完美的解决了优先级的问题. Code: #include<cstdio> #include<algorithm> #include<cstri ...

  4. 【BZOJ3506】[CQOI2014] 排序机械臂(Splay)

    点此看题面 大致题意: 给你\(n\)个数.第一次找到最小值所在位置\(P_1\),翻转\([1,P_1]\),第二次找到剩余数中最小值所在位置\(P_2\),翻转\([2,P_2]\),以此类推.求 ...

  5. 刷题总结:排序机械臂(石室中学oj)(splay)

    题目: 题目描述 为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂.它遵循一个简单的排序规则,第一次操作找到最低的物品位置 P1,并把从左起第 1 个至第 P1 个之间的物品反序 ...

  6. P3165 [CQOI2014]排序机械臂

    题目描述 为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂.它遵循一个简单的排序规则,第一次操作找到高度最低的物品的位置 P1P_1P1​ ,并把左起第一个物品至 P1P_1P1 ...

  7. LibreOJ2241 - 「CQOI2014」排序机械臂

    Portal Description 给出一个\(n(n\leq10^5)\)个数的序列\(\{a_n\}\),对该序列进行\(n\)次操作.若在第\(i\)次操作前第\(i\)小的数在\(p_i\) ...

  8. [bzoj1552\bzoj2506][Cqoi2014]robotic sort 排序机械臂_非旋转Treap

    robotic sort 排序机械臂 bzoj-1552 bzoj-2506 Cqoi-2014 题目大意:给定一个序列,让你从1到n,每次将[1,p[i]]这段区间反转,p[i]表示整个物品权值第i ...

  9. 洛谷P3165 [CQOI2014]排序机械臂

    题目描述 为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂.它遵循一个简单的排序规则,第一次操作找到摄低的物品的位置P1,并把左起第一个至P1间的物品反序:第二次找到第二低的物品 ...

随机推荐

  1. 一个.net专业户转Spring Boot V2.0开发的体会

    java web的idea开发工具总体用起来还是比vs差很多,但是在使用Hibernate跟MyBatis的感触,Hibernate有着.net core ef没有的细腻,Hibernate在细节上完 ...

  2. Cookie禁用 获取session

    转自:http://blog.csdn.net/u010433704/article/details/40950599 Cookie与 Session,一般认为是两个独立的东西,Session采用的是 ...

  3. fifteen-puzzle

    http://www.math.ubc.ca/~cass/courses/m308-02b/projects/grant/fifteen.html http://jamie-wong.com/2011 ...

  4. MySQL5.7 group by新特性,报错1055

    项目中本来使用的是mysql5.6进行开发,切换到5.7之后,突然发现原来的一些sql运行都报错,错误编码1055,错误信息和sql_mode中的"only_full_group_by&qu ...

  5. 洛谷P1345 [USACO5.4]奶牛的电信Telecowmunication【最小割】分析+题解代码

    洛谷P1345 [USACO5.4]奶牛的电信Telecowmunication[最小割]分析+题解代码 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流. ...

  6. Python自动化--语言基础3--字典、函数、全局/局部变量

    字典 dict1 = {'name':'han','age':18,'class':'first'} print(dict1.keys()) #打印所有的key值 print(dict1.values ...

  7. JS原生Ajax&Jquery的Ajax技术&Json

    1.介绍Ajax Ajax = 异步 JavaScript 和 XML Ajax是一种创建快速动态网页的技术 通过在后台与服务器进行少量数据交换,Ajax 可以使网页实现异步更新.这意味着可以不用整个 ...

  8. 使用Ajax发送http请求(get&post请求)

    本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. 同步和异步 同步和异步的概念 同步:必须等待前面的任务完成,才能继续后面 ...

  9. 一种解决eclipse中安装maven出错的方法

    1.安装步骤:https://jingyan.baidu.com/article/a17d5285feb4dd8099c8f26e.html 2.安装第三步的解决办法:m2e   路径换成  http ...

  10. centos/linux下的安装mysql

    1.从yum 下面下载mysql数据库 yum -y install mysql-server 2.查询该mysql是否安装完成 rpm -qa|grep mysql-server 出现如下图所示标明 ...