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

Source

区间翻转,注意kth时和标记时下方标记,没了。
 #include <cstdio>
#include <iostream>
#define N 100100
using namespace std;
struct SplayNode
{
SplayNode *fa,*ch[];
SplayNode();
int data,num,size;
bool rev;
int chr() {return this==fa->ch[];}
void updata() {size=ch[]->size+ch[]->size+;}
void mark() {rev=rev^;}
void push()
{
if (rev)
{
rev=rev^;;
ch[]->mark();
ch[]->mark();
swap(ch[],ch[]);
}
}
}*null;
SplayNode::SplayNode() {fa=ch[]=ch[]=null; rev=; size=;}
int n,m;
int a[N];
namespace Splay
{
SplayNode *Root;
SplayNode *Build(int l,int r)
{
if (l>r) return null;
int mid=(l+r)>>;
SplayNode *R=new SplayNode;
R->data=a[mid];
R->ch[]=Build(l,mid-);
R->ch[]=Build(mid+,r);
R->ch[]->fa=R;
R->ch[]->fa=R;
R->updata();
return R;
}
void MakeTree()
{
null=new SplayNode;
*null=SplayNode();
Root=Build(,n);
}
void rotate(SplayNode *x)
{
SplayNode *r=x->fa;
if (r==null || x==null) return;
int t=x->chr();
r->ch[t]=x->ch[t^];
r->ch[t]->fa=r;
if (r->fa==null) Root=x;
else r->fa->ch[r->chr()]=x;
x->fa=r->fa;
x->ch[t^]=r;
r->fa=x;
x->updata();
r->updata();
}
void splay(SplayNode *x,SplayNode *y)
{
for (;x->fa!=y;rotate(x))
if (x->fa->fa!=y)
if (x->fa->chr()==x->chr()) rotate(x->fa);
else rotate(x);
}
SplayNode *Kth(int k)
{
SplayNode *r=Root;
if (k< || k>n) return null;
while (r!=null)
{
r->push();
if (k<=r->ch[]->size) r=r->ch[];
else if (k==r->ch[]->size+) return r;
else
{
k-=r->ch[]->size+;
r=r->ch[];
}
}
return null;
}
void reverse(int l,int r)
{
SplayNode *q=Kth(l-);
SplayNode *p=Kth(r+);
if (q==null && p==null)
{
Root->mark();
return;
}
if (q==null)
{
splay(p,null);
p->ch[]->mark();
return;
}
if (p==null)
{
splay(q,null);
q->ch[]->mark();
return;
}
q->push();
splay(q,null);
p->push();
splay(p,q);
p->ch[]->mark();
}
void dfs(SplayNode *x)
{
if (x==null) return;
x->push();
dfs(x->ch[]);
printf("%d ",x->data);
dfs(x->ch[]);
} }
int main()
{
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++) a[i]=i;
Splay::MakeTree();
//Splay::dfs(Splay::Root);
// cout<<endl;
for (int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
Splay::reverse(x,y);
// Splay::dfs(Splay::Root);
// cout<<endl;
}
Splay::dfs(Splay::Root);
return ;
}

【BZOJ3223】 Tyvj 1729 文艺平衡树 Splay的更多相关文章

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

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

  2. bzoj3223: Tyvj 1729 文艺平衡树 splay裸题

    splay区间翻转即可 /************************************************************** Problem: 3223 User: walf ...

  3. [BZOJ3223]Tyvj 1729 文艺平衡树

    [BZOJ3223]Tyvj 1729 文艺平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区 ...

  4. bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)

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

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

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

  6. bzoj 3223: Tyvj 1729 文艺平衡树 (splay)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...

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

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

  8. 【Splay】bzoj3223 Tyvj 1729 文艺平衡树

    #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #i ...

  9. 【Splay】【块状链表】bzoj3223 Tyvj 1729 文艺平衡树

    让蒟蒻见识到了常数大+滥用STL的危害. <法一>很久之前的Splay #include<cstdio> #include<algorithm> using nam ...

随机推荐

  1. 对数据库触发器new和old的理解

    在数据库的触发器中经常会用到更新前的值和更新后的值,所有要理解new和old的作用很重要.当时我有个情况是这样的:我要插入一行数据,在行要去其他表中获得一个单价,然后和这行的数据进行相乘的到总金额,将 ...

  2. POJ1285 Combinations, Once Again(背包 排列组合)

    背包解组合数学问题,n种物品,每种num[i]个,求取r个的方法数. 背包思想,f[j]表示当前取j个数的方法数,则状态转移方程为 f[j] += f[k](max(j - num[i], 0) &l ...

  3. W:Failed to fetch http://archive.ubuntukylin.com:10006/ubuntukylin/dists/pre

    由于用ubuntu的时候装了几个ubuntukylin的软件(像搜狗拼音for linux),于是最近总是蹦出一个红色的三角提示,说无法更新,虽说不影响使用但是还是很不爽.解决方法记录如下: 进入系统 ...

  4. js中ascii码的转换

    今天在把原来用C写的程序移植到javascript上,但是有个地方一直调不通,后来才发现是js奇葩的字符处理出的问题.c中使用的字符处理比如加上一个字符值强制转换一下,在js中就行不通了. 但是js提 ...

  5. [JavaCore] 不错的Java基础学习资料-持续更新

    容易弄混的JAVA基础知识: http://www.iteye.com/topic/943647 [总结]String in Java: http://www.iteye.com/topic/5221 ...

  6. 无法打开包括文件:“windows.h”: No such file or directory

      VS2012 出现如下错误: 无法打开包括文件:"windows.h": No such file or directory   解决办法,将 C:\Program Files ...

  7. HDU 4005 The war Tarjan+dp

    The war Problem Description   In the war, the intelligence about the enemy is very important. Now, o ...

  8. 【java基础】面向过程~面向对象

    相信大家都知道这两个东西,可是大家是如何知道的呢?我们又该如何区分这个东西到底是面向过程还是面向对象的呢? 那,我们首先就要知道什么是面向过程,什么是面向对象: 面向过程"(Procedur ...

  9. ArrayList集合&特殊集合

    一.ArrayList集合 集合内可以放不同类型的元素 另:object类型为所有数据类型的基类 添加元素:.add(); 清空集合:al.clear(); 克隆集合:.clone(); 判断是否包含 ...

  10. 【jackson 异常】com.fasterxml.jackson.databind.JsonMappingException异常处理

    项目中,父层是Gene.java[基因实体]  子层是Corlib.java[文集库实体],一种基因对用多个文集库文章 但是在查询文集库这个实体的时候报错:[com.fasterxml.jackson ...