这道题要求区间反转。。。好东西。。

对于Splay:把l-1旋到根,把r+1旋到根的右儿子,这样r+1的左儿子就是整个区间了,然后对这个区间打个tg

注意要插-Inf和Inf到树里面,防止越界,坐标要+1

#include<cstdio>
#include<iostream>
#define R register int
using namespace std;
const int N=,Inf=0x3f3f3f3f;
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
}
int n,m,tot,rt;
struct node{
int fa,ch[],sz,tg,vl;
#define fa(x) t[x].fa
#define ch(x,i) t[x].ch[i]
#define sz(x) t[x].sz
#define tg(x) t[x].tg
#define vl(x) t[x].vl
#define ls ch(x,0)
#define rs ch(x,1)
}t[N];
inline void upd(int x) {sz(x)=sz(ls)+sz(rs)+;}
inline void spread(int x) {if(tg(x)) tg(ls)^=,tg(rs)^=,tg(x)=,swap(ls,rs);}
inline void rot(int x) {
R y=fa(x),d=ch(y,)==x;
if(fa(y)) ch(fa(y),ch(fa(y),)==y)=x;
fa(x)=fa(y); fa(ch(y,d)=ch(x,d^))=y;
fa(ch(x,d^)=y)=x; upd(y);
}
inline void Splay(int x,int f) {
while(fa(x)!=f) {
R y=fa(x); if(fa(y)!=f)
rot((ch(y,)==x)==(ch(fa(y),)==y)?y:x);
rot(x);
} upd(x); if(!f) rt=x;
}
inline int build(int f,int l,int r) {
if(l>r) return ; R md=l+r>>,x=++tot;
fa(x)=f,++sz(x),vl(x)=md-;
ls=build(x,l,md-);rs=build(x,md+,r);
upd(x); return x;
}
inline int get(int pos) { R x=rt;
while() {spread(x); R s=sz(ls);
if(pos==s+) return x;
if(pos<=s) x=ls;
else x=rs,pos-=(s+);
}
}
inline void reverse(int l,int r) {
l=get(l),r=get(r+);
Splay(l,),Splay(r,l); tg(ch(r,))^=;
} int s;
inline void print(int x) { spread(x);
if(!x) return ; print(ls);
if(vl(x)>=&&vl(x)<=n) printf("%d ",vl(x)); print(rs);
}
signed main() {
n=g(),m=g(); rt=build(,,n+);
for(R i=;i<=m;++i) {R l=g(),r=g(); reverse(l,r);}
print(rt); putchar('\n');
}

对于FHQ Treap,先把[1,l-1]和[l,n]split出来,再把[l,r]和[r+1,n]split出来,在代表区间的子树的根节点打tg,注意这道题的的split是按rank的

不过split完忘了upd。。。我真实沙雕qwq。。。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#define R register int
#define ls(x) ch[x][0]
#define rs(x) ch[x][1]
using namespace std;
const int N=;
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
}
int n,m,tot,rt;
int ch[N][],sz[N],vl[N],dat[N];
bool tg[N];
inline void upd(int x) {sz[x]=sz[ls(x)]+sz[rs(x)]+;}
inline int cre(int v) {R x=++tot; vl[x]=v,sz[x]=,dat[x]=rand(); upd(x); return x;}
inline void spread(int x) {if(tg[x]) swap(ls(x),rs(x)),tg[ls(x)]^=,tg[rs(x)]^=,tg[x]=;}
inline void split(int o,int rk,int& x,int& y) {
if(!o) {x=y=; return ;} spread(o);
if(sz[ls(o)]<rk) {x=o; split(rs(o),rk-sz[ls(o)]-,rs(o),y); upd(x);}
else {y=o; split(ls(o),rk,x,ls(o)); upd(y);}
}
inline int merge(int x,int y) {
if(!x||!y) return x+y;
if(dat[x]<dat[y]) {spread(x); rs(x)=merge(rs(x),y); upd(x); return x;}
else {spread(y); ls(y)=merge(x,ls(y)); upd(y); return y;}
}
inline void print(int x) {if(!x) return ; spread(x); print(ls(x)); printf("%d ",vl[x]); print(rs(x));}
signed main() { srand();
n=g(),m=g(); for(R i=;i<=n;++i) rt=merge(rt,cre(i)); R x=,y=,z=;
for(R i=;i<=m;++i) { R l=g(),r=g();
split(rt,l-,x,y); split(y,r-l+,y,z); tg[y]^=; rt=merge(x,merge(y,z));
} print(rt); while();
}

