uva 11922 - Permutation Transformer
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的更多相关文章
- UVA 11922 Permutation Transformer(平衡二叉树)
Description Write a program to transform the permutation 1, 2, 3,..., n according to m instructions. ...
- UVa 11922 - Permutation Transformer 伸展树
第一棵伸展树,各种调试模板……TVT 对于 1 n 这种查询我处理的不太好,之前序列前后没有添加冗余节点,一直Runtime Error. 后来加上冗余节点之后又出了别的状况,因为多了 0 和 n+1 ...
- UVA 11922 Permutation Transformer(Splay Tree)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18902 [思路] 伸展树+打标记. 用伸展树维护这个序列,使得能 ...
- UVA - 11922 Permutation Transformer (splay)
题目链接 题意:你的任务是根据m条指令改变排列{!,2,3,...,n}.每条指令(a,b)表示取出第a~b个元素,翻转后添加到排列的尾部.输出最终序列. 解法:splay对区间分裂合并翻转,模板题. ...
- UVA 11922 Permutation Transformer (Splay树)
题意: 给一个序列,是从1~n共n个的自然数,接下来又m个区间,对于每个区间[a,b],从第a个到第b个从序列中分离出来,翻转后接到尾部.输出最后的序列. 思路: 这次添加了Split和Merge两个 ...
- UVA 11922 Permutation Transformer —— splay伸展树
题意:根据m条指令改变排列1 2 3 4 … n ,每条指令(a, b)表示取出第a~b个元素,反转后添加到排列尾部 分析:用一个可分裂合并的序列来表示整个序列,截取一段可以用两次分裂一次合并实现,粘 ...
- uva 11922 Permutation Transforme/splay tree
原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18902 伸展树的区间翻转剪切... 如下: #include< ...
- UVA 11922 Splay tree
UVA 11922 题意: 有n个数1~n 操作a,b表示取出第a~b个数,翻转后添加到数列的尾部 输入n,m 输入m条指令a,b 输出最终的序列 代码: #include<iostream&g ...
- UVA 12003 Array Transformer
Array Transformer Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on UVA. Orig ...
随机推荐
- Java基础知识强化之IO流笔记62:三种方式实现键盘录入
1. 三种方式实现键盘录入 System.in 标准输入流.是从键盘获取数据的 键盘录入数据三种方式: A:main方法的args接收参数. java HelloWorld hello w ...
- iOS类初始化
类继承下来的初始化有三种: +(void)load: +(void)initialize: -(instancetype)init: +(void)load:会自动调用(也可手动调用),只要有引用 ...
- mysql远程连接错误提醒:2013-Lost connection to MySQL server at ‘reading initial communication packet', system error: 0
因为没有匹配/etc/hosts.allow. 解决方法: 1.在hosts.allow 文件中添加 mysqld:ALL [root@ucDB204 ~]# cat /etc/hosts.allow ...
- inux设置普通用户无密码sudo权限
配置普通用户无密码sudo权限: root用户进入到Linux系统的/etc目录下 cd /etc 将sudoers文件赋予写的权限 chmod u+w /etc/sudoers 编辑sudoers文 ...
- IBM WebSphere MQ 通道类型配置
IBM WebSphere MQ 通道类型配置 初学MQ,四种常见通道,windows下操作 目录 Sender--Receiver Server-Receiver Server-Requester ...
- Python入门 学习笔记 (二)
今天学习了一些简单的语法规则,话不多说,开始了: 二.数据类型 常用数据类型中的整形和浮点型就不多说了. 1.字符串 ...
- java IO文件读写例子(OutputStream,InputStream,Writer,Reader)
一,File创建文件 File file = new File("D:" + File.separator + "yi.txt"); 代码示例: package ...
- UVA 11462 Age Sort(计数排序法 优化输入输出)
Age Sort You are given the ages (in years) of all people of a country with at least 1 year of age. Y ...
- UVA 11384 Help is needed for Dexter(问题转化 递归)
Help is needed for Dexter Time Limit: 3 Second Dexter is tired of Dee Dee. So he decided to keep Dee ...
- MinGW 仿 linux 开发环境
MinGW 默认安装 MSYS.通常打开的 MinGW Shell 其实 MSYS,MinGW 作为一个组件存在. MSYS -- Minimal SYStem,是一个 Bourne Shell 解释 ...