题意:

依次找第i大的数下标pos[i],然后将区间[i,pos[i]]翻转

分析:

splay树区间翻转

  1. // File Name: ACM/HDU/1890.cpp
  2. // Author: Zlbing
  3. // Created Time: 2013年08月10日 星期六 20时26分39秒
  4.  
  5. #include<iostream>
  6. #include<string>
  7. #include<algorithm>
  8. #include<cstdlib>
  9. #include<cstdio>
  10. #include<set>
  11. #include<map>
  12. #include<vector>
  13. #include<cstring>
  14. #include<stack>
  15. #include<cmath>
  16. #include<queue>
  17. using namespace std;
  18. #define CL(x,v); memset(x,v,sizeof(x));
  19. #define INF 0x3f3f3f3f
  20. #define LL long long
  21. #define REP(i,r,n) for(int i=r;i<=n;i++)
  22. #define RREP(i,n,r) for(int i=n;i>=r;i--)
  23. #define L ch[x][0]
  24. #define R ch[x][1]
  25. #define KT (ch[ ch[rt][1] ][0])
  26. const int MAXN = ;
  27. int cmp(pair<int,int> a,pair<int,int> b){
  28. if(a.first!=b.first) return a.first<b.first;
  29. return a.second < b.second;
  30. }
  31. int mp[MAXN];
  32. int id[MAXN];
  33. pair<int,int> num[MAXN];
  34. struct SplayTree {
  35. int sz[MAXN];
  36. int ch[MAXN][];
  37. int pre[MAXN];
  38. int rt,top;
  39. inline void down(int x){
  40. if(flip[x]) {
  41. flip[ L ] ^= ;
  42. flip[ R ] ^= ;
  43. swap(L,R);
  44. flip[x]=;
  45. }
  46. }
  47. inline void up(int x){
  48. sz[x]=+sz[ L ] + sz[ R ];
  49. }
  50. inline void Rotate(int x,int f){
  51. int y=pre[x];
  52. down(y);
  53. down(x);
  54. ch[y][!f] = ch[x][f];
  55. pre[ ch[x][f] ] = y;
  56. pre[x] = pre[y];
  57. if(pre[x]) ch[ pre[y] ][ ch[pre[y]][] == y ] =x;
  58. ch[x][f] = y;
  59. pre[y] = x;
  60. up(y);
  61. }
  62. inline void Splay(int x,int goal){//将x旋转到goal的下面
  63. down(x);//防止pre[x]就是目标点,下面的循环就进不去了,x的信息就传不下去了
  64. while(pre[x] != goal){
  65. down(pre[pre[x]]); down(pre[x]);down(x);//在旋转之前需要先下传标记,因为节点的位置可能会发生改变
  66. if(pre[pre[x]] == goal) Rotate(x , ch[pre[x]][] == x);
  67. else {
  68. int y=pre[x],z=pre[y];
  69. int f = (ch[z][]==y);
  70. if(ch[y][f] == x) Rotate(x,!f),Rotate(x,f);
  71. else Rotate(y,f),Rotate(x,f);
  72. }
  73. }
  74. up(x);
  75. if(goal==) rt=x;
  76. }
  77. inline void RTO(int k,int goal){//将第k位数旋转到goal的下面
  78. int x=rt;
  79. down(x);
  80. while(sz[ L ]+ != k) {
  81. if(k < sz[ L ] + ) x=L;
  82. else {
  83. k-=(sz[ L ]+);
  84. x = R;
  85. }
  86. down(x);
  87. }
  88. Splay(x,goal);
  89. }
  90. void vist(int x){
  91. if(x){
  92. printf("结点%2d : 左儿子 %2d 右儿子 %2d %2d flip:%d\n",x,L,R,val[x],flip[x]);
  93. vist(L);
  94. vist(R);
  95. }
  96. }
  97. void Newnode(int &x,int c,int f){
  98. x=++top;flip[x]=;
  99. L = R = ; pre[x] = f;
  100. sz[x]=; val[x]=c;
  101. }
  102. inline void build(int &x,int l,int r,int f){
  103. if(l>r) return ;
  104. int m=(l+r)>>;
  105. Newnode(x,num[id[m]].first,f);
  106. mp[id[m]]=x;
  107. build(L , l , m- , x);
  108. build(R , m+ , r , x);
  109. pre[x]=f;
  110. up(x);
  111. }
  112.  
  113. inline void init(){
  114. ch[][]=ch[][]=pre[]=sz[]=;
  115. rt=top=; flip[]=; val[]=;
  116. }
  117. void Del(){
  118. int t=rt;
  119. if(ch[rt][]) {
  120. rt=ch[rt][];
  121. RTO(,);
  122. ch[rt][]=ch[t][];
  123. if(ch[rt][]) pre[ch[rt][]]=rt;
  124. }
  125. else rt=ch[rt][];
  126. pre[rt]=;
  127. up(rt);
  128. }
  129. int flip[MAXN];
  130. int val[MAXN];
  131. }spt;
  132. int main()
  133. {
  134. int n;
  135. while(~scanf("%d",&n))
  136. {
  137. if(!n)break;
  138. spt.init();
  139. REP(i,,n)
  140. {
  141. scanf("%d",&num[i].first);
  142. num[i].second=i;
  143. }
  144. sort(num+,num+n+,cmp);
  145. REP(i,,n)
  146. id[num[i].second]=i;
  147. spt.build(spt.rt,,n,);
  148.  
  149. REP(i,,n)
  150. {
  151. spt.Splay(mp[i],);
  152. int ans=i+spt.sz[spt.ch[spt.rt][]];
  153. spt.flip[spt.ch[spt.rt][]]^=;
  154. spt.Del();
  155. if(i==)printf("%d",ans);
  156. else printf(" %d",ans);
  157. }
  158. printf("\n");
  159. }
  160. return ;
  161. }

