思路

直接使用FHQ Treap维护即可

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node{
int lson,rson,val,ran,sz,inv;
}FHQ[100100];
int Nodecnt=0,a[100100],n,m,root;
void pushup(int o){
FHQ[o].sz=FHQ[FHQ[o].lson].sz+FHQ[FHQ[o].rson].sz+1;
}
void pushdown(int o){
if(FHQ[o].inv){
FHQ[FHQ[o].lson].inv^=1;
FHQ[FHQ[o].rson].inv^=1;
swap(FHQ[o].lson,FHQ[o].rson);
FHQ[o].inv=0;
}
}
int build(int l,int r){
if(l>r)
return 0;
int o=++Nodecnt;
FHQ[o].inv=FHQ[o].lson=FHQ[o].rson=0;
int mid=(l+r)>>1;
FHQ[o].ran=rand();
FHQ[o].sz=1;
FHQ[o].val=a[mid];
FHQ[o].lson=build(l,mid-1);
FHQ[o].rson=build(mid+1,r);
pushup(o);
return o;
}
void split(int now,int val,int &x,int &y){
if(!now){
x=y=0;
return;
}
pushdown(now);
if(FHQ[FHQ[now].lson].sz>=val){
y=now;
split(FHQ[now].lson,val,x,FHQ[now].lson);
pushup(now);
}
else{
x=now;
split(FHQ[now].rson,val-FHQ[FHQ[now].lson].sz-1,FHQ[now].rson,y);
pushup(now);
}
}
int merge(int x,int y){
if(x*y==0)
return x+y;
pushdown(x);
pushdown(y);
if(FHQ[x].ran<FHQ[y].ran){
FHQ[x].rson=merge(FHQ[x].rson,y);
pushup(x);
return x;
}
else{
FHQ[y].lson=merge(x,FHQ[y].lson);
pushup(y);
return y;
}
}
void output(int o){
if(!o)
return;
pushdown(o);
output(FHQ[o].lson);
printf("%d\n",FHQ[o].val);
output(FHQ[o].rson);
}
int main(){
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
a[i]=i;
root=build(1,n);
for(int i=1;i<=m;i++){
int l,r;
scanf("%d %d",&l,&r);
int lroot,midroot,tmproot,rroot;
split(root,l-1,lroot,tmproot);
split(tmproot,r-l+1,midroot,rroot);
FHQ[midroot].inv^=1;
pushdown(midroot);
root=merge(merge(lroot,rroot),midroot);
}
output(root);
return 0;
}

UVA11922 Permutation Transformer的更多相关文章

  1. UVA_11922 Permutation Transformer 【splay树】

    一.题目 UVA11922 二.分析 为什么会有伸展树? 伸展树与AVL的区别除了保持平衡的方式不同外,最重要的是在每次查找点时,让该点旋转到根结点,这里可以结合计算机里的局部性原理思考. 伸展树有什 ...

  2. UVA 11922 Permutation Transformer(平衡二叉树)

    Description Write a program to transform the permutation 1, 2, 3,..., n according to m instructions. ...

  3. UVa 11922 - Permutation Transformer 伸展树

    第一棵伸展树,各种调试模板……TVT 对于 1 n 这种查询我处理的不太好,之前序列前后没有添加冗余节点,一直Runtime Error. 后来加上冗余节点之后又出了别的状况,因为多了 0 和 n+1 ...

  4. uva 11922 - Permutation Transformer

    splay的题: 学习白书上和网上的代码敲的: #include <cstdio> #include <cstring> #include <cstdlib> #i ...

  5. UVA 11922 Permutation Transformer(Splay Tree)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18902 [思路] 伸展树+打标记. 用伸展树维护这个序列,使得能 ...

  6. UVA - 11922 Permutation Transformer (splay)

    题目链接 题意:你的任务是根据m条指令改变排列{!,2,3,...,n}.每条指令(a,b)表示取出第a~b个元素,翻转后添加到排列的尾部.输出最终序列. 解法:splay对区间分裂合并翻转,模板题. ...

  7. UVA 11922 Permutation Transformer (Splay树)

    题意: 给一个序列,是从1~n共n个的自然数,接下来又m个区间,对于每个区间[a,b],从第a个到第b个从序列中分离出来,翻转后接到尾部.输出最后的序列. 思路: 这次添加了Split和Merge两个 ...

  8. UVA 11922 Permutation Transformer —— splay伸展树

    题意:根据m条指令改变排列1 2 3 4 … n ,每条指令(a, b)表示取出第a~b个元素,反转后添加到排列尾部 分析:用一个可分裂合并的序列来表示整个序列,截取一段可以用两次分裂一次合并实现,粘 ...

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

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

随机推荐

  1. loj2083 优秀的拆分 [NOI2016] SA

    正解:SA 解题报告: 我永远喜欢loj! 显然$AABB$串相当于是由两个$AA$串拼起来的,所以可以先考虑如果求出来了所有$AA$串怎么求答案? 就假如能统计出$st[i]$表示所有以$i$为开头 ...

  2. 转载:Android RecyclerView 使用完全解析 体验艺术般的控件

    转自:https://blog.csdn.net/lmj623565791/article/details/45059587

  3. OC闪屏页尺寸

    1242*2688  828*1972 1125x2436 1242×2208  750×1334 640×960   640×1136 320x480    640x960  640x1136

  4. new Date() 日期格式处理

    var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...

  5. Linux的top命令学习【转载】

    转自:https://blog.csdn.net/sanshiqiduer/article/details/1933625 1.top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的 ...

  6. 2018-2019-2 20175211 实验二《Java面向对象程序设计》实验报告

    目录 代码托管 一.单元测试 (1)三种代码 二.TDD(Test Driven Development,测试驱动开发) 三.面对对象三要素 四.练习 五.问题及解决 六.PSP 代码托管 一.单元测 ...

  7. ubuntu 16.04 安装wechat, chrome等

    安装wechat 按照https://www.jb51.net/article/131179.htm,结果发现不行. 用apt-get install electronic-wechat的方式呢,也不 ...

  8. setup FTP server on CentOS 7

    Setup FTP Server on CentOS 7 Install vsftpd vsftpd (Very Secure File Transport Protocol Daemon) is a ...

  9. TypeError: Fetch argument 0.484375 has invalid type <class 'numpy.float32'>, must be a string or Tensor. (Can not convert a float32 into a Tensor or Operation.)

    报错: TypeError: Fetch argument 0.484375 has invalid type <class 'numpy.float32'>, must be a str ...

  10. C++---使用VS在C++编程中出现 fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h"”?

    啦啦啦,好久没写博客啦... 对于C++初学者来说适应一个新的编译器还是需要蛮长一段时间的,现在我就给你们说说标题所说的这个问题吧... 第一步:菜单--〉项目--〉设置,出现“项目设置”对话框,左边 ...