luogu P3391 【模板】文艺平衡树(Splay)
嘟嘟嘟
突然觉得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)的更多相关文章
- luoguP3391[模板]文艺平衡树(Splay) 题解
链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...
- 洛谷.3391.[模板]文艺平衡树(Splay)
题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...
- 【洛谷P3391】文艺平衡树——Splay学习笔记(二)
题目链接 Splay基础操作 \(Splay\)上的区间翻转 首先,这里的\(Splay\)维护的是一个序列的顺序,每个结点即为序列中的一个数,序列的顺序即为\(Splay\)的中序遍历 那么如何实现 ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- [洛谷P3391] 文艺平衡树 (Splay模板)
初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...
- BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6881 Solved: 4213[Submit][Sta ...
- BZOJ3223 文艺平衡树(splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- BZOJ3223: Tyvj 1729 文艺平衡树 [splay]
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3595 Solved: 2029[Submit][Sta ...
- Tyvj P1729 文艺平衡树 Splay
题目: http://tyvj.cn/p/1729 P1729 文艺平衡树 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 此为平衡树系列第二道:文艺平衡树 ...
- BZOJ 3223: Tyvj 1729 文艺平衡树(splay)
速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...
随机推荐
- [android] 手机卫士黑名单功能(列表展示)
先把要拦截的电话号码保存到数据库中,拦截模式用个字段区分,1 电话拦截,2 短信拦截,3全部拦截 新建Activity类CallSmsSafeActivity.java 新建布局文件activity_ ...
- lombok入门
pom.xml加入依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lo ...
- PECL: configuration option "php_ini" is not set to php.ini location
message similar to: configuration option "php_ini" is not set to php.ini locationYou shoul ...
- Centos7 安装 ActiveMq
1.安装 # cd /usr/local/src/# wget http://mirrors.shu.edu.cn/apache//activemq/5.15.3/apache-activemq-5. ...
- 使用Homebrew安装Git与Github在idea中的配置
系统环境:macOS 10.13.4 一.Homebrew的安装 linux系统有个让人蛋疼的通病,软件包依赖,好在当前主流的两大发行版本都自带了解决方案,Red hat有yum,Ubuntu有apt ...
- cakephp中使用大括号的形式避免用点号连接sql语句
在cakephp中可以使用{}的形式来代替点号连接sql语句,减少出错的几率
- struts2 上传下载文件,异常处理,数据类型转换
一,web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=" ...
- AngularJS之过滤器
AnularJS的过滤器用来格式化需要展示给用户的数据,有很多实用的内置过滤器,也可以自己编写. 在HTML中的模板绑定符号{{ }}内通过|符号来调用过滤器.例如,假设我们希望将字符串转换成大写,可 ...
- Mybatis:使用bean传值,当传入值为Null时,提示“无效的列类型”的解决办法
问题描述:在使用mybatis对数据库执行更新操作时,parameterType为某个具体的bean,而bean中传入的参数为null时,抛出异常如下:org.mybatis.spring.MyBat ...
- 关于redux
react将dom解耦,不用直接操作dom,使用了状态机制,当状态改变时视图就会相应更新.我们知道在react中,父组件可以将一些状态传递给子组件,让子组件的视图相应更新,这时我们会发现,只有有关联的 ...