hdu-1890-Robotic Sort splay区间翻转的更多相关文章

  1. hdu 1890 Robotic Sort(splay 区间反转+删点)

    题目链接:hdu 1890 Robotic Sort 题意: 给你n个数,每次找到第i小的数的位置,然后输出这个位置,然后将这个位置前面的数翻转一下,然后删除这个数,这样执行n次. 题解: 典型的sp ...

  2. HDU 1890 - Robotic Sort - [splay][区间反转+删除根节点]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 Time Limit: 6000/2000 MS (Java/Others) Memory Li ...

  3. HDU 1890 Robotic Sort | Splay

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Pr ...

  4. hdu1890 Robotic Sort (splay+区间翻转单点更新)

    Problem Description Somewhere deep in the Czech Technical University buildings, there are laboratori ...

  5. 数据结构(Splay平衡树):HDU 1890 Robotic Sort

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. HDU 1890 Robotic Sort (splay tree)

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. HDU 1890 Robotic Sort(splay)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1890 [题意] 给定一个序列,每次将i..P[i]反转,然后输出P[i],P[i]定义为当前数字i ...

  8. hdu 1890 Robotic Sort

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 如下: #include<cstdio> #include<cstdlib&g ...

  9. hdu 1890 Robotic SortI(splay区间旋转操作)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题解:splay又一高级的功能,区间旋转这个是用线段树这些实现不了的,这题可以学习splay的旋 ...

随机推荐

  1. Apache MINA 框架之默认session管理类实现

    DefaultSocketSessionConfig 类 extends AbstractSocketSessionConfig extends AbstractIoSessionConfig imp ...

  2. MVC ViewEngine视图引擎解读及autofac的IOC运用实践

    MVC 三大特色  Model.View.Control ,这次咱们讲视图引擎ViewEngine 1.首先看看IViewEngine接口的定义 namespace System.Web.Mvc { ...

  3. 一次利用MSSQL的SA账户提权获取服务器权限

    遇到小人,把服务器整走了 自己手里只有sql server的sa账号密码 模糊记起之前用这个账户提权读取文件的事 百度之,发现相关信息一堆堆 各种工具也用了不少 发现不是语法错误就是权限不够 无奈之下 ...

  4. java异常类的使用

    1.异常的概念 什么是异常?程序出错分为两部分,编译时出粗和运行时出错.编译时出错是编译器在编译源码时发生的错误: 运行时出错是在编译通过,在运行时出现的错误.这种情况叫异常. 例如:数组越界,除数为 ...

  5. 移动触摸事件(touchstart、touchmove和touchend)

    touchstart事件:当手指触摸屏幕时候触发,即使已经有一个手指放在屏幕上也会触发. touchmove事件:当手指在屏幕上滑动的时候连续地触发.在这个事件发生期间,调用preventDefaul ...

  6. 浅谈angular框架

    最近新接触了一个js框架angular,这个框架有着诸多特性,最为核心的是:MVVM.模块化.自动化双向数据绑定.语义化标签.依赖注入,以上这些全部都是属于angular特性,虽然说它的功能十分的强大 ...

  7. (一)Nodejs - 框架类库 - Nodejs异步流程控制Async

    简介 Async是一个流程控制工具包,提供了直接而强大的异步功能 应用场景 业务流程逻辑复杂,适应异步编程,减少回调的嵌套 安装 npm insatll async 函数介绍 Collections ...

  8. 根据CreateDirectory递归创建多级目录

    分为MFC下的和非MFC下的两种,MFC路径是CString类型的,非MFC的路径是wstring类型的. 下面是MFC下的创建目录: void __fastcall RecursiveDirecto ...

  9. DX笔记之一---Direct3D基础

    一.预备知识 1.表面 表面就是Direct3D用于储存2D图像数据的一个像素矩阵.width和height以像素为单位,pitch以字节单位,用接口IDirect3DSurface来描述表面 Loc ...

  10. 每天一条linux命令——login

    login命令用于给出登录界面,可用于重新登录或者切换用户身份,也可通过它的功能随时更换登入身份.当/etc/nologin文件存在时,系统只root帐号登入系统,其他用户一律不准登入. 语法: lo ...