嘟嘟嘟




突然觉得splay挺有意思的……




这道题只有一个任务:区间翻转。

首先应该知道的是,splay和线段树一样,都可以打标记,然后走到每一个节点之前先下传。

那怎么打标记呢?还应该有“区间”的思想。

对于区间\([L, R]\),想办法把这个区间所在的子树提取出来,然后打个标记即可。

那怎么提取呢?其实也不难。只要找出\(L\)的前驱\(a = L - 1\)和\(R\)的后继\(b = R + 1\),然后把\(a\)旋到根,再把\(b\)旋到根的右子节点,这样\(b\)的左子树就是当前区间了。

但是找前驱和后继只能像bst那么找,因为这棵splay的key值是下标,而下标并没有存起来,而是通过子树大小体现的。所以上述找前驱和后继操作相当于查询第\(k\)大。因为事先加了\(-INF\)和\(INF\)防止越界,所以找前驱就是查询第\(L\)大的,后继就是第\(R + 2\)大的。

int getRank(int k)
{
int now = root;
while(1)
{
pushdown(now);
if(t[t[now].ch[0]].siz >= k) now = t[now].ch[0];
else if(t[t[now].ch[0]].siz + 1 == k) return now;
else k -= t[t[now].ch[0]].siz + 1, now = t[now].ch[1];
}
}
void update(int L, int R)
{
int a = getRank(L), b = getRank(R + 2); //pre(L), nxt(R)
splay(a, 0); splay(b, a); //现在b的左子树就是当前区间
pushdown(root); pushdown(t[root].ch[1]);
int now = t[t[root].ch[1]].ch[0];
t[now].lzy ^= 1;
}



