传送门

经典的平衡树问题,之前已经用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. requests bs4 爬取 资讯 图片

    #!/usr/bin/env python # Version = 3.5.2 # __auth__ = '无名小妖' import requests from bs4 import Beautifu ...

  2. Spring MVC 自定义视图

    实现View import org.springframework.stereotype.Component; import org.springframework.web.servlet.View; ...

  3. Linux&Unix命令

    Linux下: 系统操作 文件操作 防火墙 权限管理 压缩和解压 安装应用 用户管理 端口 PID 应用 start & shutdown 远程操作 异常 注意点: linux系统下内容大多用 ...

  4. ABAP-年月期间搜索帮助

    selection-screen begin of block block1 with frame title text-. parameters:p_mon1 like s031-spmon, p_ ...

  5. VCL编写笔记整理

    unit hzqEdit1; interface uses  SysUtils, Classes, Controls, StdCtrls; type  TEditDataType = (dtpStri ...

  6. 3.3 JSP内置对象概述

    1.request 1.1 request获取页面传来的参数,参数通过浏览器网址和后面添加?的方式传达. 传参:”show.jsp?id=001” 获取参数:request.getParameter( ...

  7. 根据条件决定My97DatePicker日期控件弹出的日期格式

    代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...

  8. docker 入門

    http://dockone.io/article/277 我的碎碎念:Docker入门指南 [编者的话]之前曾经翻译过很多Docker入门介绍的文章,之所以再翻译这篇,是因为Anders的角度很独特 ...

  9. java script sleep synchronous

    function sleep(milliseconds) { var start = new Date().getTime(); for (var i = 0; i < 1e7; i++) { ...

  10. Spring/AOP框架, 以及使用注解

    1, 使用代理增加日志, 也是基于最原始的办法 import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; ...