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. MYSQL小常识

    在mysql里面利用str_to_date()把字符串转换为日期 此处以表T_TGS_ALARMED的BJSJ为例,查询当前时间在此范围之内的数据. insert into T_TGS_ALARMED ...

  2. ModelAndView使用方法

    配置支持ModelAndView 在application.xml中配置支持ModelAndView,配置方式有两种. 配置一 <bean id="ViewResolver" ...

  3. 学点css基础

    中午时间学点css,附带http://www.w3cschool.cc/css/css-tutorial.html这个链接! 中午的时间学了这些东西!如下图: 附带代码: <!DOCTYPE h ...

  4. MyEclipse常见配置及调试

    常见配置 1.配置workspace ----- 建议不要采用含有空格和中文目录 所有代码保存workspace空间中2.新建工程时,设置工程需要jre环境MyEclipse提供多种内置layout ...

  5. Masonry约束崩溃

    报错: Trapped uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint const ...

  6. 用java制作日历,想休息的时候看一看离周末还有几天!

    呀!忙碌的每一天,都忘记了明天就是我们愉快周末了.今天没有朋友的闲聊的话,明天处在绷紧的状态呢!还有朋友提到,所有今天来跟大家分享一下用java来编写日历表,累了想休息了的时候,打开看看,还有几天到周 ...

  7. scrapy shell 中文网站输出报错.记录.

    UnicodeDecodeError: 'gbk' codec can't decode bytes in position 381-382: illegal multibyte sequence 上 ...

  8. 安装"MySQLdb"一波三折.

    在慕课网学习课程"Python操作MySQL数据库",安装"MySQLdb"时遇到问题. 先是找错地方: 百度搜索"Mysql for Python& ...

  9. 16_AOP入门准备_动态代理模式

    [工程截图] [PersonDao.java] package com.HigginCui.daoProxy; //目标类接口 public interface PersonDao { public ...

  10. Essential C++ 学习笔记01--基本语法

    <Essential C++>1.1-1.4节笔记 1. main 函数 main 函数是代码的入口,若无 main 函数,编译不通过. main 函数通常声明为 int, return ...