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 ...
随机推荐
- jquery的效果地址
http://www.cnblogs.com/lhb25/p/50-jquery-plugins-g.html
- tinyxml_settattr
TiXmlString& TiXmlString::assign(const char* str, size_type len) { size_type cap = capacity(); * ...
- Mac通过以太网共享网络
在日常工作和学习中,需要WiFi热点而没有路由器,这个时候我们可以用我们工作的Mac来共享网络. 系统偏好设置->共享->互联网共享:设置共享来源和共享端口->WiFi选项:设置网络 ...
- C++学习(四)
一.拷贝构造函数和拷贝赋值运算符1.拷贝构造:用一个已有的对象,构造和它同类型的副本对象——克隆.2.形如class X { X (const X& that) { ... }};的构造函数 ...
- 关于MD5校验和java工程下的校验
File file = new File("cos_code2003.bin"); System.out.println(file.length()); byte[] data = ...
- SQL Server调优系列基础篇 - 联合运算符总
前言 上两篇文章我们介绍了查看查询计划的方式,以及一些常用的连接运算符的优化技巧,本篇我们总结联合运算符的使用方式和优化技巧. 废话少说,直接进入本篇的主题. 技术准备 基于SQL Server200 ...
- Java并发编程:进程和线程之由来__进程让操作系统的并发性成为可能,而线程让进程的内部并发成为可能
转载自海子:http://www.cnblogs.com/dolphin0520/p/3910667.html Java多线程基础:进程和线程之由来 在前面,已经介绍了Java的基础知识,现在我们来讨 ...
- SQL 查找存储过程及视图与自带函数
查找所有所有存储过程的名称及信息select * from sysobjectswhere type='P' 查看存储过程定义语句sp_helptext [存储过程名] 查看所有视图及信息select ...
- 代码:Masonry 第三方框架
必备宏使用前提: //define this constant if you want to use Masonry without the 'mas_' prefix #define MAS_SHO ...
- javascript DOM 节点 第18节
<html> <head> <title>DOM对象</title> </head><body><div >DOM对 ...