还有一件事就是建树,虽然可以像[这道题](https://www.cnblogs.com/mrclr/p/10060317.html)一样每一次插入一个数,不过有更可爱的方法。
仿照线段树的建树方法,但有一个显著的区别是线段树的每一个节点表示一个区间,而splay就表示一个点,所以递归的时候把当前区间的$a[mid]$作为线段树该节点的权值,然后到$[L, mid - 1]$和$[mid + 1, R]$中建立左右子树。
```c++
int build(int L, int R, int f)
{
if(L > R) return 0;
int mid = (L + R) >> 1, now = ++ncnt;
t[now].val = a[mid]; t[now].fa = f;
t[now].ch[0] = build(L, mid - 1, now);
t[now].ch[1] = build(mid + 1, R, now);
pushup(now);
return now;
}
```


最后一件事就是输出。利用splay自身的性质,中序遍历就是答案。


完整代码
```c++
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans = 10) write(x / 10);
putchar(x % 10 + '0');
}

int n, m, a[maxn];

struct Tree

{

int ch[2], fa;

int val, siz, lzy;

}t[maxn];

int root, ncnt = 0;

void _PrintTr(int now)

{

if(!now) return;

printf("nd:%d val:%d ls:%d rs:%d\n", now, t[now].val, t[t[now].ch[0]].val, t[t[now].ch[1]].val);

_PrintTr(t[now].ch[0]); _PrintTr(t[now].ch[1]);

}

void pushdown(int now)

{

if(now && t[now].lzy)

{

t[t[now].ch[0]].lzy ^= 1; t[t[now].ch[1]].lzy ^= 1;

swap(t[now].ch[0], t[now].ch[1]);

t[now].lzy = 0;

}

}

void pushup(int now)

{

t[now].siz = t[t[now].ch[0]].siz + t[t[now].ch[1]].siz + 1;

}

void rotate(int x)

{

int y = t[x].fa, z = t[y].fa, k = (t[y].ch[1] == x);

t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;

t[y].ch[k] = t[x].ch[k ^ 1]; t[t[y].ch[k]].fa = y;

t[x].ch[k ^ 1] = y; t[y].fa = x;

pushup(y); pushup(x);

}

void splay(int x, int s) //旋转的时候不用pushdown.(因为是自底向上的)

{

while(t[x].fa != s)

{

int y = t[x].fa, z = t[y].fa;

if(z != s)

{

if((t[z].ch[0] == y) ^ (t[y].ch[0] == x)) rotate(x);

else rotate(y);

}

rotate(x);

}

if(s == 0) root = x;

}

int build(int L, int R, int f)

{

if(L > R) return 0;

int mid = (L + R) >> 1, now = ++ncnt;

t[now].val = a[mid]; t[now].fa = f;

t[now].ch[0] = build(L, mid - 1, now);

t[now].ch[1] = build(mid + 1, R, now);

pushup(now);

return now;

}

int getRank(int k)

{

int now = root;

while(1)

{

pushdown(now);

if(t[t[now].ch[0]].siz >= k) now = t[now].ch[0];

else if(t[t[now].ch[0]].siz + 1 == k) return now;

else k -= t[t[now].ch[0]].siz + 1, now = t[now].ch[1];

}

}

void update(int L, int R)

{

int a = getRank(L), b = getRank(R + 2); //pre(L), nxt(R)

splay(a, 0); splay(b, a); //现在b的左子树就是当前区间

pushdown(root); pushdown(t[root].ch[1]);

int now = t[t[root].ch[1]].ch[0];

t[now].lzy ^= 1;

}

void print(int now)

{

pushdown(now);

if(t[now].ch[0]) print(t[now].ch[0]);

if(t[now].val != INF && t[now].val != -INF) write(t[now].val), space;

if(t[now].ch[1]) print(t[now].ch[1]);

}

int main()

{

n = read(); m = read();

a[1] = -INF; a[n + 2] = INF;

for(int i = 1; i <= n; ++i) a[i + 1] = i;

root = build(1, n + 2, 0);

//_PrintTr(root);

for(int i = 1, L, R; i <= m; ++i) L = read(), R = read(), update(L, R);

print(root), enter;

return 0;

}

luogu P3391 【模板】文艺平衡树(Splay)的更多相关文章

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

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

  2. 洛谷.3391.[模板]文艺平衡树(Splay)

    题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...

  3. 【洛谷P3391】文艺平衡树——Splay学习笔记(二)

    题目链接 Splay基础操作 \(Splay\)上的区间翻转 首先,这里的\(Splay\)维护的是一个序列的顺序,每个结点即为序列中的一个数,序列的顺序即为\(Splay\)的中序遍历 那么如何实现 ...

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

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

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

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

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

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

  7. BZOJ3223 文艺平衡树(splay)

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

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

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

  9. Tyvj P1729 文艺平衡树 Splay

    题目: http://tyvj.cn/p/1729 P1729 文艺平衡树 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 此为平衡树系列第二道:文艺平衡树 ...

  10. BZOJ 3223: Tyvj 1729 文艺平衡树(splay)

    速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...

随机推荐

  1. Kafka、RabbitMQ、RocketMQ消息中间件的对比

    引言 分布式系统中,我们广泛运用消息中间件进行系统间的数据交换,便于异步解耦.现在开源的消息中间件有很多,目前对Kafka.RabbitMQ.RocketMQ这三个消息中间件做下对比分析. - - k ...

  2. java后台向路径发送请求获得相应参数

    从java后台向一路径发送请求,获得响应的参数,put get post ,还有一个返回URL的工具类,方便代码灵活修改 import java.io.BufferedReader; import j ...

  3. flex自适应宽度显示省略号

    text-overflow:ellipsis文本溢出显示省略号,一般的搭配用法如下: div{ text-overflow:ellipsis; overflow:hidden; white-space ...

  4. Django基础四之模板系统

    一 语法   模板渲染的官方文档 关于模板渲染你只需要记两种特殊符号(语法): {{  }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 二 变量 在Django的模板语言中按此语法使 ...

  5. 如何解决css-子div设置margin-top后,父div与子div一起下移的bug?

    根据规范,一个盒子如果没有上补白(padding-top)和上边框(border-top),那么这个盒子的上边距会和其内部文档流中的第一个子元素的上边距重叠. 这是规范引起的普遍问题. 只要给父盒子设 ...

  6. Ubuntu VNC 打开spyder无法输入(检测不到键盘配置)解决方法

    在ubuntu中安装好spyder后, 打开spyder发现无法输入. 在打开spyder的终端窗口,有如下提示: QXcbConnection: Failed to initialize XRand ...

  7. pycharm 调试Django 奇葩问题:Process finished with exit code -1073741819

    想自己整个BLOG,发现python+Django好像还不错,尝试一下.在使用过程中,突然pycharm不能调试django工程.网上搜索也没解决,是google哦.好像记得启动pycharm时,看到 ...

  8. ahjesus wp-autopost破解版,亲测可用

    在funtion.php里 把fetchUrl 这个函数的判断去掉 直接执行判断为真的结果下面是修改后的函数 function fetchUrl($_var_22){        global $w ...

  9. maven 结合mybaits整合框架,打包时mapper.xml文件,mapper目录打不进war包去问题

    首先,来看下MAVENx项目标准的目录结构: 一般情况下,我们用到的资源文件(各种xml,properites,xsd文件等)都放在src/main/resources下面,利用maven打包时,ma ...

  10. AIX常用命令学习(一)

    1.prtconf命令 查看AIX主机的结构特征状态 语法: prtconf [ -c ] [ -k ] [ -L ] [ -m ] [ -s ] [ -v ] Flags: -c  Displays ...