treap大法好!!!

splay什么的都是异端

——XZZ

先%FHQ为敬

(fhq)treap也是可以搞区间翻转的

每次把它成(1L-1)(LR)(R+1~n)三块然后打标记拼回去

对于有标记的先交换一下左右儿子再异或一下各儿子的标记

虽说跑的不算快但还是可以挑战一下splay的

代码蒯上

//get out splay!
#include<iostream>
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
inline int gotcha()
{
register int _a=0;bool _b=1;register char _c=getchar();
while((_c<'0' || _c>'9') && _c!='-')_c=getchar();
if(_c=='-')_b=0,_c=getchar();
while(_c>='0' && _c<='9')_a=_a*10+_c-48,_c=getchar();
return _b?_a:-_a;
}
const int _ = 100007;int seed=19260817;
inline int ou(){return seed=seed*17283%2147483647;}//看RP的时候到了
int ch[_][2],d[_],rn[_],sz[_],all=0,n,root=0;
int rev[_];
inline void up(int x){sz[x]=1+sz[ch[x][0]]+sz[ch[x][1]];}//更新节点值
inline void down(int x)//向下传递翻转标记
{
if(x && rev[x])
{
rev[x]=0,swap(ch[x][0],ch[x][1]);
if(ch[x][0])rev[ch[x][0]]^=1;
if(ch[x][1])rev[ch[x][1]]^=1;
}
}
inline int malloc(int v){sz[++all]=1;d[all]=v;rn[all]=ou();return all;}//新建一个节点
inline int merge(int a,int b)
{
if(!a || !b) return a+b;down(a),down(b);
if(rn[a]<rn[b]){ch[a][1]=merge(ch[a][1],b);up(a);return a;}
else{ch[b][0]=merge(a,ch[b][0]);up(b);return b;}
}
void split(int now,int k,int &a,int &b)
{
if(!now){a=b=0;return;}
down(now);
if(k<=sz[ch[now][0]])b=now,split(ch[now][0],k,a,ch[now][0]);
else a=now,split(ch[now][1],k-sz[ch[now][0]]-1,ch[now][1],b);
up(now);
}
int plant(int l,int r)//种树
{
if(l>r)return 0;
int mid=(l+r)>>1,now=malloc(mid-1);
ch[now][0]=plant(l,mid-1);ch[now][1]=plant(mid+1,r);
up(now);return now;
}
void print(int now)//按中序遍历输出答案
{
if(!now)return;down(now);
print(ch[now][0]);
if(d[now]>=1 && d[now]<=n)printf("%d ",d[now]);
print(ch[now][1]);
return;
}
void reverse(int l,int r)//旋转
{
int a,b,c;
split(root,l-1,a,b),split(b,r-l+1,b,c);
rev[b]^=1;
root=merge(a,merge(b,c));
}
int main()
{
register int m,l,r;
n=gotcha();m=gotcha();
root=plant(2,n+3);
while(m--)
{
l=gotcha(),r=gotcha();
reverse(l,r);
}
print(root);
return 0;
}

treap坠强!

如何用treap写luogu P3391的更多相关文章

  1. [luogu P3391] 文艺平衡树

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

  2. 如何用ActiveQt写导出类

    如何用ActiveQt写导出类 最近一直在用ActiveQt框架来写ActiveX插件, 由于项目需要提示类的导出, 所以上午捣鼓了一下, 现在记录记录.其实内容主要是把Qt手册里自己用到的部分整理一 ...

  3. linux常用终端指令+如何用vim写一个c程序并运行

    在装好ubuntu之后今天学习了一些linux的一些基础知识: windows里面打开命令窗口是win+r,在linux系统里面,ctrl+alt+t打开终端,今天的一些指令都是围绕终端来说的 首先s ...

  4. 2019-2-13-Latex-论文elsevier,手把手如何用Latex写论文

    title author date CreateTime categories Latex 论文elsevier,手把手如何用Latex写论文 lindexi 2019-02-13 10:38:20 ...

  5. Luogu P3391 文艺平衡树(Splay or FHQ Treap)

    这道题要求区间反转...好东西.. 对于Splay:把l-1旋到根,把r+1旋到根的右儿子,这样r+1的左儿子就是整个区间了,然后对这个区间打个tg 注意要插-Inf和Inf到树里面,防止越界,坐标要 ...

  6. luogu P3391 【模板】文艺平衡树(Splay) 非旋转treap

    Code: #include<bits/stdc++.h> using namespace std; void setIO(string s) { string in=s+".i ...

  7. Luogu P3391 【模板】文艺平衡树 Splay 平衡树

    https://www.luogu.org/problemnew/show/P3391 以前写过题解的入门题重写练板子.wdnmd真就 ' == ' 写成 ' = ' 了编译器不报错呗. #inclu ...

  8. Luogu P3391 【模板】文艺平衡树(FHQ-Treap)

    题意 给出一个长为$n$序列$[1,2,...,n]$,$m$次操作,每次指定一段区间$[l,r]$,将这段区间翻转,求最终序列 题解 虽然标题是$Splay$,但是我要用$FHQ\ Treap$,考 ...

  9. 如何用java写出无副作用的代码

    搞java的同学们可能对无副作用这个概念比较陌生,这是函数式编程中的一个概念,无副作用的意思就是: 一个函数(java里是方法)的多次调用中,只要输入参数的值相同,输出结果的值也必然相同,并且在这个函 ...

随机推荐

  1. 快色排序算法(C语言描述)

    快速排序 算法思想 快速排序采用了一种分治策略,学术上称之为分治法(Divide-and-Conquer Method). 哨兵(如下算法中的key) 每趟排序将哨兵插入到数组的合适位置,使得哨兵左侧 ...

  2. 对flex-grow和flex-shrink的深入理解

    flex弹性布局,如果子元素宽度之和大于或者小于父元素宽度,空间就会存在剩余和不够,flex默认不换行,除非设置flex-wrap,那么这种情况下,有两个重要的属性,flex-grow和flex-sh ...

  3. jQuery转盘插件rotate

    css .rotate{ background:#aaa; padding:100px; position: relative; } .point { position: absolute; top: ...

  4. python基础教程总结3—字典

    1.字典 1.1 字典类型与序列类型的区别: 存取和访问数据的方式不同. 序列类型只用数字类型的键(从序列的开始按数值顺序索引): 映射类型可以用其他对象类型作键(如:数字.字符串.元祖,一般用字符串 ...

  5. 通用的flash代码

    黑体字部分为常修改的部分 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=&quo ...

  6. Expires和Cache-Control

    本文原链接:https://blog.csdn.net/zhouziyu2011/article/details/71312452 浅谈前端性能优化(一)——Expires和Cache-Control ...

  7. linux目录结构及文件管理

    Linux的目录结构: /            根分区 linux文件系统的起点 /bin           普通用户的命令,普通用户能使用 /sbin         管理员使用的命令,只有管理 ...

  8. 字符串的驻留(String Interning)

    http://www.cnblogs.com/artech/archive/2007/03/04/663728.html 关于字符串的驻留的机制,对于那些了解它的人肯定会认为很简单,但是我相信会有很大 ...

  9. ssh整合思想 Spring与Hibernate和Struts2的action整合 调用action添加数据库 使用HibernateTemplate的save(entity)方法 update delete get 等方法crud操作

    UserAction类代码: package com.swift.action; import com.opensymphony.xwork2.ActionSupport; import com.sw ...

  10. linux 安装并且设置环境lua环境变量

    在lua官网下载lua安装包并安装: http://www.lua.org/download.html 解压编译: wget http://www.lua.org/ftp/lua-5.3.2.tar. ...