Description

您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1

Input

第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n)  m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n

Output

输出一行n个数字,表示原始序列经过m次变换后的结果

Sample Input

5 3

1 3

1 3

1 4

Sample Output

4 3 2 1 5

HINT

N,M<=100000

板子啊,常规操作

 #include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN (100000+10)
using namespace std;
int Father[MAXN];
int Son[MAXN][];
int Size[MAXN];
int Smark[MAXN];
int a[MAXN];
int Key[MAXN];
int Root;
int n,m,l,r;
int Get(int x)
{
return Son[Father[x]][]==x;
} void Update(int x)
{
Size[x]=Size[Son[x][]]+Size[Son[x][]]+;
} void Pushdown(int x)
{
if (x && Smark[x])
{
Smark[Son[x][]]^=;
Smark[Son[x][]]^=;
swap(Son[x][],Son[x][]);
Smark[x]=;
}
} void Rotate(int x)
{
Pushdown(Father[x]);
Pushdown(x);
int fa=Father[x];
int fafa=Father[fa];
int wh=Get(x); Son[fa][wh]=Son[x][wh^];
Father[fa]=x;
if (Son[fa][wh]) Father[Son[fa][wh]]=fa; Father[x]=fafa;
Son[x][wh^]=fa;
if (fafa) Son[fafa][Son[fafa][]==fa]=x; Update(fa);
Update(x);
} void Splay(int x,int tar)
{
for (int fa;(fa=Father[x])!=tar;Rotate(x))
if (Father[fa]!=tar)
Rotate(Get(fa)==Get(x)?fa:x);
if (!tar) Root=x;
} void Build (int l,int r,int fa)
{
if (l>r) return;
int mid=(l+r)/;
if (mid<fa) Son[fa][]=mid;
if (mid>fa) Son[fa][]=mid;
Father[mid]=fa;
Size[mid]=;
Key[mid]=a[mid];
if (l==r) return;
Build(l,mid-,mid);
Build(mid+,r,mid);
Update(mid);
} int Findx(int x)
{
int now=Root;
while ()
{
Pushdown(now);
if (x<=Size[Son[now][]])
now=Son[now][];
else
{
x-=Size[Son[now][]];
if (x==) return now;
x-=;
now=Son[now][];
}
}
} void Rever(int l,int r)
{
int f1=Findx(l);
int f2=Findx(r+);
Splay(f1,);
Splay(f2,f1);
Smark[Son[Son[Root][]][]]^=;
} void Write(int x)
{
Pushdown(x);
if (Son[x][]) Write(Son[x][]);
if (x>= && x<=n+) printf("%d ",Key[x]);
if (Son[x][]) Write(Son[x][]);
} int main()
{
scanf("%d%d",&n,&m);
for (int i=;i<=n+;++i)
a[i]=i-;
Build(,n+,);Root=(n+)/;
for (int i=;i<=m;++i)
{
scanf("%d%d",&l,&r);
if (l>=r) continue;
Rever(l,r);
}
Write(Root);
}

3223. 文艺平衡树【平衡树-splay】的更多相关文章

  1. fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)

    题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...

  2. P3391 【模板】文艺平衡树(Splay)新板子

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

  3. 洛谷——P3369 【模板】普通平衡树(splay)(基础splay,维护一些神奇的东东)

    P3369 [模板]普通平衡树 平衡树大法好,蒟蒻(博主)最近正在收集高级数据结构的碎片,企图合成数据结构的元素之力来使自己的RP++... 您需要写一种数据结构(可参考题目标题),来维护一些数,其中 ...

  4. bzoj 3223 文艺平衡树 - Splay

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

  5. 【BZOJ】3223: Tyvj 1729 文艺平衡树(splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3223 默默的.. #include <cstdio> #include <cstr ...

  6. bzoj 3223 文艺平衡树 splay 区间翻转

    Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 17715  Solved: 7769[Submit][Status][ ...

  7. BZOJ 3223 Tyvj 1729 文艺平衡树(Splay)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3223 [题目大意] 给出一数列,问m次区间翻转后的结果. [题解] Splay 区间翻 ...

  8. bzoj 3223 文艺平衡树 Splay 打标志

    是NOI2003Editor的一个子任务 #include <cstdio> #include <vector> #define maxn 100010 using names ...

  9. 【模板】文艺平衡树(Splay) 区间翻转 BZOJ 3223

    您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 N,M<= ...

随机推荐

  1. C# 分页方法

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web; ...

  2. __block和__weak修饰符的区别

    block下循环引用的问题 __block本身并不能避免循环引用,避免循环引用需要在block内部把__block修饰的obj置为nil __weak可以避免循环引用,但是其会导致外部对象释放了之后, ...

  3. MySQL数据库的回滚失败(JAVA)

    这几天在学习MySQL数据的知识,有一个小测试,用来测试数据库的提交和回滚. 刚开始的时候真的没把这个当回事,按照正常的步骤来讲的话,如下所示,加载驱动,获取数据库的连接,并且把数据库的自动提交给关闭 ...

  4. @ConfigurationProperties与@value区别

    @ConfigurationProperties与@value区别   @ConfigurationProperties @value 功能 批量注入配置文件中的属性 一个个指定 松散绑定 支持 不支 ...

  5. 翻译 – CSS3 Backgrounds相关介绍——张鑫旭

    —————以下为翻译内容—————- CSS2.1中有5个background属性可以用来控制元素的背景.这5个属性是: background-color background-image backg ...

  6. opencv3.2.0图像处理之均值滤波blur API函数

    ##.均值滤波:blur函数 ##函数原型 : ,-),int borderType=BORDER_DEFAULT) (参数详解同boxFilter函数) /**********新建Qt控制台程序** ...

  7. Android ListView的XML属性

    1.ListView的XML属性 android:divider //在列表条目之间显示的drawable或color android:dividerHeight //用来指定divider的高度 a ...

  8. sql 脚本 oracle scott 用户的四张表导入 mysql 中

    /* 要先删除emp表,不能先删除dept表,因为dept有一个外键关联emp表*/drop TABLE emp;drop TABLE dept; drop TABLE salgrade;drop T ...

  9. BS网站架构演变

    BS网站架构演变 网站架构的整个演变过程主要是围绕大数据和高并发这两个问题展开的,解决的方案主要分为使用缓存和使用多资源两种类型.多资源主要指多存储(包括多内存).多CPU和多网络,对于多资源来说又可 ...

  10. python学习手册中的一些易忘的点(前三部分)

    1.ubuntu下让python脚本可直接运行: test.py文件(后缀可省)#!/usr/bin/pythonprint('wwwww') sudo chmod +x ./test.py (sud ...