这题的建模有点不太一样,是按结点横坐标赋予键值的

同时每次rotate和splay时都要注意下往上往下更新

/*
先建立好splay tree,将结点按num/输入顺序排序,遍历时每次将当前结点提到根节点,输出其在splay树中排第几个
然后rev左子树,最后remove
*/
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#include<algorithm>
#define maxn 100005
struct A{
int num,id;
bool operator < (const A &a) const {
if(num==a.num) return id<a.id;
return num<a.num;
}
}a[maxn];
int pre[maxn],ch[maxn][],size[maxn],keys[maxn],tot,root,n;
int rev[maxn];
inline void pushup(int r){
size[r]=size[ch[r][]]+size[ch[r][]]+;
}
void update(int r){//就是交换r的两个子区间
if(!r) return;
else {
swap(ch[r][],ch[r][]);
rev[r]^=;
}
}
inline void pushdown(int r){
if(rev[r]){//把左右子树rev
update(ch[r][]);
update(ch[r][]);
rev[r]=;
}
}
inline void newnode(int &r,int fa,int key){
r=key;
pre[r]=fa;
ch[r][]=ch[r][]=;
size[r]=;
keys[r]=key;
rev[r]=;
}
void build(int &r,int L,int R,int fa){
if(L>R) return;
int m=L+R>>;
newnode(r,fa,m);
build(ch[r][],L,m-,r);
build(ch[r][],m+,R,r);
pushup(r);
}
void init(){
root=tot=;
ch[root][]=ch[root][]=;
size[root]=;
rev[root]=;
build(root,,n,);
}
//rotate操作只会改变r,fa,ch[r][kind]三个结点
void rotate(int x,int kind){
int fa=pre[x];
pushdown(fa);
pushdown(x);
ch[fa][!kind]=ch[x][kind];
pre[ch[x][kind]]=fa;
if(pre[fa])
ch[pre[fa]][ch[pre[fa]][]==fa]=x;
pre[x]=pre[fa];
pre[fa]=x;
ch[x][kind]=fa;
pushup(fa);
pushup(x);
}
void splay(int r,int goal){
pushdown(r);
while(pre[r]!=goal){
if(pre[pre[r]]==goal){
pushdown(pre[r]);
pushdown(r);
rotate(r,ch[pre[r]][]==r);
}
else {
pushdown(pre[pre[r]]);
pushdown(pre[r]);
pushdown(r);
int fa=pre[r];
int kind=(ch[pre[fa]][]==fa);
if(ch[fa][kind]==r){//方向相反
rotate(r,!kind);
rotate(r,kind);
}
else {
rotate(fa,kind);
rotate(r,kind);
}
}
}
if(goal==) root=r;
pushup(r);
}
void remove(){//删掉根节点
if(ch[root][]==){
root=ch[root][];
pre[root]=;
}
else {
pushdown(root);
int tmp=ch[root][];
while(ch[tmp][])
pushdown(tmp),tmp=ch[tmp][];
splay(tmp,root);
ch[tmp][]=ch[root][];
pre[ch[root][]]=tmp;
root=tmp;
pre[root]=;
pushup(root);
}
} int main(){
while(scanf("%d",&n) && n){
init();
for(int i=;i<=n;i++)
scanf("%d",&a[i].num),a[i].id=i;
sort(a+,a++n);
for(int i=;i<n;i++){
splay(a[i].id,);//按权值大小遍历点
update(ch[root][]);//逆转区间
printf("%d ",i+size[ch[root][]]);//输出结点在当前伸展树中排的序号
remove();
}
printf("%d\n",n);
}
return ;
}

hdu1890 splay维护区间翻转的更多相关文章

  1. bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2202  Solved: 1226[Submit][Sta ...

  2. BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6881  Solved: 4213[Submit][Sta ...

  3. 「Splay」区间翻转

    传送门:>Here< 解法分析 用splay来维护这个序列. 一直没有搞明白的是,这里的splay的节点究竟维护的是什么?是权值吗?肯定不是,因为区间是会翻转的,如果维护权值的话很快平衡树 ...

  4. 【模板】文艺平衡树(Splay) 区间翻转 BZOJ 3223

    您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 N,M<= ...

  5. 文艺平衡树 lg3391(splay维护区间入门)

    splay是支持区间操作的,先做这道题入个门 大多数操作都和普通splay一样,就不多解释了,只解释一下不大一样的操作 #include<bits/stdc++.h> using name ...

  6. splay(1区间翻转区间最值与区间修改)

    bzoj1251权限题 题目点这里,你懂得 直接上板子,这个要好好体会 操作是最经典的. #include <algorithm> #include <iostream> #i ...

  7. Splay(区间翻转) 模板

    洛谷:P3391 [模板]文艺平衡树(Splay) #include<cstdio> #include<iostream> #include<algorithm> ...

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

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

  9. hdu-1890-Robotic Sort splay区间翻转

    题意: 依次找第i大的数下标pos[i],然后将区间[i,pos[i]]翻转 分析: splay树区间翻转 // File Name: ACM/HDU/1890.cpp // Author: Zlbi ...

随机推荐

  1. php简单一句话分析

    <?php $arr="j{fq-)dUTXY`}b.@"; ;$i< strlen($arr);$i++){ $arr[$i]=chr(ord($arr[$i])-) ...

  2. selenium_基本用法

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...

  3. Scala进阶之路-并发编程模型Akka入门篇

    Scala进阶之路-并发编程模型Akka入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Akka Actor介绍 1>.Akka介绍 写并发程序很难.程序员不得不处 ...

  4. VNC 在ubuntu desktop下只显示空白桌面

    看不到上下的菜单栏,但是有桌面.要么是配置文件,要么是gnome缺组件. 1.先安装组件 apt-get install --no-install-recommends ubuntu-desktop ...

  5. Swift学习笔记9--错误控制

    1.Swift 中有4种处理错误的方式.你可以把函数抛出的错误传递给调用此函数的代码.用do-catch语句处理错误.将错误作为可选类型处理.或者断言此错误根本不会发生. 2.wift 中的错误处理并 ...

  6. 机器学习:Python实现聚类算法(二)之AP算法

    1.算法简介 AP(Affinity Propagation)通常被翻译为近邻传播算法或者亲和力传播算法,是在2007年的Science杂志上提出的一种新的聚类算法.AP算法的基本思想是将全部数据点都 ...

  7. git删除仓库的某个文件

    可以用git rm命令删除文件(删除远程仓库文件) git clone 仓库地址 git add . git rm 文件//本地中该文件会被删除 git rm -r文件夹 //删除文件夹 上面会把对应 ...

  8. vue.js初始学习笔记&vue-cli

    笔记一下: vue.js 安装,参考: http://www.cnblogs.com/wisewrong/p/6255817.html (vue-cli) http://www.cnblogs.com ...

  9. vue自学入门-1(Windows下搭建vue环境)

    本人是一个喜欢动手的程序员,先跑起来个HelloWorld,增加感性认识,这三篇入门文章,花了不到一个小时,从网上找资料,程序跑通后,整理出来的,有的新人可能去哪找资料,运行代码都不知道,分享出来,大 ...

  10. 尚硅谷spring_boot课堂笔记

    尚硅谷spring_boot课堂笔记