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. NSLog演化

    使用下面代码打印行号,功能函数,以及要打印的内容 #if DEBUG #define MBLog(format, ...) NSLog((@"%s--[Line:%d]--" fo ...

  2. Spring界的HelloWorld

    1.Spring依赖的jar包下载网址: https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-loca ...

  3. 基于socket的简单p2p聊天项目

    https://blog.csdn.net/Jacky_Can/article/details/74984822 https://blog.csdn.net/qq_20889581/article/d ...

  4. 不使用JavaScript让IE浏览器支持HTML5元素——张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=2515 如果我们不做什 ...

  5. 使用CSS3改变文本选中的默认颜色——张鑫旭

    关于浏览器文字选中颜色 以我的系统举例(xp 默认主题),浏览器上页面文字选中后默认的背景色是一种蓝色, 不同浏览器的颜色有些许差异,但大致相同,文字颜色也近乎白色,如下图所示,截自Firefox3. ...

  6. csharp:获取 DNS、网关、子网掩码、IP

    /// <summary> /// DNS.网关.子网掩码.IP /// 涂聚文 2015 /// </summary> public class IPAddressStrin ...

  7. .NET中的异步编程

    开篇 异步编程是程序设计的重点也是难点,还记得在刚开始接触.net的时候,看的是一本c#的Winform实例教程,上面大部分都是教我们如何使用Winform的控件以及操作数据库的实例,那时候做的基本都 ...

  8. PHP 数值处理的几种常用的方法

    一.直接取整,舍弃小数,保留整数:intval(): intval(9.21); /*结果是9*/ intval(9.89); /*结果是9*/ intval(string); /*如果里面是字符串, ...

  9. 专访阿里资深研发工程师窦贤明:PG与商业数据库差距并不明显

    窦贤明认为, 支持类型.功能和语法丰富,性能优良   9月24日,窦贤明将参加在北京举办的线下活动,并做主题为<Greenplum分片案例分析>的分享.值此,他分享了PG.工作上的一些经历 ...

  10. new 关键字

    学习过的调用或者是执行函数的方式有几种? ①函数名+小括号 ②事件处理函数 ③定时器 ④数组里面的元素是函数,枚举出来执行 ⑤new关键字 提示:需要注意new 关键字需要在函数名前面使用 构造函数是 ...