传送门

经典的平衡树问题,之前已经用splay写过一次了,今天我突发奇想,写了一发非旋treap的版本,发现挺好写的(虽然跑不过splay)。

代码:

#include<bits/stdc++.h>
#define N 100005
using namespace std;
typedef pair<int,int> res;
int rt=0,n,m,cnt=0,son[N][2],siz[N],val[N],rd[N],rev[N];
inline int read(){
    int ans=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return ans;
}
inline int build(int v){val[++cnt]=v,rd[cnt]=rand(),siz[cnt]=1,son[cnt][0]=son[cnt][1]=0;return cnt;}
inline void pushup(int p){siz[p]=siz[son[p][0]]+siz[son[p][1]]+1;}
inline void pushdown(int p){
    if(!rev[p])return;
    swap(son[p][0],son[p][1]);
    if(son[p][0])rev[son[p][0]]^=1;
    if(son[p][1])rev[son[p][1]]^=1;
    rev[p]^=1;
}
inline int merge(int a,int b){
    if(!a||!b)return a+b;
    pushdown(a),pushdown(b);
    if(rd[a]<rd[b]){son[a][1]=merge(son[a][1],b),pushup(a);return a;}
    son[b][0]=merge(a,son[b][0]),pushup(b);return b;
}
inline res split(int p,int k){
    if(!p)return res(0,0);
    res ans,tmp;
    pushdown(p);
    if(siz[son[p][0]]>=k){
        tmp=split(son[p][0],k);
        son[p][0]=tmp.second,pushup(p);
        ans.first=tmp.first,ans.second=p;
        return ans;
    }
    tmp=split(son[p][1],k-siz[son[p][0]]-1);
    son[p][1]=tmp.first,pushup(p);
    ans.first=p,ans.second=tmp.second;
    return ans;
}
inline int rank(int p,int k){
    if(!p)return 0;
    if(val[p]>k)return rank(son[p][0],k);
    return rank(son[p][1],k)+siz[son[p][0]]+1;
}
inline int build(int l,int r){
    if(l==r){siz[l]=1,rd[l]=rand(),son[l][0]=son[l][1]=0,val[l]=l;return l;}
    int mid=l+r>>1;
    return merge(build(l,mid),build(mid+1,r));
}
inline void update(int l,int r){
    res x=split(rt,l-1),y=split(x.second,r-l+1);
    rev[y.first]^=1;
    rt=merge(x.first,merge(y.first,y.second));
}
inline void print(int p){
    pushdown(p);
    if(son[p][0])print(son[p][0]);
    cout<<p<<' ';
    if(son[p][1])print(son[p][1]);
}
int main(){
    srand(time(NULL));
    n=read(),m=read();
    rt=build(1,n);
    while(m--){
        int l=read(),r=read();
        update(l,r);
    }
    print(rt);
    return 0;
}

2018.08.05 bzoj3223: Tyvj 1729 文艺平衡树(非旋treap)的更多相关文章

  1. BZOJ3223: Tyvj 1729 文艺平衡树 无旋Treap

    一开始光知道pushdown却忘了pushup......... #include<cstdio> #include<iostream> #include<cstring ...

  2. [BZOJ3223]Tyvj 1729 文艺平衡树

    [BZOJ3223]Tyvj 1729 文艺平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区 ...

  3. BZOJ3223: Tyvj 1729 文艺平衡树 [splay]

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

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

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

  5. 2018.08.06 bzoj1500: [NOI2005]维修数列(非旋treap)

    传送门 平衡树好题. 我仍然是用的fhqtreap,感觉速度还行. 维护也比线段树splay什么的写起来简单. %%%非旋treap大法好. 代码: #include<bits/stdc++.h ...

  6. BZOJ3223——Tyvj 1729 文艺平衡树

    1.题目大意:维护序列,只有区间翻转这个操作 2.分析:splay的经典操作就是实现区间翻转,就是在splay中有一个标记,表示这个区间被翻转了 然后就是记得各种的操作访问某个点时,记得下传,顺便交换 ...

  7. bzoj3223: Tyvj 1729 文艺平衡树 splay裸题

    splay区间翻转即可 /************************************************************** Problem: 3223 User: walf ...

  8. 【Splay】bzoj3223 Tyvj 1729 文艺平衡树

    #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #i ...

  9. 【Splay】【块状链表】bzoj3223 Tyvj 1729 文艺平衡树

    让蒟蒻见识到了常数大+滥用STL的危害. <法一>很久之前的Splay #include<cstdio> #include<algorithm> using nam ...

随机推荐

  1. php缓存类

    <?php /* * 缓存类 cache * 实 例: include( "cache.php" ); $cache = new cache(30); $cache-> ...

  2. VBA 选择文件

    Private Function SelectFile(ByVal strFilter As String) As String    Dim FileName As Variant     '打开文 ...

  3. License 校验

    1:了解keytool 的一些命令  http://www.micmiu.com/lang/java/keytool-start-guide/ 本人在github找的一个demo:https://gi ...

  4. Haskell语言学习笔记(59)Bitraversable

    Bitraversable class (Bifunctor t, Bifoldable t) => Bitraversable t where bitraverse :: Applicativ ...

  5. 使用css技术代替传统的frame技术

    http://www.dynamicdrive.com/style/layouts/item/css-left-frame-layout/ <!--Force IE6 into quirks m ...

  6. 批量导入数据(Mysql)报MySQL server has gone away 问题的解决方法

    问题分析 首先度娘:mysql出现ERROR : (2006, 'MySQL server has gone away') 的问题意思就是指client和MySQL server之间的链接断开了. 造 ...

  7. Javascript 浏览器检测

    推荐 Browser Detecter, 很好用,自己也很容易扩展. 原文链接:http://www.quirksmode.org/js/detect.html <script type=&qu ...

  8. 一文看懂Stacking!(含Python代码)

    一文看懂Stacking!(含Python代码) https://mp.weixin.qq.com/s/faQNTGgBZdZyyZscdhjwUQ

  9. php ip2long 负数问题

    官方网站: Note: 因为PHP的 integer 类型是有符号,并且有许多的IP地址讲导致在32位系统的情况下为负数, 你需要使用 "%u" 进行转换通过 sprintf()  ...

  10. rabbitmq web管理界面 用户管理

    安装最新版本的rabbitmq(3.3.1),并启用management plugin后,使用默认的账号guest登陆管理控制台,却提示登陆失败. 翻看官方的release文档后,得知由于账号gues ...