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 31 31 31 4

Sample Output

4 3 2 1 5

HINT

N,M<=100000

还是比较经典的模板题,只有翻转操作。维修数列A了之后这道就很水了。

Code:

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<queue>
#define N 1000005
#define Inf 1000000000
using namespace std;
int n,m,root,cnt,a[N],id[N],fa[N],c[N][2];
int size[N],rev[N];
queue<int> Q;
inline int read()
{int f=1,x=0;char s=getchar();while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}x*=f;return x;
}
void update(int x)
{int l=c[x][0],r=c[x][1];size[x]=size[l]+size[r]+1;
}
void pushdown(int x)
{int l=c[x][0],r=c[x][1];if(rev[x]){rev[x]^=1;rev[l]^=1;rev[r]^=1;swap(c[l][0],c[l][1]);swap(c[r][0],c[r][1]);}
}
void rotate(int x,int &k)
{int y=fa[x],z=fa[y];int l=(c[y][1]==x);int r=l^1;if(y==k)k=x;elsec[z][c[z][1]==y]=x;fa[c[x][r]]=y;fa[y]=x;fa[x]=z;c[y][l]=c[x][r];c[x][r]=y;update(y);update(x);
}
void splay(int x,int &k)
{while(x!=k){int y=fa[x],z=fa[y];if(y!=k){if(c[y][0]==x^c[z][0]==y)rotate(x,k);else rotate(y,k);}rotate(x,k);}
}
int find(int x,int k)
{pushdown(x);int l=c[x][0],r=c[x][1];if(size[l]+1==k)return x;if(size[l]>=k)return find(l,k);return find(r,k-size[l]-1);
}
int split(int k,int tot)
{int x=find(root,k),y=find(root,k+tot+1);splay(x,root);splay(y,c[x][1]);return c[y][0];
}
void rever(int k,int tot)
{int x=split(k,tot),y=fa[x];rev[x]^=1;swap(c[x][0],c[x][1]);update(y);update(fa[y]);
}
void build(int l,int r,int f)
{if(l>r)return;int mid=(l+r)>>1;int now=id[mid],last=id[f];if(l==r){size[now]=1;rev[now]=0;}elsebuild(l,mid-1,mid),build(mid+1,r,mid);fa[now]=last;update(now);c[last][mid>=f]=now;
}
int main()
{int n=read(),m=read();for(int i=1;i<=n+2;i++)id[i]=i;build(1,n+2,0);root=(n+3)>>1;cnt=n+2;for(int i=1;i<=m;i++){int x,y;scanf("%d%d",&x,&y);rever(x,y-x+1);}for(int i=2;i<=n+1;i++)printf("%d ",find(root,i)-1);return 0;
}

BZOJ3223-Tyvj 1729 文艺平衡树相关推荐

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

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

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

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

  3. [Tyvj 1729] 文艺平衡树

    题面如下: Tyvj 1729 文艺平衡树 Time Limit: 1 Sec  Memory Limit: 128 MB Description 您需要写一种数据结构(可参考题目标题),来维护一个有 ...

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

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

  5. splay区间翻转(bzoj 3223: Tyvj 1729 文艺平衡树)

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 4854  Solved: 2844 [Submit][S ...

  6. [HZOI 2016][Tyvj 1729]文艺平衡树 这道题我真是哭了,调了一下午,一晚上

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

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

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

  8. bzoj 3223: Tyvj 1729 文艺平衡树

    Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 2853  Solved: 1602 [Submit][Status][Discuss] Descri ...

  9. BZOJ Tyvj 1729 文艺平衡树

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

最新文章

  1. 2021年春季学期-信号与系统-第十三次作业参考答案-第六小题
  2. cmd:计算机cmd常用命令集合之详细攻略daiding
  3. Android安卓模拟器中模拟SD卡
  4. C# webservice服务跟踪调试方法(转)
  5. 程序员下班回家,路上被拦…
  6. VS2017 提示警告 IDE0006
  7. 阿里的爱心助农“生意”:严把质量关 一场多业务线的联动大练兵
  8. Tomcat自己解压WAR包
  9. 【Android驱动】高通串口驱动,串口驱动中的msm_serial.c
  10. 谷歌生物医学专用翻译_实用技能 | 知云文献翻译
  11. 使用putty进行UART串口调试
  12. Directive 详解
  13. python人脸识别门禁系统_树莓派人脸识别门禁系统图文教程
  14. hihocode-2月29
  15. (转)360的困兽之斗——重新探讨奇虎商业模式
  16. 【ARM Linux 系统稳定性分析入门及渐进 1 -- Crash 工具简介】
  17. MATLAB算法实战应用案例精讲-【智能优化算法】细菌觅食优化-BFO(附MATLAB源码)
  18. 【Java设计模式】迭代器模式
  19. 从gitlab下载公司项目代码流程(还有git的学习和使用)
  20. 唤醒手腕Python全栈工程师学习笔记(网络爬虫篇)

热门文章

  1. MySQL数据库系统的学习(一)
  2. PhotoShop使用魔术棒简单抠图
  3. 快速幂、快速乘、矩阵快速幂
  4. Swift语言Storyboard教程:第二部
  5. 英语中代替 I think 的Debate回答
  6. 计及光伏波动性的主动配电网有功无功协调优化(Matlab代码实现)
  7. C# tcp发送十六进制数据
  8. MySQL学习笔记(三)— 查询篇(DQL语言)
  9. CreateChildControls、EnsureChildControls、RenderControl、Render、RenderChildren
  10. 遗传算法(求函数极值)简易代码C语言200行