splay的题;

学习白书上和网上的代码敲的;

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
int n,m;
struct node
{
node *ch[];
int s,v;
int flip;
node(int v):v(v)
{
ch[]=ch[]=NULL;
s=;
flip=;
}
void maintain()
{
s=;
if(ch[]!=NULL)s+=ch[]->s;
if(ch[]!=NULL)s+=ch[]->s;
}
void pushdown()
{
if(flip)
{
flip=;
swap(ch[],ch[]);
if(ch[]!=NULL)ch[]->flip=!ch[]->flip;
if(ch[]!=NULL)ch[]->flip=!ch[]->flip;
}
}
int cmp(int x)const
{
int t=(ch[]==NULL)?:ch[]->s;
if(t>=x)return ;
if(t+==x)return -;
return ;
}
};
void rotate(node* &root,int d)
{
node *k=root->ch[d^];
root->ch[d^]=k->ch[d];
k->ch[d]=root;
root=k;
root->ch[d]->maintain();
root->maintain();
} void build(node* &root,int l,int r)
{
int mid=(l+r)>>;
root=new node(mid);
if(l<mid)build(root->ch[],l,mid-);
if(r>mid)build(root->ch[],mid+,r);
root->maintain();
} void splay(node* &root,int k)
{
root->pushdown();
int d=root->cmp(k);
if(d==)
{
if(root->ch[]!=NULL)
k-=root->ch[]->s;
k--;
}
if(d!=-)
{
node *p=root->ch[d];
p->pushdown();
int d2=p->cmp(k);
int k2=k;
if(d2==)
{
if(p->ch[]!=NULL)
k2-=p->ch[]->s;
k2--;
}
if(d2!=-)
{
splay(p->ch[d2],k2);
if(d==d2) rotate(root,d^);
else rotate(root->ch[d],d);
}
rotate(root,d^);
}
return;
} void split(node *root,int k,node* &left,node* &right)
{
splay(root,k);
left=root;
right=root->ch[];
root->ch[]=NULL;
left->maintain();//左边的要维护,右边不需要;
} node *merge(node *left,node *right)
{
splay(left,left->s);//
left->ch[]=right;
left->maintain();
return left;
} void dfs(node *root)
{
root->pushdown();
if(root->ch[])dfs(root->ch[]);
if(root->v&&root->v!=n+)printf("%d\n",root->v);
if(root->ch[])dfs(root->ch[]);
} void del(node *root)
{
if(root->ch[])del(root->ch[]);
if(root->ch[])del(root->ch[]);
delete root;
} node *root;
int main()
{
int a,b;
while(scanf("%d%d",&n,&m)!=EOF)
{
root=NULL;
build(root,,n+);
while(m--)
{
scanf("%d%d",&a,&b);
node *right,*mid,*left,*tmp,*o;
split(root,a,left,o);
split(o,b-a+,mid,right);
if(right->s>)
{
split(right,right->s-,tmp,o);
mid->flip^=;
root=merge(left,merge(merge(tmp,mid),o));
}
else
{
mid->flip^=;
root=merge(left,merge(mid,right));
}
}
dfs(root);
del(root);
}
return ;
}

uva 11922 - Permutation Transformer的更多相关文章

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

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

  2. UVa 11922 - Permutation Transformer 伸展树

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

  3. UVA 11922 Permutation Transformer(Splay Tree)

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

  4. UVA - 11922 Permutation Transformer (splay)

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

  5. UVA 11922 Permutation Transformer (Splay树)

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

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

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

  7. uva 11922 Permutation Transforme/splay tree

    原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18902 伸展树的区间翻转剪切... 如下: #include< ...

  8. UVA 11922 Splay tree

    UVA 11922 题意: 有n个数1~n 操作a,b表示取出第a~b个数,翻转后添加到数列的尾部 输入n,m 输入m条指令a,b 输出最终的序列 代码: #include<iostream&g ...

  9. UVA 12003 Array Transformer

    Array Transformer Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on UVA. Orig ...

随机推荐

  1. jquery click事件的可选参数data的作用

    $("#ID").click({x:"value"},function (e) { alert(e.data.x); });

  2. ASP.NET MVC之PagedList使用

    ASP.NET MVC之PagedList使用 ---由于最近项目中用到了分页这里也来记录一下,一方面给自己一个记录,另一方面给后来者一些帮助! 一.首先大家先来看一下效果

  3. JAVA深入研究——Method的Invoke方法

    http://www.cnblogs.com/onlywujun/p/3519037.html 在写代码的时候,发现Method可以调用子类的对象,但子类即使是改写了的Method,方法名一样,去调用 ...

  4. ios专题 - 图片(UIImage)获取方法

    说到图片获取的方法,就得看API文档. UIImage生成实例的方法有: 1)imageNamed 从指定文件返回对象. 这个方法有个比较特殊的地方:该方法首先从系统缓存中寻找该图片,如果有,则从缓存 ...

  5. js 获取时间对象代码

    /** * 获取时间对象 */function getDateObj(addDayCount) { var dd = new Date(); dd.setDate(dd.getDate()+addDa ...

  6. windows server 2008镜像重启后密码变为默认密码的问题的解决方案

    1. cmd中执行regedit,打开注册表: 修改HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Cloudbase Solusions\Cloudbase-Init ...

  7. 第3章文件I/O总结

    1. open和create函数在fcntl.h中,close.lseek.read.write函数在unistd.h中 open函数通过进程有效用户ID判断读文件的权限 可以调用access函数判断 ...

  8. 专题二、ArrayList序列化技术细节详解

    一.绪论 所谓的JAVA序列化与反序列化,序列化就是将JAVA 对象以一种的形式保持,比如存放到硬盘,或是用于传输.反序列化是序列化的一个逆过程. JAVA规定被序列化的对象必须实现java.io.S ...

  9. Linux命令——监视相关

    1.netstat 用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Membershi ...

  10. 记录一次mount问题

    linux  centos 6.5 _64 oracle 11.2g 今天接到一个客户电话说重启了服务器,数据库没有重启来,看了数据库的报错 没有找到control文件的路径,数据库启动到了 unmo ...