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. Android开发——构建自定义组件

    Android中,你的应用程序程序与View类组件有着一种固定的联系,例如按钮(Button). 文本框(TextView), 可编辑文本框(EditText), 列表框(ListView), 复选框 ...

  2. javascript-01

    1.JavaScript:浏览器脚本语言 2.JavaScript的作用  |-1.进行前端验证  |-2.实现页面的动态效果 3.JavaScript的特点  |-1.和java没有任何关系,官方标 ...

  3. 纯js写“运动”框架

    所谓“运动”不一定真的是运动,在连续的一段时间内改变某一样式都可以成为“运动”. 先写几个会用到的函数 //获取某一元素的指定样式 function getstyle (element, target ...

  4. Android中log4j的运用

    网上一查关于android上面运用Log4j的运用,各种说需要添加多样的包的,照着log4j的官网教程看了下,给了个简单的输出到console上面的代码,似乎没什么用.看网上关于Log4j更多是在ja ...

  5. php session_id()函数介绍及代码实例

    session_id()功能: 获取设置当前回话ID. 函数说明: string session_id ([ string $id ] ) 参数: 如果指定了参数$id,那么函数会替换当前的回话id. ...

  6. Java根据出生年月日获取到当前日期的年月日

    源码链接:http://pan.baidu.com/s/1sj61IUD

  7. bootstrap 正则表达式

    <asp:TextBox runat="server"  title="邮箱正确格式:xxx@xxx.xxx" class="form-cont ...

  8. 转载:为什么Linux不需要磁盘碎片整理

    转载自:www.aqee.net 如果你是个Linux用户,你可能听说过不需要去对你的linux文件系统进行磁盘碎片整理.也许你注意到了,在Liunx安装发布包里没有磁盘碎片整理的工具.为什么会这样? ...

  9. C#中2、8、16进制 有符号转换10进制正负数

    曾经让我苦想的其他进制转有符号整型问题,结果自己想到方法解决后才发现原来如此简单. 1.Int16(2个byte长度 ) : 方法 :Convert.ToInt16(进制编码,进制) a.16进制转1 ...

  10. 10_Mybatis开发Dao方法——mapper代理实现

    [工程截图(几个关键的标红框)] [UserMapper.xml] <?xml version="1.0" encoding="UTF-8"?> & ...