2019.05.06

Luogu P3391 文艺平衡树(Splay or FHQ Treap)的更多相关文章

  1. 【数据结构】平衡树splay和fhq—treap

    1.BST二叉搜索树 顾名思义,它是一棵二叉树. 它满足一个性质:每一个节点的权值大于它的左儿子,小于它的右儿子. 当然不只上面那两种树的结构. 那么根据性质,可以得到该节点左子树里的所有值都比它小, ...

  2. [luogu P3391] 文艺平衡树

    [luogu P3391] 文艺平衡树 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区 ...

  3. [洛谷P3391] 文艺平衡树 (Splay模板)

    初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...

  4. [Bzoj3223][Tyvj1729] 文艺平衡树(splay/无旋Treap)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3223 平衡树处理区间问题的入门题目,普通平衡树那道题在维护平衡树上是以每个数的值作为维护 ...

  5. BZOJ - 3223 Tyvj 1729 文艺平衡树 (splay/无旋treap)

    题目链接 splay: #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3f3f3f ...

  6. 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay

    [阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...

  7. luoguP3391[模板]文艺平衡树(Splay) 题解

    链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...

  8. 平衡树(Splay、fhq Treap)

    Splay Splay(伸展树)是一种二叉搜索树. 其复杂度为均摊\(O(n\log n)\),所以并不可以可持久化. Splay的核心操作有两个:rotate和splay. pushup: 上传信息 ...

  9. 平衡树合集(Treap,Splay,替罪羊,FHQ Treap)

    今天翻了翻其他大佬的博客,发现自己有些...颓废... 有必要洗心革面,好好学习 序:正常的BST有可能退化,成为链,大大降低效率,所以有很多方法来保持左右size的平衡,本文将简单介绍Treap,S ...

随机推荐

  1. 洛谷P2878 [USACO07JAN]保护花朵Protecting the Flowers

    题目描述 Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. ...

  2. [转]nodejs中的process模块--child_process.exec

    1.process是一个全局进程,你可以直接通过process变量直接访问它. process实现了EventEmitter接口,exit方法会在当进程退出的时候执行.因为进程退出之后将不再执行事件循 ...

  3. MyBatis动态传入表名,字段名参数的解决办法---statementType用法

    statementType="STATEMENT" 要实现动态传入表名.列名,需要做如下修改 添加属性statementType="STATEMENT" 同时s ...

  4. 【转】Jquery折叠效果

    转自:http://www.cnblogs.com/clc2008/archive/2011/10/25/2223254.html <!DOCTYPE html PUBLIC "-// ...

  5. shutdown-用于关闭/重启计算机

    Linux系统下的shutdown命令用于安全的关闭/重启计算机,它不仅可以方便的实现定时关机,还可以由用户决定关机时的相关参数.在执行shutdown命令时,系统会给每个终端(用户)发送一条屏显,提 ...

  6. IoC概述

    ---------------siwuxie095 IoC,即 Inversion of Control,控制反转,它是 Spring 容器的内核 AOP.声明式事务等功能都是在此基础上开花结果,即 ...

  7. R语言最好的IDE——RStudio

    转自http://www.dataguru.cn/article-1602-1.html 看到很多的R语言教材,介绍的编辑器或者IDE都是很简陋的那些,就没有见到有人提到RStudio.对于不使用Em ...

  8. Umbraco遇到的问题解决

    在本地VS2015运行公司的Corporate website时,有几个页面出现错误如下: 但事实是那个,这几个View都是存在的.弄了半天也没有能够解决.后来看到这个blog: https://ou ...

  9. Ubuntu14跑DSO

    按照https://github.com/JakobEngel/dso上的说明,make -j4的时候出现一下错误: /home/zhao/dso/src/FullSystem/CoarseIniti ...

  10. Sharepoint2013商务智能学习笔记之Excel Service展示Sql Server数据Demo(五)

    第一步,打开Excel新建空白工作簿 第二步,使用Excel连接sql 数据库 第三步,画图 第四步 添加筛选器 最后效果如下: 第五步,将Excel上传到sharepoint任意文档库,并直接点击 ...