题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890

题解:splay又一高级的功能,区间旋转这个是用线段树这些实现不了的,这题可以学习splay的旋转方法还有splay tree是按照中序来的,也就是说中序遍历后会得到原序列所以建树和线段树差不多稍微有点不一样。其实splay tree核心操作就是splay就是将某个点移到goal下面优化bst的操作。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int M = 1e5 + ;
int pre[M] , ch[M][] , size[M] , rev[M] , tot , root;//rev类似懒惰标记表示该区间是否要旋转
void NewNode(int &r , int fa , int k) {
r = k;
pre[r] = fa;
ch[r][] = ch[r][] = ;
size[r] = ;
rev[r] = ;
}
void Rev(int r) {
if(!r) return ;
else {
swap(ch[r][] , ch[r][]);
rev[r] ^= ;
}
}//新的操作区间旋转
void push_up(int r) {
size[r] = size[ch[r][]] + size[ch[r][]] + ;
}
void push_down(int r) {
if(rev[r]) {
Rev(ch[r][]);
Rev(ch[r][]);
rev[r] = ;
}
}//新操作的push_down;
void Rotate(int x , int kind) {
int y = pre[x];
push_down(y);
push_down(x);
ch[y][!kind] = ch[x][kind];
pre[ch[x][kind]] = y;
if(pre[y]) ch[pre[y]][ch[pre[y]][] == y] = x;
pre[x] = pre[y];
ch[x][kind] = y;
pre[y] = x;
push_up(y);
push_up(x);
}
void Splay(int r , int goal) {
push_down(r);
while(pre[r] != goal) {
if(pre[pre[r]] == goal) {
push_down(r);
push_down(pre[r]);
Rotate(r , ch[pre[r]][] == r);
}
else {
push_down(r);
push_down(pre[r]);
push_down(pre[pre[r]]);
int y = pre[r];
int kind = (ch[pre[y]][] == y);
if(ch[y][kind] == y) {
Rotate(r , !kind);
Rotate(r , kind);
}
else {
Rotate(y , kind);
Rotate(r , kind);
}
}
}
push_up(r);
if(goal == ) root = r;
}//有新的操作之后注意稍微修改一下旋转操作
int get_Max(int r) {
push_down(r);
while(ch[r][]) {
r = ch[r][];
push_down(r);
}
push_up(r);
return r;
}
void Remove() {
if(!ch[root][]) {
root = ch[root][];
pre[root] = ;
push_up(root);
}
else {
int Max_point = get_Max(ch[root][]);
Splay(Max_point , root);
ch[Max_point][] = ch[root][];
pre[ch[root][]] = Max_point;
root = Max_point;
pre[root] = ;
push_up(root);
}
}//新的remove操作删除节点。
void build(int l , int r , int &x , int fa) {
if(l > r) return ;
int mid = (l + r) >> ;
NewNode(x , fa , mid);
build(l , mid - , ch[x][] , x);
build(mid + , r , ch[x][] , x);
push_up(x);
}
struct TnT {
int pos , val;
}a[M << ];
bool cmp(TnT x , TnT y) {
if(x.val == y.val) return x.pos < y.pos;
return x.val < y.val;
}
int main() {
int n;
while(scanf("%d" , &n) , n) {
for(int i = ; i < n ; i++) {
scanf("%d" , &a[i].val);
a[i].pos = i + ;
}
sort(a , a + n , cmp);
root = tot = ;
pre[root] = ch[root][] = ch[root][] = size[root] = rev[root] = ;
build( , n , root , );
for(int i = ; i < n - ; i++) {
Splay(a[i].pos , );
Rev(ch[root][]);
printf("%d " , size[ch[root][]] + i + );
Remove();
}
printf("%d\n" , n);
}
return ;
}

hdu 1890 Robotic SortI(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. HDU 1890 Robotic Sort (splay tree)

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

  5. splay tree旋转操作 hdu 1890

    很神奇的旋转操作. 目前没看到其他数据结构能实现这个功能.平衡树不好处理区间操作,线段树很难旋转.splay tree搞这个就很简单了. 下面用的这个模板跑了700ms,好慢,估计是删除操作太费时了, ...

  6. HDU 1890 Robotic Sort(splay)

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

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

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

  8. hdu 1890 Robotic Sort

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

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

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

随机推荐

  1. ubuntu下借助qt creator创建属于自己的共享库

    简介: 在 Windows 上,共享库由 .dll 表示:在 Linux 上,由 .so 表示. Shared Library的优势 共享库,又称动态库或so文件,顾名思义,它可以在可执行文件启动时加 ...

  2. 计算机网络中IP地址和MAC地址

    计算机 网络中的网络地址有I P 地址和物理地址之分,对 于主机间的通信时,它们的作用也不一样 . l   I P 地址 为 了保证 I n t e r n e t 网上主机通信时能够相互识别 ,不引 ...

  3. [Chat]实战:仿网易云课堂微信小程序开发核心技术剖析和经验分享

    本Chat以一个我参与开发并已上线运营近2年——类似网易云课堂的微信小程序项目,来进行微信小程序高级开发的学习. 本场Chat围绕项目开发核心技术分析,帮助你快速掌握在线视频.音频类小程序开发所需要的 ...

  4. Java | Map排序,工具类改进

    package util; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; ...

  5. Template(模板)模式

    第三章:模板模式 Template模式比较简单,是基于继承关系的一种设计模式,由父类定义处理流程框架,由子类中实现具体处理. Code: package example.template; /*** ...

  6. JDK、JRE、JVM之间的区别和联系

    JDK : Java Development ToolKit(Java开发工具包).JDK是整个JAVA的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工 ...

  7. windows环境composer install失败的解决办法

    报错信息:[Composer\Downloader\TransportException] The "https://repo.packagist.org/p/doctrine/inflec ...

  8. CodeForces 29D Ant on the Tree

    洛谷题目页面传送门 & CodeForces题目页面传送门 题意见洛谷里的翻译. 这题有\(\bm3\)种解法,但只有一种是正解(这不是废话嘛). 方法\(\bm1\):最近公共祖先LCA(正 ...

  9. The 3n + 1 problem UVA - 100

    3n+1问题 PC/UVa IDs: 110101/100 Popularity: A Success rate: low Level: 1 测试地址: https://vjudge.net/prob ...

  10. idea(java)实用开发插件

    Idea常用的插件: mybatisX,      ----------------  (Alt + enter) codeGlace, Lombok, sonarlint, translation, ...