文艺平衡树(Splay)
题目背景
这是一道经典的Splay模板题——文艺平衡树。
题目描述
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1
输入输出格式
输入格式:
第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2, \cdots n-1,n)(1,2,⋯n−1,n) m表示翻转操作次数
接下来m行每行两个数 [l,r][l,r] 数据保证 1 \leq l \leq r \leq n1≤l≤r≤n
输出格式:
输出一行n个数字,表示原始序列经过m次变换后的结果
输入输出样例
5 3
1 3
1 3
1 4
4 3 2 1 5
说明
n, m \leq 100000n,m≤100000
splay区间翻转每个区间逐层翻转然后合并
当然我们并不需要每次翻转就直接翻到底,而是打一个标记,类似于线段树,在查询的时候在随着查询的深入翻转
#include<cstdio>
#include<iostream>
using namespace std;
#define N 100005
int n,m;
struct node{
int siz,fa,ch[];//0zuo,1you;
bool rev;
}tr[N];
int root;
void push_down(int rt)
{
if(tr[rt].rev)
{
swap(tr[rt].ch[],tr[rt].ch[]);
tr[rt].rev=;
tr[tr[rt].ch[]].rev^=;tr[tr[rt].ch[]].rev^=;
}
}
void pushup(int rt)
{
tr[rt].siz=tr[tr[rt].ch[]].siz+tr[tr[rt].ch[]].siz+;
return;
}
int find(int rt,int x)
{
if(rt==)return ;
push_down(rt);
if(x<=tr[tr[rt].ch[]].siz)return find(tr[rt].ch[],x);
if(x==tr[tr[rt].ch[]].siz+)return rt;
return find(tr[rt].ch[],x-tr[tr[rt].ch[]].siz-);
}
void rotate(int &rt,int x)
{
int y=tr[x].fa,q=tr[y].fa;
bool dy=tr[y].ch[]==x,dz=tr[q].ch[]==y;
push_down(y);
if(rt==y)rt=x,tr[x].fa=q;
else tr[q].ch[dz]=x,tr[x].fa=q;
tr[y].ch[dy]=tr[x].ch[dy^],tr[tr[x].ch[dy^]].fa=y;
tr[x].ch[dy^]=y,tr[y].fa=x;
pushup(y);
return ;
}
void splay(int &rt,int x)
{
push_down(x);
while(rt!=x)
{
int y=tr[x].fa;int q=tr[y].fa;
if(y!=rt)
{
if(tr[y].ch[]==x^tr[q].ch[]==y)rotate(rt,x);
else rotate(rt,y);
}
rotate(rt,x);
}
pushup(x);
return;
}
void print(int rt)
{
if(rt==)return;
push_down(rt);
print(tr[rt].ch[]);
if(rt>&&rt<n+)printf("%d ",rt-);
print(tr[rt].ch[]);
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n+;i++)
{
tr[i].siz=n+-i;tr[i].fa=i-;tr[i].ch[]=i+;
}
tr[n+].ch[]=,root=;
while(m--)
{
int l,r;
scanf("%d%d",&l,&r);
splay(root,find(root,l));
splay(tr[root].ch[],find(root,r+));
tr[tr[tr[root].ch[]].ch[]].rev^=;
}
print(root);
return ;
}
文艺平衡树(Splay)的更多相关文章
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- luoguP3391[模板]文艺平衡树(Splay) 题解
链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...
- 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标记表示是否翻转 ------------------------------------------------- ...
- bzoj3223Tyvj 1729 文艺平衡树 splay
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5644 Solved: 3362[Submit][Sta ...
- bzoj 3223: Tyvj 1729 文艺平衡树 (splay)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...
- bzoj 3223 文艺平衡树 - Splay
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3884 Solved: 2235[Submit][Sta ...
- BZOJ3223 文艺平衡树(splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6881 Solved: 4213[Submit][Sta ...
随机推荐
- Python9-事件及队列-day37
信号量 from multiprocessing import Process from multiprocessing import Semaphore import time import ran ...
- LeetCode(282) Peeking Iterator
题目 Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peek ...
- 卸载firefox多余的搜索引擎
火狐默认了几个搜索引擎,百度,bing,yahoo等.搜一些技术方面的东西的时候,google返回的结果比这些要准确有用.所以想卸载掉那些不用的. 具体操作: 点击搜索栏,左侧搜索引擎图票右下角的倒三 ...
- linux学习-用户的特殊 shell 与 PAM 模块
特殊的 shell, /sbin/nologin 『无法登入』指的是:『这个使用者无法使用 bash 或其他 shell 来登入系统』而已, 并不是说这个账号就无法使用其他的系统资源! 让某个具有 / ...
- 栈的push、pop序列 【微软面试100题 第二十九题】
题目要求: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1.2.3.4.5是某栈的压栈序列,序列4.5.3.2.1是该压栈 ...
- Set容器——TreeSet及常用API
TreeSet及常用Api ① TreeSet为使用树来进行存储的Set接口提供了一个工具,对象按升序存储,访问和检索很快; ② 在存储了大量的需要进行快速检索的排序信息的情况下,TreeSe ...
- hdu1599 find the mincost route floyd求出最小权值的环
find the mincost route Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- Element 'plugin' cannot have character [children], because the type's content type is element-only
原因是你复制的时候,带了一些特殊符号. 解决方案: 将那一串代码复制到notpad++ 或者文本上面,再复制到你的编译器里面,就可以解决问题了
- mybatis读取oracle中blob
controller: byte[] blob = commonService.getPersonImage(bean.getIdCard()); String base64 = new String ...
- jq 的replaceWith方法在360下面会出现兼容问题
弄的繁琐点, 先remove旧的元素,然后append就好了